Loading...
Select Version
&pagelevel(5)&pagelevel
The following two program examples illustrate how a streams connection in the Internet domain is initialized by the client and accepted by the server:
Example 1: Initialization of a streams connection by the client
#include <sys.types.h> #include <sys.socket.h> #include <netinet.in.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #define DATA "Half a league, half a league . . ." /* * This program creates a socket and initializes a connection with the * Internet address passed in the command line. * A message is sent via the connection. * The socket is then closed and the connection shut down. * The client program expects the entry of a host name and * port number. It is the host on which the server program runs and * the port number of the list socket of the server program (in the example * the port number 2222). */ main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in server; struct hostent *hp; /* Create a socket */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /* Connection setup using the name specified in the * command line. */ server.sin_family = AF_INET; hp = gethostbyname(argv[1]); if (hp == 0) { fprintf(stderr, "%s: unknown host\n", argv[1]); exit(2); } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); server.sin_port = htons(atoi(argv[2])); if (connect(sock, (struct sockaddr*)&server, sizeof server) < 0) { perror("connecting stream socket"); exit(1); } if (send(sock, DATA, sizeof DATA, 0) < 0) perror("writing on stream socket"); soc_close(sock); exit(0); }
Example 2: Acceptance of the streams connection by the server
#include <sys.types.h> #include <sys.socket.h> #include <netinet.in.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #define TRUE 1 #define TESTPORT 2222 /* * This program creates a socket and then goes into an endless loop. * With each loop run, it accepts a connection and sends messages. * If the connection is interrupted or a termination message is passed, * the program accepts a new connection. */ main() { int sock, length; struct sockaddr_in server, client; int msgsock; char buf[1024]; int rval; /* Create socket. */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /* The socket is assigned a name using wildcards. */ server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(TESTPORT); if (bind(sock, (struct sockaddr*)&server, sizeof server ) < 0) { perror("binding stream socket"); exit(1); } /* Start acceptance of connection requests. */ listen(sock, 5); do { length = sizeof client; msgsock = accept(sock, (struct sockaddr*)&client, &length); if (msgsock == -1) perror("accept"); else do { memset(buf, 0, sizeof buf ); if ((rval = recv(msgsock, buf, 1024, 0)) < 0) perror("reading stream message"); else if (rval == 0) printf("Ending connection\n"); else printf("-->%s\n", buf); } while (rval > 0); soc_close(msgsock); } while (TRUE); /* * As this program runs in an endless loop, the socket "sock" is * never explicitly closed. * However, all sockets are closed automatically if a task is * terminated or reaches its normal conclusion. */ exit(0); }