User Tools

Site Tools


pt:netconnect

Differences

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

Link to this comparison view

pt:netconnect [2020/02/28 10:46] (current)
Line 1: Line 1:
 +===== NetConnect =====
  
 +==== Formato ====
 +**netconnect** //server_name//, //port_number//\\
 +**netconnect** ( //server_name//, //port_number// )\\
 +**netconnect** //socket_number//, //server_name//, //port_number//\\
 +**netconnect** ( //socket_number//, //server_name//, //port_number// )
 +
 +==== Descrição ====
 +Abre uma ligação em rede (client) com um servidor.  O endereço IP ou nome do servidor são especificados em //server_name// argument, e o número da entrada (port) em //port_number// Se o número da ligação //socket_number// não for especificado então o (0) será usado.
 +
 +==== Exemplo ====
 +Abra duas instâncias de Basic 256 no mesmo computador.  Copie o código "servidor" para um e o código "cliente" para outro.  Corra primeiro o servidor e depois o cliente.  Note como as duas instancias do programa comunicam.
 +=== Servidor ===
 +<code>
 +# 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
 +</code>
 +will display (where xxx.xxx.xxx.xxx is the IPv4 address of your computer)
 +<code>
 +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
 +</code>
 +
 +=== Cliente ===
 +<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>
 +
 +==== Ver também ====
 +[[NetAddress|NetAddress]], [[NetClose|NetClose]], [[NetData|NetData]], [[NetListen|NetListen]], [[NetRead|NetRead]], [[NetWrite|NetWrite]]
 +
 +==== Desde ==== 
 +0.9.6.31
pt/netconnect.txt · Last modified: 2020/02/28 10:46 (external edit)