netconnect server_name, port_number
netconnect ( server_name, port_number )
netconnect socket_number, server_name, port_number
netconnect ( socket_number, server_name, port_number )
Open a network connection (client) to a server. The IP address or host name of a server are specified in the server_name argument, and the specific network port number in the port_number argument. If socket_number is not specified socket number zero (0) will be used.
Open two instances of BASIC-256 on a single computer. Paste the “server” code into one and the “client” code into the other. Run the server first and the client second. You can see how the messages are sent back and forth between the two different processes.
# get a message and send back success print "wait for connection on " + netaddress() netlisten 9997 print "got connection" do while not netdata pause .1 print "."; end while n$ = netread print n$ netwrite "I got '" + n$ + "'." until n$ = "end" netclose
will display (where xxx.xxx.xxx.xxx is the IPv4 address of your computer)
wait for connection on xxx.xxx.xxx.xxx got connection .1 Hi There ....2 Hi There ........3 Hi There ..........4 Hi There .....5 Hi There .......6 Hi There ....7 Hi There ..........8 Hi There ....9 Hi There .....10 Hi There .end
# have the user enter a message and send it to the server input "enter message?", m$ netconnect "127.0.0.1", 9997 for t = 1 to 10 pause rand netwrite t + " " + m$ print netread next t netwrite "end" print netread netclose
enter message?Hi There I got '1 Hi There'. I got '2 Hi There'. I got '3 Hi There'. I got '4 Hi There'. I got '5 Hi There'. I got '6 Hi There'. I got '7 Hi There'. I got '8 Hi There'. I got '9 Hi There'. I got '10 Hi There'. I got 'end'.