Binding a socket to a specific port number

By binding the socket to a specific port number, you avoid having an ephemeral port number assigned to the socket.

Servers find it inconvenient to have an ephemeral port number assigned, because clients have to connect to a different port number for every instance of the server. By using a predefined port number, clients can be developed to always connect to a given port number.

Client programs can use the socket call bind(), but client programs rarely benefit from using the same port number every time they execute.

Figure 1 shows a list of BIND call variables.

Figure 1. Variables used for the BIND call
*---------------------------------------------------------------*
* Variables used for the BIND Call                              *
*---------------------------------------------------------------*
 01  server-socket-address.
     05  server-afinet              pic 9(4) binary value 2.
     05  server-port                pic 9(4) binary value 9998.
     05  server-ipaddr              pic 9(8) binary value 0.
     05  filler                     pic x(8) value low-value.
 01  socket-descriptor              pic 9(4) binary.
*---------------------------------------------------------------*
* Bind socket to our server port number                         *
*---------------------------------------------------------------*
   call 'EZASOKET' using soket-bind
      socket-descriptor
      server-socket-address
      errno
      retcode.
   if retcode < 0 then
      - process error -     

Before you issue this call, you must build a socket address structure for your own socket using the following information:

Normally, the IP address is set to INADDR_ANY, but there are situations in which you might want to use a specific IP address. Consider the case of a TCP/IP system address space having been configured with two virtual IP addresses (VIPA). One VIPA address is returned by the named server when clients resolve one host name, and the other VIPA address is returned by the name server when clients resolve the other host name. In fact, both host names represent the same TCP/IP system address space, but the host names can be used to represent two different major socket applications on that MVS™ host. If your Server A and your Server B can generate a very high amount of network traffic, your network administrator might want to implement what is known as session traffic splitting. This means that IP traffic for one server comes in on one network adapter while traffic for the other server comes in on another adapter. To facilitate such a setup, you must be able to bind the server listener socket to one of the two VIPA addresses.

At this point in the process, you have not told TCP/IP anything about the purpose of the socket you obtained. You are free to use it as a client to issue connect requests to servers in the IP network, or use it to become a server yourself. In terms of the socket, it is, at the moment, active; this is the default status for a newly created socket.