The two following program examples illustrate how a streams connection in the Internet domain is initialized by the client and accepted by the server:
The example programs are only valid for the communications domain AF_INET. If they are modified according to the information in the sections "Socket addressing" and "Creating a socket", they are also valid for the AF_INET6 domain.
Example 1: Initialization of a streams connection by the client
This program creates a socket and initializes a connection with the socket passed in the command line. A message is sent over the connection. The socket is then closed and the connection shut down.
The program call is: programname hostname portnumber
#include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #define DATA "Half a league, half a league . . ." int main(int argc, char **argv) { int sock; struct sockaddr_in server; struct hostent *hp; /* Create socket. */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /* Connection setup using the specified name. */ 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"); close(sock); exit(0); }
Example 2: Acceptance of the streams connection by the server
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. As this program runs in an endless loop, the socket is never explicitly closed. However, all sockets are closed automatically if a process is terminated or reaches its normal termination.
#include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <stdio.h> #define TRUE 1 #define TESTPORT 2222 int main(int argc, char **argv) { 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 name 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); } /* Find and output the appropriate port number. */ length = sizeof server; if (getsockname(sock, (struct sockaddr *)&server, &length) < 0) { perror("getting socket name"); exit(1); } printf("Socket port #%d\n", ntohs(server.sin_port)); /* 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); close(msgsock); } while (TRUE); /* NOTREACHED */ exit(0); }