In the example program, the client uses the following socket interface functions:
getbcamhost(): get BCAM host name
socket(): create socket
bind(): assign a name to the socket
connect(): request a connection on the socket
send(): write data to the socket
soc_close(): close socket
Example: Connection-oriented client for AF_ISO
/* * Example: ISO CLIENT * * DESCRIPTION * 1. getbcamhost - socket - bind - connect * 2. send * 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, ret, lng; int tsellen, nsellen, par_tsellen, par_nsellen; char tsel[MAXTSEL]; char par_tsel[MAXTSEL]; char nsel[MAXNSEL]; char par_nsel[MAXNSEL]; char buffer [MAXREC]; struct sockaddr_iso cli_addr, serv_addr; lng = 1024 ; strcpy (tsel,"CLIENT"); tsellen = strlen(tsel); strcpy (par_tsel,"SERVER"); par_tsellen = strlen(par_tsel); nsel[MAXNSEL-1] = '\0'; /* Get partner host name */ if (argc > 1) { strcpy (par_nsel,argv[1]); if ((par_nsellen = strlen(par_nsel)) != MAXNSEL - 1) { printf ("Error: Invalid host name !!\n"); exit (-1); } } else { printf ("Partner host name was not passed as an argument in the command line !\n"); exit (-1); } /* Get BCAM host name*/ errno = 0; if (getbcamhost(nsel,sizeof(nsel)) < 0) error_exit("ISO_cli: 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_cli: Socket Creation failed ",errno); else printf ("socket OK!\n"); /* Assign a name to the socket */ memset ((char *) &cli_addr, 0, sizeof(cli_addr)); cli_addr.siso_len = sizeof (struct sockaddr_iso); cli_addr.siso_family = AF_ISO; cli_addr.siso_plen = 0; cli_addr.siso_slen = 0; cli_addr.siso_tlen = tsellen; cli_addr.siso_addr.isoa_len = tsellen + nsellen; memcpy (cli_addr.siso_addr.isoa_genaddr,nsel,nsellen); memcpy (cli_addr.siso_addr.isoa_genaddr + nsellen,tsel,tsellen); 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 = par_tsellen; serv_addr.siso_addr.isoa_len = par_tsellen + par_nsellen; memcpy (serv_addr.siso_addr.isoa_genaddr,par_nsel,par_nsellen); memcpy (serv_addr.siso_addr.isoa_genaddr + par_nsellen,par_tsel,par_tsellen); errno = 0; if (bind (sockfd, (struct sockaddr_iso *) &cli_addr, sizeof(cli_addr)) < 0) error_exit("ISO_cli: Bind failed ",errno); else printf ("bind OK!\n"); /* Start connection */ errno = 0; if (connect (sockfd, (struct sockaddr_iso *) &serv_addr, sizeof(serv_addr)) < 0) error_exit("ISO_cli: Connect failed ",errno); else printf ("connect OK!\n"); sleep(2); /* Write data to the socket */ ret = send (sockfd, buffer, lng, 0); if (ret == -1) error_exit("ISO_cli: Send in Error", errno); else printf ("send OK!(%d)\n",ret); /* Close socket*/ sleep (2); errno = 0; if (soc_close (sockfd) <0) error_exit("Tcp_svr: soc_close failed ",errno); else printf ("soc_close 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 client takes the name of the partner host from the command line argument argc of the main() function.The following steps are executed in the program:
The client determines the BCAM host name with the function getbcamhost().
The client creates a communications endpoint (client socket) and the corresponding descriptor with the function socket().
The client assigns a name to the newly created socket with bind().
The client sets up the connection to the communications partner (server socket) with connect().
The client send user data to the partner socket (server socket) with send().
The function soc_close() closes the (client) socket.