About this tutorial:
IP sockets are high-level Internet protocols to establish the bottom - all HTTP, SSL, POP3, Kerberos and UDP-Time interaction between the protocol are based on this. To control agreement, a programmer should have knowledge of the basic socket infrastructure. A similar API will appear in many different languages, this tutorial focuses C programming language, also used as a high-level language python representative.
This tutorial requires basic knowledge of C language knowledge, the best course, there are some pythond relevant knowledge. However, the two languages are not familiar to the reader to more in this tutorial, the skills of the up and down; most of the basic concepts of applications in different languages are similar, such as Ruby, Perl, TCK, etc. Of this tutorial introduces the IP network behind some of the basic concepts, readers will have if the original network protocols and protocol layers of knowledge, then, is for the good.
About the author:
David Mertz is a writer, a programmer, and teachers. He was always committed to improving the communication between him and the reader level. He welcomed all the readers can contact him: [email protected] .
Understanding of IP networks and network layers:
What is the network?
We usually say the computer network is composed of grass-roots network layer. Each layer in the layer of data provide different restrictions or guarantees. Each network layer protocol packet has its own format, header and layout. In fact the traditional structure was divided into seven groups: the upper and lower. Sockets interface provides a uniform lower API, and allows you to complete the application in the socket within the upper interaction.
Application formats application format (eg HTML, XML)
Layer5-7: session (session), present (that) & application (applications). (Eg SSL, HTTP)
Socket Interface (Socket Interface)
Layer4: Transport (Transport Layer) (TCP, UDP)
Layer3: Network (network layer) (eg IP)
Layer2: Data (data link layer) (eg Ethenet)
Layer1: Physical (physical layer) (eg Twisted pair)
In fact, you use all the network layer will use IP. This tutorial we are only concerned with IPV4, IPV6 of course, is very important in the future, but the principle is the same. In the transport layer, sockets support two specific transmission protocols: TCP (Transmission Control Protocol) and UDP (User Datagram Protocol).
Socket can not be used to access the underlying network layer; for example, a socket application will not know whether he is running in which the data link layer (Ethernet, Token Ring, etc.). Sockets emulation layer does not know a higher level network layer, such as whether the top is HTTP, NFS, FTP like (unless you write your own application for these specific socket).
Is, Sockets Network Programming Interface API is not the best choice. In particular, a number of excellent multi-language library can be used to program high-level agreements, do not worry about the details of sockets - Library to help you deal with any minor.
IP, TCP and UDP
In your network programming, you have the choice between TCP and UDP.
TCP is a stream-oriented connection, but UDP is packet-oriented data. In other words, TCP connection for the establishment of a continuous server and client on-line data transmission, in this mechanism, the entire connection period, the data byte order is guaranteed. TCP data and then write that there is no built-in structure, thus requiring high-level agreement must divide the boundaries between different data records.
UDP, the contrary, it is not required for the connection, he simply the client and server for data transfer. UDP is a good feature can be customized for each UDP defined, that is it they can know exactly where to start, where to end. UDP potential drawback is no guarantee that when the packet arrived, or is completely disordered. Based on UDP protocol on top of course, can also provide a mechanism to shake hands and confirmed.
Distinguish between UDP and TCP, simulation is a useful - phone calls will not be established unless the caller is the caller and the callee picks up the handset to answer calls. Call channel will be maintained until either party hangs up, and in the course of the conversation, both sides say how much love that the two sides time to hear things are orderly. Then prepared to receive not the same, do not like a telephone call to establish a communication channel, which no one could have predicted when the letter to which the letter first.
(Client, server-side), port, name and address
In addition to (TCP, UDP), there are two things (client and server side) needs to know each other: IP address and port. An IP address is a 32-bit size value, generally used for the points expressed in fractions, for example: 126.96.36.199. A port is a 16-bit values, usually with a range from 0 to 65535 digits. IP addresses can determine where a data packet to the sending machine, and port to determine which program (process) to be responsible for the start to send the packet.
The above description of the basic right, but missing something. Usually, it is hard to remember a IP address, on the contrary will be replaced with some domain name, such as the use of domain name 188.8.131.52: gnosis.cx instead.
Determine the host name:
In linux how to judge a host name? Nslookup can be symbolic name to obtain the host IP address. In fact, some tools such as ping or network configuration tool, you can do it. But doing the same work, it may be encoded in a way that even a simple point.
High-level language with a python or other ways to code to get the IP address, is a relatively trivial matter:
#! / Usr / bin / env python
"USAGE: nslookup.py <inet_address>"
Print (socket.gethostbyname (sys.argv ))
We use C language as gethostbyname () that simple.
$. / Nslookup.py gnosis.cx
Host name determination, continued
These are the python language of the nslookup command-line tool, the following is the command line C language nsloopup a simple example. Of course, C language will be more harsh than the python point.
/ * Bare nsloopup utility (w / minimal error checking) * /
# Include <stdio.h> / * stderr, stdout * /
# Include <netdb.h> / * hostent struct, gethostbyname () * /
# Include <arpa/inet.h> / * inet_ntoa () to format IP address * /
# Include <netinet/in.h> / * in_addr structure * /
Int main (int argc, char **)
Struct hostent * host; / * host information * /
Struct in_addr h_addr; / * internet address * /
If (argc! = 2)
fprintf (stderr, "USAGE: nslookup <inet_address> \ n");
If ((host = gethostbyname (argv )) = = NULL)
Fprintf (stderr, "(mini) nslookup failed on '% s' \ n", argv );
h_addr.s_addr = * ((unsigned long *) host-> h_addr_list );
fprintf (stdout, "% s \ n", inet_ntoa (h_addr));
Noting gethostbyname () return value is a description of the host name of the hostent structure. The host-> h_addr_list contains an address list, each address is 32-bit - byte order may not be the machinery original order, so in order to be able to change the dotted format, we need to use inet_ntoa () function.
---------------------------------- Using C to write a client process --------- ------------------------------------------
Write a socket client program steps
I write for the client and the server will use the example of the simplest procedures possible to write; the program to send and receive the same data. In fact, for debugging purposes, and many machines can run "echo server"; This facilitate our initial client, it can reach the server part of our previously used.
I am here for you recommend a book written with C TCP / IP sockets in the book, the book is written by Donahoo and Calver. I followed them a few more appropriate example. I recommend this book - it is clear, echo server / clients in most of the sockets programming should be studied earlier.
TCP and UDP based client-side programming is an obvious difference. In both cases, you had to first create the socket; but only in the TCP, you next need to create a connection to the server; Next we send data to the server; then Next to accept the return data; is likely a time send and receive will be in alternating; Finally, in the case of TCP, you need to close the connection.
A TCP echo client example (create ready)
This section mainly contains some header files.
# Include <stdio.h>
# Include <sys/socket.h>
# Include <arpa/inet.h>
# Include <stdlib.h>
# Include <string.h>
# Include <unistd.h>
# Include <netinet.h/in.h>
# Define BUFFSIZE 32
Void die (char * mess) (perror (mess); exit (1);)
Some do not have much set up to prepare the content, is primarily a specific buffer is created, the echo buffer is used to limit the amount of data transmitted. Also defines a small error handling function.
A TCP echo client (create socket)
Socket () function of the parameters of the decision to create what type of socket: PF_INET that the use of IP (usually); SOCKET_STREAM and IPPROTO_TCP with representatives of the TCP socket.
Int main (int argc, char * argv )
Struct sockaddr_in echoserver;
Char buffer [BUFFSIZE];
Unsigned int echolen;
Int received = 0;
If (argc! = 4)
Fprintf (stderr, "USAGE: TCPecho <server_ip> <word> <port> \ n");
If ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)
Die ("Failed to create socket");
Socket () function returns the value is a socket handle, similar to the file handle; particular, if the creation fails, return -1 instead of a positive number of handles.
A TCP client (connection)
Now that we have created a socket handle, we need to establish a connection and the server. The connection requires a socketaddr to describe the structural parameters to connect to the server. In particular, we need to identify the server and port can be used to determine echoserver.sin_addr and echoserver.sin_port. We actually use the IP address is determined by echoserver.sin_family, but this is usually set a value of AF_INET.
/ * Construct the server sockaddr_in structure * /
Memset (& echoserver, 0, sizeof (echoserver)); / * clear the structure 0 * /
Echoserver.sin_family = AF_INET / * internet / IP * /
Echoserver.sin_addr.s_addr_ = inet_addr (argv ); / * IP address * /
Echoserver.sin_port = tons (atoi (argv )); / * server port * /
/ * Connection * /
If (connect (sock, (struct sockaddr *) & echoserver, sizeof (echoserver)) <0)
Die ("Failed to connect with server");
If you create a connection fails, return -1. Otherwise, it will represent the socket is ready to send or receive data.
A TCP client (send / receive data)
Once a good connection, we to send / receive data ready. Send () call to include the following parameters: socket handle themselves, to send the character production, the character length of production, and a flag parameter. Normally, the flag is by default set to 0. Return value represents the number of bytes of data has been sent.
/ * Send word to the server * /
Echolen = strlen (argv );
If (send (sock, argv , echolen, 0)! = Echolen)
Die ("number of bytes sent does not match!");
/ * The data returned from the server * /
Fprintf (stdout, "Received:");
While (received <echolen)
Int bytes = 0;
If ((bytes = recv (sock, buffer, BUFFSIZE-1, 0)) <1)
Die ("Failed to receive bytes from server");
Received + = bytes;
Buffer [bytes] = "\ 0"; / * determine the capacity of the terminator character is null * /
Fprintf (stdout, buffer);
Recv () function does not guarantee a particular call can get all the data - it only got time to block out some of the data. Therefore, we adopted the loop to get and send us out as much information as we get each part of the data, we will write it to buffer. Obviously, different tools may come to an end there will be a different decision to accept the data, different methods (which may be the boundary between data streams).
A TCP client (summary)
We default call send () and recv () blocking mode, but there may be an option to change the socket to allow non-blocking manner. However, this tutorial will not cover the content of non-blocking mode, it will not cover, such as process, thread or universal asynchronous processing (based on non-blocking sockets) approach.
In the process the end, we will be closed socket, as we operate on a file handle.
Fprintf (stdout, "\ n");