<< Previous | Index | Next >>

tcp_open


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

Description

This function actively creates a session with another machine. After a call to tcp_open(), 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.

Parameters

s

Pointer to a socket structure.

lport

Our local port. Use zero for the next available port in the range 1025- 65536. A few applications will require you to use a particular local port number, but most network applications let you use almost any port with a certain set of restrictions. For example, FINGER or TELNET clients can use any local port value, so pass the value of zero for lport and let DCRTCP.LIB pick one for you.

remip

IP address to connect to.

port

Port to connect to.

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.

Return value

 0: Unable to resolve the remote computer's hardware address.
!0 otherwise.

Library

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

See also

tcp_listen

Example Using tcp_open()


// 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 of setting network addresses
#define TCPCONFIG 1

#memmap xmem
#use "dcrtcp.lib"

#define ADDRESS "10.10.6.19"
#define PORT "200"

main() {
word status;
word port;
longword host;
tcp_Socket tsock;

   sock_init();

   if (!(host = resolve(ADDRESS))) {
puts("Could not resolve host");
exit( 3 );
}
port = atoi( PORT );

   printf("Attempting to open '%s' on port %u\n\r", ADDRESS, port );

   if ( !tcp_open( &tsock, 0, host, port , NULL )) {
puts("Unable to open TCP session");
exit( 3 );
}

   printf("Waiting a maximum of %u seconds for connection"\         " to be established\n\r", sock_delay );

   while (!sock_established(&tsock) && sock_bytesready(&tsock)== -1){

      tcp_tick(NULL);
}
puts("Socket is established");
sock_close( &tsock );
exit( 0 );
}


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