linux IP socket programming (translation) unfinished, continued

2010-04-09  来源:本站原创  分类:Python  人气:248 

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)

Socket's work:

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.


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: 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 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: <inet_address>"

Import socket.sys

Print (socket.gethostbyname (sys.argv [1]))

We use C language as gethostbyname () that simple.

$. /

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");

Exit (1);


If ((host = gethostbyname (argv [1])) = = NULL)


Fprintf (stderr, "(mini) nslookup failed on '% s' \ n", argv [1]);

Exit (1);


h_addr.s_addr = * ((unsigned long *) host-> h_addr_list [0]);

fprintf (stdout, "% s \ n", inet_ntoa (h_addr));

exit (0);


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 [])


Int sock;

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");

Exit (1);


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 [1]); / * IP address * /

Echoserver.sin_port = tons (atoi (argv [3])); / * 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 [2]);

If (send (sock, argv [2], 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");

Close (sock);

  • linux IP socket programming (translation) unfinished, continued 2010-04-09

    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

  • linux IP socket programming (translation) 2010-04-09

    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

  • Java TCP / IP Socket Programming Notes (1) - the basic concept 2009-09-12

    Some concepts: Communication channel (communication channel): a sequence of bytes transmitted from one host to another host as a means may be wired cable, such as Ethernet (Ethernet), it may be wireless, such as WiFi, or other means connections. Info

  • Java TCP / IP Socket Programming Notes (2)-TCP example 2009-08-11

    1.InetAddress classes and SocketAddress used to identify network hosts TCP protocol client and server-side socket as Socket and ServerSocket UDP protocol client and server-side socket as a DatagramSocket 2. Class NetworkInterface said a name and assi

  • Java TCP / IP Socket Programming Notes (4) - send and receive data 2009-02-03

    1.TCP/IP agreement calls for information must be in the block (chunk) to send and receive, while the block length must be a multiple of 8 bits, therefore, we can assume that TCP / IP protocol to transmit the message that byte sequence. How to send an

  • 10. Network programming (TCP / IP.Socket programming. C / S) 2010-03-28

    1. What is the concept of network infrastructure, computer network: The distributed in different geographical regions of the external computer equipment and specialized communications lines interconnected into a large-scale, functionally efficient ne

  • Java TCP / IP Socket Programming Notes (3)-UDP example 2010-04-29

    1.UDP sockets and TCP sockets different. UDP sockets do not need to connect before use. TCP protocol and telephone communications, similar to the UDP protocol is similar to communicate with the e-mail: You Do not send packages or letters to "connect&

  • Linux Socket Programming Environment 2010-09-26

    What is Socket Socket interface is a TCP / IP network, API, Socket Interface defines a number of functions or routines that programmers can use them to develop TCP / IP network applications. To learn on the Internet TCP / IP network programming, we m

  • TCP / IP and Socket Programming 2010-10-14

    Switched network: Must be used to write Web applications Socket, this is the programmer know that. Moreover, the interview, we will ask the other party will not Socket programming? In general, many people would say, Socket programming is the basic li

  • c socket programming language guide 2010-09-29

    Socket Programming Introduction to depress you? From the man pages is difficult to get useful information? The times you want to compile Internet related program, but for you to call connect () before bind () the structure and know what to do? More .

  • Socket programming in Perl language 2011-09-05

    Network programming is a mysterious and complex art, of course, very interesting. Perl provides a rich language, TCP / IP network functions, all of these functions directly from C language socket library functions. Since Perl and C library functions

  • Udp socket programming in Java 2012-10-26 Udp socket programming in Java Datagram sockets UDP sockets can be used in java with the DatagramSocket class. Server Let code a simple udp server that listens on a certain port number. /** J

  • Three-step learn Java Socket Programming 2010-03-29

    The so-called socket usually referred to as the "socket", used to describe the IP address and port, is a communications chain handle. Application, usually through the "socket" to the network with a request or response network request.

  • ActionScript 3.0 Socket programming 2010-04-14

    Programming using ActionScript3.0 time to note the following issues: 1, and Socket server connection. 2, the server sends data to the Socket. 3, reading data from the Socket server. 4, shook hands with the Socket server and determine what kind of dat

  • php socket programming model through the smtp protocol 2010-05-24

    php socket programming model through the smtp protocol SMTP based on TCP / IP Internet protocol general command format is through a request / response to achieve, are used in text messages, so easier to deal with them. SMTP is short for Simple Mail T

  • Java Socket Programming example explanation 2010-06-04

    Understanding of network programming simple fact is that only two computers to communicate data. For programmers to master a programming interface and uses a programming model will seem relatively more simple. Java SDK provides some relatively simple

  • Socket Programming Guide and sample programs 2010-06-05

    1 Introduction In some common programming techniques, Socket Network programming can be described as the simplest. Socket programming and very little basic knowledge needed for beginners to learn network programming. Currently supports network transm

  • Transfer: python's socket programming 2010-06-23

    python socket programming Description: python write server steps: 1, the first step is to create a socket object. Call the socket constructor. Such as: socket = socket.socket (family, type) address family argument on behalf of the family, or for AF_I

  • ActionScript3.0 Socket Programming [Reserved] 2010-07-01

    We carried out using ActionScript3.0 Socket programming problem when the need to focus on the following, we will study one by one in the future on the following issues are discussed, and possible improvements in our program gradually. 1. And Socket s

  • Based on the UDP and TCP Socket Programming Introduction 2010-07-15

    Trackback URI I. Introduction TCP (Transmission Control Protocol) and UDP (User Datagram Protocol is a network architecture, TCP / IP transport layer a layer of two different commu