12.07.2015 Views

z/VM: TCP/IP Programmer's Reference - z/VM - IBM

z/VM: TCP/IP Programmer's Reference - z/VM - IBM

z/VM: TCP/IP Programmer's Reference - z/VM - IBM

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

sockets. In the AF_IUCV domain, the type must be SOCK_STREAM. Theprotocol parameter is a constant that specifies the protocol to use, and isignored unless type is set to SOCK_RAW. Passing 0 chooses the defaultprotocol. If successful, socket() returns a positive integer socket descriptor.2. Once an application has a socket descriptor, it can explicitly bind() a uniquename to the socket, as in the example shown in Figure 2. For a completedescription, see “bind()” on page 23.int bind(int s, struct sockaddr *name, int namelen);.int rc;int s;struct sockaddr_in myname;C Sockets Application Program Interface./* clear the structure to be sure that the sin_zero field is clear */memset(&myname, 0, sizeof(myname));myname.sin_family = AF_INET; /* internet addressing family */myname.sin_addr.s_addr = inet_addr(“129.5.24.1”); /* specific interface */myname.sin_port = htons(1024); /* port number */rc = bind(s, (struct sockaddr *) &myname, sizeof(myname));Figure 2. An Application Uses the bind() CallThis example binds myname to socket s. The name specifies that the applicationis in the internet domain (AF_INET) at internet address 129.5.24.1, and isbound to port 1024. Servers must bind a name to become accessible from thenetwork. The example in Figure 2 shows two useful utility routines:vinet_addr() takes an internet address in dotted-decimal form and returns itin network byte order. For a complete description, see “inet_addr()” onpage 52.v htons() takes a port number in host byte order and returns the port innetwork byte order. For a complete description, see “htons()” on page 51.For another example of the bind() call, see Figure 3. It uses the utility routinegethostbyname() to find the internet address of the host, rather than usinginet_addr() with a specific address.int bind(int s, struct sockaddr_in name, int namelen);.int rc;int s;char *hostname = “myhost”;struct sockaddr_in myname;struct hostent *hp;hp = gethostbyname(hostname);/*clear the structure to be sure that the sin_zero field is clear*/memset(&myname,0,sizeof(myname));myname.sin_family = AF_INET;myname.sin_addr.s_addr = *((unsignedlong *)hp->h_addr);myname.sin_port = htons(1024);.rc = bind(s,(struct sockaddr *) &myname, sizeof(myname));Figure 3. A bind() Call Uses the gethostbyname() CallChapter 2. C Sockets Application Program Interface 9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!