Socket Quick Start manual

Source: Internet
Author: User
Tags ftp client

 


Introduction

When you enter the mysterious world of UNIX, you will immediately find more and more things hard to understand. For most people, the concept of BSD socket is one of them. This is a very short tutorial to explain what they are, how they work, and give some simple code to explain how to use them.


Analogy (what is socket ?)

Socket
Is the BSD Method for inter-program communication (IPC. This means that the socket is used to allow a process to communicate with other processes, just as we communicate with others by phone.

It is quite appropriate to use telephone to describe socket in the future.


Install your new phone number (how to listen ?)

To receive a call from someone else, first install a call. Similarly, you must first establish a socket to listen on the line. This process involves several steps. First, you need to create a new socket, just like installing a phone number first.socket()
Command.

Because sockets has several types, you need to specify the type you want to establish. You need to select the socket address format. Just as the phone has two forms of audio and pulse, socket has two most important options:Af_unix
AndIaf_inet
.Af_unix
Recognize sockets just like a Unix path name. This form is useful for IPC on the same machine. WhileAf_inet
The address format of the four decimal numbers separated by periods, such as 192.9.200.10. In addition to the machine address, you can also use the port
Number to allow multipleAf_inet
Socket. We will focus onAf_inet
Because it is very useful and widely used.

The other parameter you must provide is the socket type. Two important types are:Sock_stream
AndSock_dgram
.
Sock_stream
It indicates that the data passes through socket like a ghost stream. WhileSock_dgram
It indicates that the data is a datagram (Datagrams
. We will explainSock_stream
Sockets, which is common and easy to use.

After a socket is created, we need to provide the socket listening address. Just like you need a phone number to pick up your phone.bind()
Function.

Sock_stream sockets forms a queue for connection requests. If you are busy processing a connection, other connection requests will be waiting for the connection
Processing is completed.listen()
The function is used to set the maximum number of requests not rejected (generally five ).
Generally, it is best not to uselisten()
Function.

The following code describes how to usesocket()
,
bind()
Andlisten()
The function establishes a connection and can accept data.

 

/* Code to establish a socket; originally from bzs@bu-cs.bu.edu
*/

Int establish (unsigned short portnum)
{Char myname [maxhostname + 1];
Int S;
Struct sockaddr_in SA;
Struct hostent * HP;

Memset (& SA, 0, sizeof (struct sockaddr_in);/* clear our address */
Gethostname (myname, maxhostname);/* Who Are We? */
HP = gethostbyname (myname);/* Get our address info */
If (HP = NULL)/* We don't exist !? */
Return (-1 );
SA. sin_family = hp-> h_addrtype;/* this is our host address */
SA. sin_port = htons (portnum);/* this is our port number */
If (S = socket (af_inet, sock_stream, 0) <0)/* Create socket */
Return (-1 );
If (BIND (S, & SA, sizeof (struct sockaddr_in) <0 ){
Close (s );
Return (-1);/* Bind Address to socket */
}
Listen (s, 3);/* max # Of queued connects */
Return (s );
}

After the socket is created, you have to wait for the socket to be called.accept()
Function for this purpose. Callaccept()
It is like bringing up a call after the phone rings.Accept()
Returns a new socket to connect to the caller.

The following code demonstrates how to use it.

 

/* Wait for a connection to occur on a socket created with establish ()
*/
Int get_connection (int s)
{Int t;/* socket of connection */

If (t = accept (S, null, null) <0)/* accept connection if there is one */
Return (-1 );
Return (t );
}

Different from the phone number, you can accept the call when you process the previous connection. Therefore, fork is generally used to process each connection. The following code demonstrates how to useestablish()
Andget_connection()
To process multiple connections.

 

# Include <errno. h>/* obligatory includes */
# Include <signal. h>
# Include <stdio. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/socket. h>
# Include <sys/Wait. H>
# Include <netinet/in. h>
# Include <netdb. h>

# Define portnum 50000/* random port number, we need something */

Void fireman (void );
Void do_something (INT );

Main ()
{Int S, T;

If (S = Establish (portnum) <0) {/* plug in the phone */
Perror ("establish ");
Exit (1 );
}

Signal (sigchld, fireman);/* This eliminates zombies */

For (;) {/* loop for phone cballs */
If (t = get_connection (s) <0) {/* get a connection */
If (errno = eintr)/* eintr might happen on accept (),*/
Continue;/* try again */
Perror ("accept");/* bad */
Exit (1 );
}
Switch (Fork () {/* try to handle connection */
Case-1:/* bad news. Scream and die */
Perror ("fork ");
Close (s );
Close (t );
Exit (1 );
Case 0:/* We're the child, do something */
Close (s );
Do_something (t );
Exit (0 );
Default:/* We're the parent so look */
Close (t);/* another connection */
Continue;
}
}
}

/* As children die we shoshould get catch their returns or else we get
* Zombies, a bad thing. Fireman () catches falling children.
*/
Void fireman (void)
{
While (waitpid (-1, null, wnohang)> 0)
;
}

/* This is the function that plays with the socket. It will be called
* After getting a connection.
*/
Void do_something (int s)
{
/* Do your thing with the socket here
:
:
*/
}

Dialing (how to call socket)

Now you should know how to establish a socket to accept the call. So how to call it? Like a phone number, you must have a phone number first. Usesocket()
Function to do this, just like creating a socket for listening.

After giving the socket address, you can useconnect()
Function to connect the listening socket. Below is a piece of code.

 

Int call_socket (char * hostname, unsigned short portnum)
{Struct sockaddr_in SA;
Struct hostent * HP;
Int A, S;

If (HP = gethostbyname (hostname) = NULL) {/* do we know the host's */
Errno = econnrefused;/* address? */
Return (-1);/* No */
}

Memset (& SA, 0, sizeof (SA ));
Memcpy (char *) & SA. sin_addr, HP-> h_addr, HP-> h_length);/* Set address */
SA. sin_family = hp-> h_addrtype;
SA. sin_port = htons (u_short) portnum );

If (S = socket (HP-> h_addrtype, sock_stream, 0) <0)/* Get socket */
Return (-1 );
If (connect (S, & SA, sizeof SA) <0) {/* connect */
Close (s );
Return (-1 );
}
Return (s );
}

This function returns a socket that can flow through data.


Conversation (how to talk through sockets)

Now you have established a connection between the two parties that want to transmit the data.read()
Andwrite()
Function. In addition to the difference between socket reading and writing and file reading and writing, it is the same as processing common files. The difference is that you generally cannot get what you want
. So you need to keep repeating to the arrival of the data you need. A simple example: Read data to the cache.

 

Int read_data (int s,/* connected socket */
Char * Buf,/* pointer to the buffer */
Int N/* number of characters (bytes) We want */
)
{Int bcount;/* counts bytes read */
Int BR;/* bytes read this pass */

Bcount = 0;
BR = 0;
While (bcount <n) {/* loop until full buffer */
If (BR = read (S, Buf, N-bcount)> 0 ){
Bcount + = BR;/* increment byte counter */
BUF + = BR;/* Move buffer PTR for next read */
}
Else if (BR <0)/* signal an error to the caller */
Return (-1 );
}
Return (bcount );
}

You can write data for the same function.


Suspend (end)

Like when you talk to someone by phone, you need to close the connection between sockets. Averageclose()
The function is used to close socket connections on each side. If one side is disabled while the other side is writing data to it, an error code is returned.


World Language (the language of communication is very important)

Now you can contact the machine, but be careful with what you said. Many machines have their own dialects, such as ASCII and ebcdic. The more common problem is the byte order. Unless you have always transmitted text, you must pay attention to this problem. Fortunately, people have found a solution.

Long ago, people argued which order was more "correct ". Now it is necessary to have corresponding functions for conversion.
There arehtons()
,ntohs()
,htonl()
Andntohl()
. Before transmitting an integer data, convert it.

 

I = htonl (I );
Write_data (S, & I, sizeof (I ));

After reading the data, change it back.

 

Read_data (S, & I, sizeof (I ));
I = ntohl (I );

If you keep sticking to this habit, you will have fewer chances of making mistakes than others.


The future is under your control (next step ?)

With what we have discussed, you can write your own communication program. And treat all new things
It is best to see what others have done. There are many things about BSD socket for reference.

Please note that there is no error check in the example, which is very important in the "real" program. You should pay full attention to this.

 

 

 

The original meaning of socket is "hole" or "socket ". Here as 4bds
UNIX process communication mechanism. Socket is very similar to a telephone outlet. Take a national-level telephone network as an example. The two sides of a telephone call are equivalent to two processes that communicate with each other, and the area code is its
Network address. A vswitch in a region is equivalent to a host, and the local number assigned to each user by the host is equivalent to a socket number. Before a call, any user must possess a telephone, which is equivalent
Apply for a socket. At the same time, you must know the other party's number, which is equivalent to a fixed socket. Then call the other party by dialing, which is equivalent to sending a connection request (if the other party is not in the same region
To dial the target area code, it is equivalent to giving a network address ). If the other party is present and idle (equivalent to the communication of another host starting and accepting connection requests), pick up the phone microphone and both parties can formally call, equivalent to a connection
The connection is successful. The communication process between the two parties is the process of sending a signal to the phone and receiving a signal from the phone. It is equivalent to sending data to and receiving data from the socket. After the call
To suspend a telephone is equivalent to disabling socket and revoking the connection.


In the telephone system, the user can only feel the existence of the local phone number and the other party's phone number, establish the call process, voice transmission
The process and technical details of the entire telephone system are transparent to him, which is also very similar to the socket mechanism. Socket uses the inter-network communication facilities to implement process communication, but it specifies the details of the communication facilities.
Don't care, as long as the communication facilities can provide sufficient communication capabilities, it will satisfy.
So far, we have made an intuitive description of socket. Abstract: Socket essentially provides the end of Process Communication.
Point. Before processes communicate with each other, both parties must first create an endpoint. Otherwise, there is no way to establish a connection and communicate with each other. Just as before making a call, both parties must have their own telephones. Within the network
Each socket uses a semi-correlation description:
(Protocol, local address, local port)
A complete socket has a unique local socket number, which is allocated by the operating system.
Most importantly, socket
It is designed for customer/Server models and provides different Socket System calls for customers and server programs. The customer randomly applies for a socket
(Equivalent to a caller who wants to make a call on any incoming phone number), the system allocates a socket number for it. The server has a globally recognized socket
Any customer can send a connection request and information request to it (equivalent to a called phone with a phone number known to the caller ).
The socket uses the Client/Server mode to cleverly solve the problem of establishing communication connections between processes. Server
The semi-correlation of socket is recognized as very important globally. Readers may consider how to establish communication between two completely random user processes? Assume that neither party has a socket
Fixed, just like the two sides of a call do not know each other's phone number, it is impossible to call each other.

 

The socket interface is the most widely used method to access the Internet.
If you have a host with the TCP/IP protocol configured, the IP address is 202.120.127.201. At this time, execute FTP on another host or the same host.
202.120.127.201, it is clear that a connection cannot be established. Because the host "202.120.127.201" does not run FTP service software. Similarly,
Run browsing software on another host or on the same host
For example, if you enter "http: // 202.120.127.201" in Netscape, a connection cannot be established. Now, if you run an FTP service software on this host
Open a socket and bind it to port 21), and then run a web
Service software (the software will open another socket and bind it to port 80 ). In this way, execute the ftp
202.120.127.201, FTP client software will call the host through port 21
Establish a connection with the socket provided by the service software. When "http: // 202.120.127.201" is entered in Netscape, port 80 is used to call
Call the socket provided by web service software on the host, establish a connection with it, and talk to it.

There are many such hosts on the Internet. These Hosts generally run multiple service software and provide several services at the same time.
Each service opens a socket and binds it to a port. Different ports correspond to different services. Socket is like a porous socket, just as it was originally intended. A host is full
Each socket has a serial number in each socket room. Some sockets provide 220 v ac, some provide 110 v ac, and some provide cable TV programs.
The customer software inserts the plug into a socket with different numbers to obtain different services.

 

**************************************** **************************************** **************************************** ******

To write a network program, you must use socket. This is what programmers know.
Path. In addition, during the interview, we will also ask if the other party will be able to program the socket? In general, many people will say that socket programming is basically listen, accept and
Send, write, and other basic operations. Yes, just like common file operations, as long as they have been written.

 

For network programming, we must also call it TCP/IP.
The network protocol does not exist. For TCP/IP, we also know TCP and UDP. The former ensures data correctness and reliability, while the latter allows data loss. Finally, we also know that
Before connection, you must know the IP address and port number of the other party. In addition, ordinary programmers will not know too much, and many times this knowledge is enough. Most of them are processed by multiple threads when writing a service program.
Concurrent access.

 

We also know the following facts:

1. A specified port number cannot be shared by multiple programs. For example, if IIS occupies port 80, Apache cannot use port 80.

2. Many firewalls only allow data packets on specific ports to pass through.

3. After a service program accesses a connection request on a port of listen, a new socket is generated to process the request.

 

As a result, a problem that puzzled me for a long time emerged. If
After the socket is created and bound to port 80, does it mean that the socket occupies port 80? If so, the new
What port does the socket use? (I always thought the system would assign it an idle port number by default )? If it is an idle port, it must not be port 80.
The destination port of the data packet is not 80-the firewall will definitely organize it to pass! In fact, we can see that the firewall does not block such a connection, and this is the most common connection request and processing method.
Why is the firewall not blocking such a connection? How does it determine that the connection is generated because of port connet80? Is there any special mark in the TCP packet? Or
What does the firewall remember?

 

Later, I carefully studied the principles of the TCP/IP protocol stack,
I have a deeper understanding of many concepts. For example, TCP and UDP belong to the same transport layer and are jointly deployed on the IP layer (Network Layer. The IP layer is mainly responsible
End). The node here is a network device, such as a computer. Because the IP layer is only responsible for sending data to nodes, but cannot distinguish different applications above, TCP and UDP
The port information is added, and the port identifies an application on a node. In addition to adding port information, the UPD protocol basically does not process the IP layer data. TCP protocol
It also adds more complex transmission control, such as sliding data transmission window (slice)
Window), as well as receiving confirmation and re-sending mechanisms to achieve reliable data transmission. No matter what a stable TCP data stream is seen at the application layer, the following IP data packets are transmitted.
Data is reorganized by the TCP protocol.

 

