TCP/IP Programming Manual
/* Bind the socket */
if (bind (fd,(struct sockaddr *)&sin, (int)sizeof (sin)) < 0) {
perror ("SERVER: bind");
exit (0);
}
printf ("SERVER: BIND completed ...\n");
if (listen (fd, 5) < 0) {
perror ("SERVER: listen");
exit (0);
}
printf ("SERVER: Listening on socket # %d \n", fd);
if ((s2 = accept (fd,(struct sockaddr *)&from, &flen)) < 0) {
perror ("SERVER: accept");
exit (0);
}
printf ("SERVER: Connected ...\n");
total_read = 0;
while ((cc = recv (s2, buf, sizebuf,0)) > 0) {
printf ("SERVER: read %d bytes ... \n",cc);
total_read += (long)cc;
printf ("SERVER: copying buffer to file ... \n");
if ((wc=write(fo,buf,cc)) <0) {
printf ("SERVER: write failed\n");
exit(0);
}
else
printf ("SERVER: copied %d bytes \n",wc);
}
(void) FILE_CLOSE_ ((short int)s2);
printf ("SERVER: Receive completed.\n");
FILE_CLOSE_((short int)fo);
exit(0);
usage:
fprintf(stderr, "usage: SERVER recv_file port proc_name\n");
exit(0);
}
Client and Server Programs Using UDP
This subsection contains a client and a server program that demonstrate a UDP communication.
The client on one NonStop system sends a string of characters entered by a user to the server on
another NonStop system. The server sends (echoes) the string back to the client.
TIP: When using the NonStop TCP/IPv6 network mode to call the socket_ioctl function, you
must configure the “Family” attribute to “DUAL” in the PROVIDER object (associated with the
CIPSAM process). If the Family attribute is set to "INET", all NonStop TCP/IPv6 addresses are
ignored and not returned to the socket_ioctl caller. When the attribute is set to DUAL, the NonStop
TCP/IPv6 addresses are returned, but the size of the entries are variable and based on the actual
address type:
• For a NonStop TCP/IP address, IFNAMSIZ=sizeof(struct sockaddr) bytes is passed
back.
• For a NonStop TCP/IPv6 address, IFNAMSIZ=sizeof(struct sockaddr_in6)bytes is
passed back.
UDP Client Program
The following programming example shows how to use the socket routines in a UDP client
application using the NonStop TCP/IP network mode:
#pragma nolist
#include <$system.ztcpip.param.h>
#include <$system.ztcpip.socket.h>
#include <$system.ztcpip.ioctl.h>
#include <$system.ztcpip.in.h>
#include <$system.ztcpip.netdb.h>
Programs Using AF_INET Sockets 219