The server uses the following socket interface functions in the example program:
getbcamhost(): get the host name entry
socket(): create socket
bind(): assign a socket a name
listen(): “listen” to a socket for connection requests
accept(): accept a connection on a socket
sendmsg(): send a message from socket to socket / confirm connection
recv(): read data from a socket
soc_close(): close socket
Example: Connection-oriented server for AF_ISO
/* * Example: ISO SERVER * * DESCRIPTION * 1. getbcamhost - socket - bind - listen - accept - sendmsg * 2. recv * 3. soc_close */ #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <iso.h> #include <netinet/in.h> #include <netdb.h> #define INT 5 #define MAXREC 1000000 #define MAXTSEL 32 #define MAXNSEL 9 main(argc, argv) int argc; char *argv[]; { void error_exit(); int sockfd, newfd, clilen, ret; int tsellen, nsellen; char tsel[MAXTSEL]; char nsel[MAXNSEL]; char buffer [MAXREC]; struct sockaddr_iso cli_addr, serv_addr; struct msghdr message; struct cmsghdr cmessage; strcpy (tsel,"SERVER"); tsellen = strlen(tsel); nsel[8] = '\0'; /* Get BCAM host name */ errno = 0; if(getbcamhost(nsel,sizeof(nsel)) < 0) error_exit("ISO_svr: getbcamhost failed ",errno); else printf ("getbcamhost OK! (%s)\n",nsel); nsellen = strlen(nsel); /* Create socket*/ errno = 0; if((sockfd = socket(AF_ISO, SOCK_STREAM, 0)) < 0) error_exit("ISO_svr: Socket Creation failed ",errno); else printf("socket OK!\n"); /* Assign the socket a name */ memset ((char *) &serv_addr, 0, sizeof(serv_addr)); serv_addr.siso_len = sizeof (struct sockaddr_iso); serv_addr.siso_family = AF_ISO; serv_addr.siso_plen = 0; serv_addr.siso_slen = 0; serv_addr.siso_tlen = tsellen; serv_addr.siso_addr.isoa_len = tsellen + nsellen; memcpy (serv_addr.siso_addr.isoa_genaddr,nsel,nsellen); memcpy (serv_addr.siso_addr.isoa_genaddr + nsellen,tsel,tsellen); errno = 0; if(bind(sockfd, (struct sockaddr_iso *) &serv_addr, sizeof(serv_addr)) < 0) error_exit("ISO_svr: Bind failed ",errno); else printf("bind OK!\n"); /* Start acceptance of connection requests */ errno = 0; if (listen(sockfd, INT) < 0) error_exit("ISO_svr: Listen failed ",errno); else printf("listen OK!\n"); errno = 0; clilen = sizeof(cli_addr); newfd = accept(sockfd, (struct sockaddr_iso *) &cli_addr, &clilen); if(newfd < 0) error_exit("ISO_svr: New Socket Creation failed",errno); else printf("accept OK!\n"); /* Confirm connection request (CONNECTION CONFIRM) No actual transfer of data takes place */ memset ((char *) &message, 0, sizeof(message)); memset ((char *) &cmessage, 0, sizeof(cmessage)); message.msg_control = (char *) &cmessage; message.msg_controllen = sizeof (struct cmsghdr); cmessage.cmsg_level = SOL_TRANSPORT; cmessage.cmsg_type = TPOPT_CFRM_DATA; cmessage.cmsg_len = sizeof (struct cmsghdr); errno = 0; ret = sendmsg (newfd, (struct msghdr *) &message, 0); if (ret == -1) error_exit("ISO_svr: Sendmsg in Error", errno); else printf("sendmsg OK!(%d)\n",ret); /* Read data from a socket */ if ((ret = recv (newfd, buffer, MAXREC, 0)) < 0) { if (errno != EPIPE) /* Broken Pipe */ error_exit("ISO_svr: Receive in Error", errno); } else printf("recv OK!(%d)\n",ret); /* Close socket */ errno = 0; if (soc_close (newfd) < 0) error_exit("ISO_svr: soc_close failed ",errno); else printf("soc_close (newfd) OK!\n"); if (soc_close (sockfd) < 0) error_exit("ISO_svr: soc_close failed ",errno); else printf("soc_close (sockfd) OK!\n"); } /* END MAIN */ void error_exit(estring,erno) char *estring; int erno; { fprintf(stderr,"%s errno=%d\n",estring,erno); perror (estring); exit(erno); }
The server determines the BCAM host name with the getbcamhost() function.The following steps are executed in the program example for AF_ISO:
The server creates a communications endpoint (server socket) and the corresponding descriptor with the socket() function.
The server assigns the newly created socket a name with bind().
The server (socket) is prepared for accepting connection requests with listen().
The (server) socket accepts a connection request with accept().
The server confirms the connection request (CFRM) with sendmsg(), i.e. the connection has now been set up. sendmsg() does not transfer any user data.
The server socket receives user data from the partner socket (client socket) with recv().
The (server) socket is closed with the function soc_close().