BSD socket easy to get started manual

Source: Internet
Author: User
Tags manual socket port number

Introduced

As you enter the mysterious World of Unix, you will soon find that more and more things are hard to understand. For most people, the BSD socket

is one of the concepts. This is a short tutorial to explain what they are, how they work and give some simple code to explain how to use them.

Analogy (what is a socket?) )

A socket is a BSD method for inter-Program Communication (IPC). This means that the socket is used to exchange information between a process and other processes, just as we use the telephone to communicate with other people.

Using the telephone analogy is very appropriate, we will continue to use the concept of the telephone to describe the socket.

Put on your new phone (how to listen?) )

A person should be able to receive a call from someone, first of all, he has to put a phone. Again, you must first set up the socket to listen for the line. This process consists of several steps. First, you have to create a new socket, just like you put it on the phone first. The socket () command completes the work.

Because there are several types of sockets, you have to specify what type you want to build. You have to make a choice is the address format of the socket. Just as the phone has both audio and pulse forms, the socket

The two most important options are Af_unix and iaf_inet. Af_unix recognizes sockets just like a UNIX pathname. This form is useful for IPC on the same machine. Af_inet uses an address format of four decimal digits, such as 192.9.200.10, that is separated by dots. In addition to machine addresses, you can use the port number to allow multiple af_inetsocket on each machine. We will focus here on the af_inet approach because he is useful and widely used.

Another parameter you must provide is the type of socket. Two important types are sock_stream and sock_dgram. Sock_stream indicates that the data is passed through the socket like a character stream. Sock_dgram, however, indicates that the data will be in the form of a datagram (datagrams). We will explain SOCK_STREAM sockets, which is common and easy to use.

After establishing the socket, we will provide the address on which the socket is listening. It's like you need a phone number to answer the phone. Bind () function to handle this matter.

SOCK_STREAM sockets Let the connection request form a queue. If you are busy with a connection, other connection requests will wait until the connection is processed. The Listen () function is used to set the maximum number of requests that are not rejected (typically 5). It is generally best not to use the listen () function.

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.