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


Binding using socket call gethostbyname

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

Figure 1 shows another example of socket call bind(). It uses the utility routine gethostbyname() to find the internet address of the host, rather than using socket call inet_addr with a specific address.

Figure 1. A bind() call using gethostbyname()
int bind(int s, struct sockaddr *name, int namelen);
.
.
.
int rc;
int s;
char *hostname = “myhost”;
struct sockaddr_in myname;
struct hostent *hp;
 
   hp = gethostbyname(hostname);
 
   /* clear the structure to clear the sin_zero field */
   memset(&myname,0,sizeof(myname));
   myname.sin_family = AF_INET;
   myname.sin_addr.s_addr = *((unsigned long *)hp->h_addr);
   myname.sin_port = htons(1024);
⋮
rc = bind(s,(struct sockaddr *) &myname, sizeof(myname));

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014