Therefore, I have reason to suspect that the firewall does not have enough information to determine more information about TCP packets, except for IP addresses and port numbers. In addition, we can also see that the so-called port is used to distinguish different applications, so that it can be correctly forwarded when different IP packets arrive.

 

TCP/IP is just a protocol stack. Like the operating mechanism of the operating system, it must be implemented in detail and provide external operation interfaces. Just as the operating system provides standard programming interfaces, such as Win32 programming interfaces, TCP/IP must also provide programming interfaces externally. This is the socket programming interface!

 

In the socket programming interface, the designer puts forward a very important
The concept is socket. This socket is very similar to the file handle. In fact, in the BSD system, it is stored in the same process handle table as the file handle. This socket
It is actually a sequence number, indicating its position in the handle table. We have seen many of them, such as file handles and window handles. These handles actually represent some specific objects in the system.
Passed in as a parameter in various functions to operate on specific objects-this is actually a problem in C ++. In C ++, this handle is actually a this pointer, actually, it is an object pointer.

 

Now we know that socket and TCP/IP are not required.
However. The socket programming interface is designed to adapt to other network protocols. Therefore, the emergence of socket is only more convenient to use the TCP/IP protocol stack, its
Abstract TCP/IP to form several basic function interfaces. For example, create, listen, accept, connect, read, and write.

 

Now we understand that if a program creates
Socket and let it listen to port 80. In fact, it declares its possession of port 80 to the TCP/IP protocol stack. Later, all TCP packets destined for port 80 will be forwarded to this program (
Because the socket programming interface is used, it is first handled by the Socket Layer ). The so-called accept function abstracts the TCP connection establishment process.
The new socket returned by the accept function actually refers to the connection created this time. A connection includes two parts: the source IP address and the source port, and the source IP address and the target port. Institute
Therefore, accept can generate multiple different sockets, And the IP addresses and ports contained in these sockets remain unchanged. The only difference is the source IP address and source port. In this case
The socket host port can be 80, and the socket layer can accurately identify the ownership relationship between the IP packet and the socket based on the source/destination pair, so as to complete the operation on the TCP/IP protocol.
As encapsulation! At the same time, the rules for IP packet handling by the fire wall are clear and clear, and there are no complicated situations as previously imagined.

 

It is important to understand that socket is just an abstraction of TCP/IP stack operations, rather than a simple ing relationship!

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.