Sie sind auf Seite 1von 6

SOCKETS

This section introduces you to sockets. At the end of this section you should be able to explain what a socket is and write simple programs based on sockets.

Sockets
A socket is an end to end communication link between a server and a client application. This allows applications to be network aware, and send and receive data via a network. Interface details vary from computer to computer. The examples given in this section have been tested under LINIX Debian v2.1. We will detail an interface between application programs and the TCP/IP protocols using system calls based on the socket interface. In general, applications consist of a server portion and a client portion. An application program request the operating system to create a socket connection. Each time a socket connection is used, the application program must specify the destination address, or alternatively, bind the IP address to the socket. Sockets use a destination address and port number to communicate with another application. Each connection uses a specific port number, some of which are reserved (see /etc/services). Application programs written by users should take care not to existing port numbers that are already bound to running applications.

Datagram and stream sockets


The datagram protocol, also known as UDP, is connectionless. This means that each time a datagram (a packet of data to a destination) is sent, the socket and destination computers address must be included. There is a limit of 64KB for datagrams sent to a specific location. UDP is also unreliable, as there is no quarantee that the datagrams sent will arrive in the same order at the destination. The stream protocol, also known as TCP, is connection orientated. This requires a connection to be established between the sender and receiver. One of the sockets listens for a connection request (the server), the other socket asks for a connection (the client). When the server accepts the connection request from the client, data can then be sent between the server and client. In TCP there is no limit on the amount of data that can be transmitted. TCP is also a reliable protocol, in that data is received in the same order in which it was sent.

Include Files
The files to include in application programs that define sockets and the various calls associated with them are sys/types.h sys/socket.h netinet/in.h arpa/inet.h The following examples apply to the example code that appears later. For more detailed information on each of the system calls, please refer to the online manual.

Creating a socket
The socket() call creates a socket on demand. The format is int s; s = socket( AF_INET, SOCK_DGRAM, 0 ); /* specify TCP/IP and use datagrams */ If the socket was not created, -1 is returned to indicate an error. When a socket is created, it is in an unconnected state. An application program normally uses the system call connect() to bind a destination address to the socket and place it into a connected state. Sockets can be used in either connectionless datagram or as a more reliable stream. In connectionless datagram (udp), there is no guarentee of delivery. In tcp sockets, data delivery is guaranteed. recvfrom(), sendto() and sendmsg() allow udp as they require the destination address to be specified as part of the call.

Setting up a destination address and port number


An application program creates a variable of type struct sockaddr_in, then assigns the destination address and port number to this variable. In sending or receiving data on the socket connection, this variable is passed as a parameter. struct sockaddr_in server; /* set up server name and port number */ server.sin_family = AF_INET; /* use TCP/IP */ server.sin_port = 800; /* specify port 800 */ server.sin_addr.s_addr = inet_addr("156.59.20.50");

Binding the destination address


Rather than specify the destination address in each call, the destination address can be bound to the socket.
/* set up the server connection side */ server.sin_family = AF_INET; /* use TCP/IP */ server.sin_port = 0; /* use first available port */ server.sin_addr.s_addr = INADDR_ANY; if( bind( s, &server, sizeof(server) ) < 0 ) { perror("Error, socket not bound."); exit(3); }

Sending data to the socket connection


There are five possible system calls that an application program can use to send data to a socket. They are send(), sendto(), sendmsg(), write() and writev(). The following code fragment sends data to the port. char buf[32]; strcpy( buf, "Hello" ); sendto( s, buf, sizeof(buf)+1, 0, &server, sizeof(server));

Receiving data from the socket connection


The following code fragment receives data from the port.
char buf[32]; int s, client_address_size; struct sockaddr_in client, server; if( recvfrom( s, buf, sizeof(buf), 0, (struct sockaddr *) &client, &client_address_size) < 0 ) { perror("Error getting data from socket connection."); exit( 4 ); }

Closing the socket connection


When the application program is finished, the socket connection should be closed. close( s );

Example UDP Client Program


This client program establishes a socket connection and sends the message "Hello" to the server application.
/* rem invoke with a.out 156.59.20.100 portnum */ #include #include #include #include #include #include <string.h> <stdlib.h> <sys/types.h> <sys/socket.h> <netinet/in.h> <arpa/inet.h>

main( argc, argv) int argc; char **argv; { int s; unsigned short port; struct sockaddr_in server; char buf[32]; /* argv[1] is internet address of server argv[2] is port number Convert the port from ascii to integer and then from host byte order to network byte order using htons() */ port = htons( atoi( argv[2] )); /* create datagram socket using UDP */ printf("Creating datagram socket.\n"); s = socket(AF_INET, SOCK_DGRAM, 0); if( s == -1 ) printf("Socket was not created.\n"); else printf("Socket created successfully.\n"); /* set up the server name */ server.sin_family = AF_INET; server.sin_port = port; server.sin_addr.s_addr = inet_addr( argv[1] ); strcpy( buf, "Hello" ); printf("Sending data to the socket.\n"); sendto( s, buf, (strlen(buf)+1), 0, &server, sizeof(server ) ); printf("Data has been sent to the socket\n"); printf("Closing the socket connection.\n"); close(s); printf("Socket closed.\n"); }

Example UDP Server Program


This server program establishes a socket connection and receives data from a client application.
/* server program, run this first */ #include #include #include #include #include #include main() { int sockint, s, namelen, client_address_size; struct sockaddr_in client, server; char buf[32]; /* create datagram socket using UDP */ printf("Creating datagram socket.\n"); s = socket(AF_INET, SOCK_DGRAM, 0); if( s == -1 ) printf("Socket was not created.\n"); else printf("Socket created successfully.\n"); /* set up the server name */ server.sin_family = AF_INET; server.sin_port = 0; /* use first available port number */ server.sin_addr.s_addr = INADDR_ANY; if( bind(s, &server, sizeof( server )) < 0 ) { printf("Error binding server.\n"); exit(3); } /* find out what port was assigned */ namelen = sizeof( server ); if( getsockname( s, (struct sockaddr *) &server, &namelen) < 0 ) { perror("getsockname()\n"); exit(3); } printf("The assigned port is %d\n", ntohs( server.sin_port)); /* receive message on socket s in buf */ client_address_size = sizeof( client ); printf("Waiting for a message to arrive.\n"); if( recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &client, &client_address_size) < 0 ) { printf("recvfrom()\n"); exit(4); } /* print the message */ printf("Data has been sent to the socket\n"); printf("The message was\n"); <string.h> <stdlib.h> <sys/types.h> <sys/socket.h> <netinet/in.h> <arpa/inet.h>

printf("%s\n", buf ); printf("Closing the socket connection.\n"); close(s); printf("Socket closed.\n"); }

Compiling and running the example programs


Type the following commands. $ cd $HOME $ mkdir sockets $ cd sockets $ mkdir udp $ cd udp $ cp /etc/demo/socket/client1.c client1.c $ cp /etc/demo/socket/server1.c server1.c $ cc client1.c $ cp a.out client1.out $ cc server1.c $ cp a.out server1.out The command sequence first establishes a subdirectory where the examples and programs will be stored. The sample programs are then copied into this new directory. After compiling the client program, the executable file is copied to a.out because this will be lost when the server program is compiled. After typing all the commands about, the client application is stored in client1.out and the server application in server1.out The server application is started first. You will be required to have two telnet sessions running at the same time, one in which you can run the server application, the other in which you will run the client application.

Run the server program by typing the following command $ server1.out Write down the port number and TCP/IP address that the server application is using.

Das könnte Ihnen auch gefallen