z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Binding with a known port number

z/OS Communications Server: IP Sockets Application Programming Interface Guide and Reference
SC27-3660-00

In C, the server puts the port number and IP address into structure sockaddr_, x, passing it, and the socket, to the bind() function. For example:

bind(s, (struct sockaddr *)&x, sizeof(struct sockaddr));
After an application possesses a socket descriptor, it can explicitly bind() a unique address to that socket, as in the example listed in Figure 1. For more information about binding, see bind().
Figure 1. An application using the bind() call
int bind(int s, struct sockaddr *name, int namelen);
.
.
.
int rc;
int s;
struct sockaddr_in myname;
 
   /* clear the structure to clear the sin_zero field */
   memset(&myname,; 0, sizeof(myname));
   myname.sin_family = AF_INET;
   myname.sin_addr = inet_addr(“129.5.24.1”); /* specific interface */
   myname.sin_port = htons(1024);
⋮
   rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));
This example binds socket descriptor s to the address 129.5.24.1, and port 1024 to the internet domain. Servers must bind to an address and port to be accessible to the network. The example in Figure 1 lists two utility routines:
  • Socket call inet_addr() takes an internet address in dotted decimal form and returns it in network byte order. For a description, see inet_addr().
  • Socket call htons() takes a port number in host byte order and returns the port number in network byte order. For a description, see htons().

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014