DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Programming with sockets

Closing sockets

Once a socket is no longer of interest, it may be discarded by applying a close to the descriptor,

   close(s);
If data is associated with a socket that promises reliable delivery (for example, a stream socket) when a close takes place, the system will continue to attempt to transfer the data. However, if the data is still undelivered after a fairly long period of time, it will be discarded. If a user has no use for pending data, a shutdown(S) may be performed on the socket before closing it. This call is of the form:
   shutdown(s, how);
where how is 0 if the user is no longer interested in reading data, 1 if no more data will be sent, and 2 if no data is to be sent or received.

The following two code samples illustrate how to initiate and accept an Internet domain stream connection.


   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <netdb.h>
   #include <stdio.h>
   

#define DATA "Half a league, half a league . . ."

/* * This program creates a socket and initiates a connection with the socket * given in the command line. One message is sent over the connection and * then the socket is closed, ending the connection. The form of the command * line is: streamwrite hostname portnumber */

main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in server; struct hostent *hp, *gethostbyname(); char buf[1024];

/* Create socket. */ sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) { perror("opening stream socket"); exit(1); } /* Connect socket using name specified by command line. */ server.sin_len = sizeof(server); 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 (write(sock, DATA, sizeof(DATA)) < 0) perror("writing on stream socket"); close(sock); exit(0); }


   #include <sys/types.h>
   #include <sys/socket.h>
   #include <netinet/in.h>
   #include <netdb.h>
   #include <stdio.h>
   #define TRUE 1
   

/* * This program creates a socket and then begins an infinite loop. Each time * through the loop it accepts a connection and prints out messages from it. * When the connection breaks, or a termination message comes through, the * program accepts a new connection. */

main() { int sock, length; struct sockaddr_in server; 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); } /* Name socket using wildcards. */ server.sin_len = sizeof(server); server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = 0; if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { perror("binding stream socket"); exit(1); } /* Find out assigned port number and print it out. */ 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 accepting connections. */ listen(sock, 5); do { msgsock = accept(sock, (struct sockaddr *)0, (int *)0); if (msgsock == -1) perror("accept"); else do { memset(buf, 0, sizeof(buf)); if ((rval = read(msgsock, buf, 1024)) < 0) perror("reading stream message"); if (rval == 0) printf("Ending connection\n"); else printf("-->%s\n", buf); } while (rval != 0); close(msgsock); } while (TRUE); /* * Since this program has an infinite loop, the socket "sock" is * never explicitly closed. However, all sockets will be closed * automatically when a process is killed or terminates normally. */ exit(0); }


© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005