Beruflich Dokumente
Kultur Dokumente
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.
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.
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"); }
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.