#include #include #include #include #include #include #include #include #include #include #include /* change hostent to comply with BSD 4.3 */ #include #include /* INET symbol definitions */ void cleanup (int how_many, int sock1, int sock2); /*--------------------------------------------------------------------*/ void main (void) { int sock_2, sock_3; /* sockets */ static struct sockaddr_in sock2_name; /* Address struct for socket2.*/ static struct sockaddr_in retsock2_name; /* Address struct for socket2.*/ struct hostent hostentstruct; /* Storage for hostent data. */ struct hostent *hostentptr; /* Pointer to hostent data. */ static char hostname[256]; /* Name of local host. */ int flag; int retval; /* helpful for debugging */ int namelength; char buff [16384]; char OkMsg [] = "OkMsg"; #define port_number 1027 /* * Open socket 2: AF_INET, SOCK_STREAM. */ if ((sock_2 = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror( "socket"); exit(0); } /* * Get the host local name. */ retval = gethostname(hostname,sizeof hostname); if (retval) { perror ("gethostname"); cleanup (1, sock_2, 0); } else printf("Hostname = %s",hostname); /* * Get pointer to network data structure for socket 2. */ if ((hostentptr = gethostbyname (hostname)) == NULL) { perror( "gethostbyname"); cleanup(1, sock_2, 0); } /* * Copy hostent data to safe storage. */ hostentstruct = *hostentptr; /* * Fill in the name & address structure for socket 2. */ sock2_name.sin_family = hostentstruct.h_addrtype; sock2_name.sin_port = htons(port_number); sock2_name.sin_addr = * ((struct in_addr *) hostentstruct.h_addr); /* * Bind name to socket 2. */ retval = bind (sock_2, (struct sockaddr *) &sock2_name, sizeof (sock2_name)); if (retval) { perror("bind"); cleanup(1, sock_2, 0); } /* * Listen on socket 2 for connections. */ retval = listen (sock_2, 5); if (retval) { perror("listen"); cleanup(1, sock_2, 0); } /* * Accept connection from socket 2: * accepted connection will be on socket 3. */ namelength = sizeof (sock2_name); sock_3 = accept (sock_2, (struct sockaddr *) &sock2_name, &namelength); if (sock_3 == -1) { perror ("accept"); cleanup( 2, sock_2, sock_3); } /* * Receive message from socket 1. */ flag = 0; /* maybe 0 or MSG_OOB or MSG_PEEK */ while (1) { if ( 0 > recv(sock_3, buff ,sizeof (buff), flag) ) { perror ("Srv:recv"); cleanup( 2, sock_2, sock_3); } else printf ("Srv:recv=%s\n", buff); if ( 0 > send(sock_3, OkMsg,sizeof (OkMsg), flag) ) { perror ("Srv:send"); } }; /* * Call cleanup to shutdown and close sockets. */ cleanup(2, sock_2, sock_3); } /* end main */ /*-----------------------------------------------------------*/ void cleanup (int how_many, int sock1, int sock2) { int retval; /* * Shutdown and close sock1 completely. */ retval = shutdown(sock1,2); if (retval == -1) perror ("shutdown"); retval = close (sock1); if (retval) perror ("close"); /* * If given, shutdown and close sock2. */ if (how_many == 2) { retval = shutdown(sock2,2); if (retval == -1) perror ("shutdown"); retval = close (sock2); if (retval) perror ("close"); } exit(0); } /* end cleanup*/