User Tools

Site Tools


de:netconnect

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

de:netconnect [2016/01/01 22:37]
de:netconnect [2020/02/28 10:46] (current)
Line 1: Line 1:
 +===== NetConnect =====
  
 +==== Format ====
 +**netconnect** //server_name//, //port_number//\\
 +**netconnect** ( //server_name//, //port_number// )\\
 +**netconnect** //socket_number//, //server_name//, //port_number//\\
 +**netconnect** ( //socket_number//, //server_name//, //port_number// )
 +
 +==== Description ====
 +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.
 +
 +==== Example ====
 +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.
 +
 +=== Server Code ===
 +<code>
 +# get a message and send back success
 +print "wait for connection"
 +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
 +</code>
 +will display
 +<code>
 +wait for connection
 +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
 +</code>
 +
 +=== Client Code ===
 +<code>
 +# 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
 +</code>
 +will display
 +<code>
 +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'.
 +</code>
 +
 +==== See Also ====
 +[[NetClose|NetClose]], [[NetData|NetData]], [[NetListen|NetListen]], [[NetRead|NetRead]], [[NetWrite|NetWrite]]
 +
 +==== New To Version ==== 
 +0.9.6.31