TutSocketCreateServerTcp


Name

TutSocketCreateServerTcp — create the server side of a TCP/IP socket

Synopsis

T_INT4 TutSocketCreateServerTcp(port_num) 
T_INT4 port_num; 

Arguments

port_num — TCP/IP port number to associate with this socket

Return Values

Non-negative socket file descriptor if successful, -1 otherwise.

Diagnostics

If TutSocketCreateServerTcp fails, it returns -1 and sets the global SmartSockets error number to one of:

Description

TutSocketCreateServerTcp is a convenience function that creates a TCP/IP (Internet-domain) socket, binds an address to it, and listens for new connections on the socket. Once both ends of the socket are connected, data may be read from the socket with recv and written to the socket with send. The socket can be checked for input with select.

TCP/IP sockets are bidirectional and have a TCP/IP port number associated with them. port_num is used to specify this port number. When creating a client/server socket connection, the server side must always be created first (with sockets the client side must have something to connect to).

TutSocketCreateServerTcp uses the wildcard network address INADDR_ANY to ensure that the socket is visible on all network addresses.

TutSocketCreateServerTcp sets the socket option SO_REUSEADDR to ensure that the TCP/IP port number can be reused quickly if necessary.

TutSocketCreateServerTcp sets the socket option SO_LINGER to ensure that no data is discarded when the socket is closed.

Caution

TutSocketCreateServerTcp arranges for SIGPIPE signals to be ignored.

On UNIX, TutSocketCreateServerTcp sets the close-on-exec bit on the file descriptor, so that any child processes do not share the file descriptor.

If you are using Digital TCP/IP Services for OpenVMS, also known as UCX, TutSocketCreateServerTcp requires NETMBX privilege. If you are using MultiNet TCP/IP on OpenVMS, TutSocketCreateServerTcp does not require any privileges.

On Windows NT, TutSocketCreateServerTcp calls SetHandleInformation with the HANDLE_FLAG_INHERIT flag set to FALSE, so that any child processes do not share the file descriptor.

See Also

TutSocketAccept, TutSocketCreateClientTcp

Examples

This example creates a socket connection to a client, receives an integer from the client, adds one to that integer, and then sends the integer back to the client:

int main(argc, argv) 
int argc; 
char **argv; 
{ 
  T_INT4 server_fd;      /* socket for accepting new connections */ 
  T_INT4 socket_fd;      /* socket connection to client */ 
  T_INT4 a_number;       /* number read from and written to client */ 
  T_INT4 status;         /* status code */ 
 
  /* create socket to accept connections on */ 
  server_fd = TutSocketCreateServerTcp(4242); 
  if (server_fd == -1) { 
    TutOut("TutSocketCreateServerTcp failed.\n"); 
    TutExit(T_EXIT_FAILURE); 
  }  
 
  /* make sure client connects within 30 seconds */ 
  if (!TutSocketCheck(server_fd, T_IO_CHECK_READ, 30.0)) { 
    TutOut("Client did not connect in time.\n"); 
    TutExit(T_EXIT_FAILURE); 
  }  
 
  /* create connection to client */ 
  socket_fd = TutSocketAccept(server_fd); 
  if (socket_fd == -1) { 
    TutOut("TutSocketAccept failed.\n"); 
    TutExit(T_EXIT_FAILURE); 
  }  
 
  /* close original socket: we don’t need it anymore */ 
  status = T_C_CLOSESOCKET(server_fd); 
  if (status != 0) { 
    TutPerror("close"); 
  }  
 
  /* read an integer from our client */ 
  status = TutSocketRecvAll(socket_fd, (char *)&a_number, 
                                sizeof(a_number), 0); 
  if (status != sizeof(a_number)) { 
    TutOut("only read %d/%d bytes from client.\n", 
            status, sizeof(a_number)); 
    TutExit(T_EXIT_FAILURE); 
  }  
  TutOut("Number from client is %d.\n", a_number); 
 
  /* increment the integer and send it back to the client */ 
  a_number++; 
  TutOut("Sending number %d back to client.\n", a_number); 
  status = T_C_SEND(socket_fd, (char *)&a_number,  
                        sizeof(a_number), 0);   
  if (status != sizeof(a_number)) { 
    TutOut("only wrote %d/%d bytes from client.\n", 
            status, sizeof(a_number)); 
    TutExit(T_EXIT_FAILURE); 
  }  
 
  TutExit(T_EXIT_SUCCESS); 
} /* main */ 

TIBCO SmartSockets™ Utilities
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com