Sockets for Connections
Connections are intended to serve as sockets between processes on
different machines providing reliable data transmission between the
two ends. Data should arrive in the order it was sent without loss.
The Server Sequence
The following is a simplified view of how a server works in the case
of establishing connections.
- socket() used to create a socket,
- bind() attaches the socket to a port
- listen() set up queues for incoming requests
- accept() block until a connect request arrives.
- read() get a message from the socket.
- write() send message,
- close().
The Client Sequence
- socket() create the socket,
- connect() send a request to connect to a specific port,
the client needs to know the address
- write()
- read()
- close()
An example program pair.
The server
#include
#include
#include
#include
#include
#define TRUE 1
/*
* This program creates a socket and rhen begins an infinite loop. Each time
* through the loop it accepts one 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;
int i;
/* Create socket. */
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("opening stream socket");
exit(1);
}
/* Name socket using wild cards. */
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 {
bzero(buf, sizeof buf );
if ((rval = read(msgsock, buf, 1024)) < 0)
perror("reading stream message");
i = 0;
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. Hawever, all sockets will be closed
* automatically when a process is killed or terminates normally
*/
exit(0) ;
}
The Client
#include
#include
#include
#include
#include
#define DATA "Half a league, half a league ... "
/*
* This program creates a socket and initiates a connection
* with the socket given on 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 iine . */
server.sin_family = AF_INET;
hp = gethostbyname (argv[1]);
if (hp == 0){
fprintf(stderr,"%s:unknown host", argv[1]);
exit(2);
}
bcopy( (char *)hp->h_addr, (char *)&server.sin_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);
}
Jonathan Hodgson
Return to
Networks home page
Last Changed: 1 April 1997