<< Previous | Index | Next >>

tcp_listen


int tcp_listen( tcp_Socket *s, word lport, longword remip,
word port, dataHandler_t datahandler, word reserved );

Description

This function tells DCRTCP.LIB that an incoming session for a particular port will be accepted. After a call to tcp_listen(), the function sock_established() (or the macro sock_wait_established) must be called to poll the connection until a session is fully established.

It is possible for a connection to be opened, written to and closed between two calls to the function sock_established(). To handle this case, call sock_bytesready() to determine if there is data to be read from the buffer.

Multiple calls to tcp_listen() to the same local port (lport) are acceptable and constitute the mechanism for supporting multiple incoming connections to the same local port. Each time another host attempts to open a session on that particular port, another one of the listens will be consumed until such time as all listens have become established sessions and subsequent remote host attempts will receive a reset.

Parameters

s

Pointer to a socket.

lport

Port to listen on (the local port number).

remip

IP address of the remote host to accept connections from or 0 for all.

port

Port to accept connections from or 0 for all.

datahandler

Function to call when data is received; NULL for placing data in the socket's receive buffer. Prior to Dynamic C 7.30, some details for implementation of this service had not been finalized. Insert a value of NULL if you are using a version of Dynamic C prior to 7.30.

reserved

Set to 0 for now. This parameter is for compatibility and possible future use.

Return value

0: Failure.
1: Success.

Library

TCP.LIB (Prior to DC 7.05, this was DCRTCP.LIB)

See also

tcp_extlisten

Example using tcp_listen()


// Old way of setting network addresses is commented out.
//#define MY_IP_ADDRESS "10.10.6.100"
//#define MY_NETMASK "255.255.255.0"

// New method of setting network addresses
#define TCPCONFIG 1

#memmap xmem
#use "dcrtcp.lib"

#define TELNET_PORT 23

static tcp_Socket *s;
char *userid;

telnets(int port) {
tcp_Socket telnetsock;
char buffer[ 512 ];
int status;
s = &telnetsock;

   tcp_listen( s, port, 0L, 0, NULL, 0);

   while (!sock_established(s) && sock_bytesready(s)==-1){
tcp_tick(NULL);
}
puts("Receiving incoming connection");
sock_mode( s, TCP_MODE_ASCII );
sock_puts( s, "Welcome to a sample telnet server.");
sock_puts( s, "Each line you type will be printed on"\
" this screen once you hit return.");
/*
other guy closes connection except if we timeout ... */
do {
if (sock_bytesready(s) >= 0) {
sock_gets(s, buffer, sizeof(buffer)-1);
puts ( buffer);
}
} while (tcp_tick(s));
}
main() {
sock_init();
telnets( TELNET_PORT);
exit( 0 );
}


TCP/IP Manual
Vol 1
<<Previous | Index | Next>> rabbit.com