NetConnect
NetConnect (Instrucción)
Formato
netconnect nombre_del_servidor, número_de_puerto
netconnect ( nombre_del_servidor, número_de_puerto )
netconnect número_de_socket_de_red, nombre_del_servidor, número_de_puerto
netconnect ( número_de_socket_de_red, nombre_del_servidor, número_de_puerto )
Descripción
Abre una conexión de red (cliente) con un servidor. La dirección IP o el nombre del servidor se indican en el argumento nombre_del_servidor, y el número de puerto de red en el argumento número_de_puerto. Si no se indica número_de_socket_de_red, se usa el socket número cero (0).
Ejemplo
Abre dos copias de BASIC-256 en un mismo ordenador. Pega el código del “servidor” en una y el del “cliente” en la otra. Ejecuta primero el servidor y después el cliente. Verás cómo los mensajes van y vienen entre los dos procesos.
Código del servidor
# recibir un mensaje y responder que ha llegado
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
mostrará (donde xxx.xxx.xxx.xxx es la dirección IPv4 de tu ordenador)
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
Código del cliente
# el usuario escribe un mensaje y se envía al servidor
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
mostrará
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'.
Véase también
Freenet, NetAddress, NetClose, NetConnect, NetData, NetListen, NetRead, NetWrite
Historial
| 0.9.6.31 | Nuevo en esta versión |