What socket that? What sockets are?

Network programming is to write a computer program to make two networked with each other to exchange data. That's all there yet? Yes! Network programming much simpler than you might expect.

So, what is it with data transmission between two computers? First, the need to physically connect. Today, most computers are already connected to the Internet, so do not worry about that.

On this basis, only we need to consider how to write data transfer program. But in fact this point do not worry, because the operating system has provided  socket . Even if the principle of network data transmission are not familiar with, we can socket programming.

What is a socket?

the socket means "socket", in the field of computer communications, socket is translated as "socket", which is a convention or one way communication between the computers. By this convention socket, a computer may receive data from other computers, and may also send data to other computers.

We will be able to plug into the socket to get electricity from the grid, the same, in order to transmit data to a remote computer, you need to connect to the Internet, while the socket is used to connect to the Internet-based tools.
 

What socket that?


Typical applications socket is Web servers and browsers: Browser obtain the URL entered by the user to initiate a request to the server, the server analyzes the received URL, the corresponding Web page content back to the browser, the browser and then after parsing and rendering it the text, pictures, video and other elements to the user.

Learning socket, which is learning how to communicate between computers, and write useful programs.

UNIX / Linux  what the socket that?

In UNIX / Linux systems, in order to unify the operation of the various hardware and simplify the interface, different hardware devices are also regarded as one file. Operations on the files, equivalent to the operation of an ordinary file on disk.

You may listen to many experts say, UNIX / Linux file of everything! The guy was right.

In order to express and differentiate file has been opened, UNIX / Linux will assign each file a ID, which is an integer, is called a file descriptor (File Descriptor). E.g:

  • Usually 0 to indicate the standard input file (stdin), which corresponds to the hardware device is a keyboard;
  • 1 generally represented by the standard output file (stdout), which corresponds to the hardware device is a display.


UNIX / Linux program is executed in any form of I / O operations, you are reading or writing a file descriptor. Only a file descriptor associated with a file and open an integer, which may be behind a normal file on the hard disk, FIFO, pipeline, terminal, keyboard, monitor, or even a network connection.

Please note that internet access is a file, it also has a file descriptor! You have to understand this sentence.

We can create a network connection via socket () function, or open a network file, socket () return value is the file descriptor. With a file descriptor, we can use common file manipulation functions to transmit data, for example:

  • Coming from a remote computer to read data Read ();
  • A write () to write data to the remote computer.


You see, as long as socket () creates a connection, the rest is a file operation, network programming but it was so simple!

What WIndow system socket that?

Windows has a similar "file descriptor" concept, but often referred to as "file handle." Therefore, this tutorial if it involves Windows platform will use the "handle", if it involves the use of the Linux platform "descriptors."

And UNIX / Linux is different, Windows will distinguish between socket and file, Windows put the socket connection is treated as a network, it is necessary to call a special function for data transmission socket designed for input and output functions common file is invalid.

Guess you like

Origin blog.csdn.net/Qsir/article/details/93737502