SOCKET brief introduction

The client / server architecture

1. Hardware C / S architecture (printer)

2. Software C / S architecture (Internet everywhere is C / S structure): B / S architecture is C / S structure of one kind, B / S browser / server

Relations C / S architecture and socket: We use socket is to complete the C / S architecture development

OSI seven-layer protocol (*******)

Internet protocol according to different functions into seven or osi tcp / ip five or tcp / ip four

 

 Each run common physical device

 

 Introduction:

Notice a complete computer system is composed of hardware, operating system, application software composed of three, with the three conditions, one computer system can be finished with their own (such as playing a demining). If you're going to play with others , it would need access to the Internet, what is the Internet?

The core of the Internet that make up a protocol, the protocol is the standard, such as people around the world standard for communication is English

If the computer to a human, then Internet Protocol is the computer industry in English, all computers have learned to Internet protocol, that all the computers can go to send and receive messages in accordance with uniform rules standard to communicate, people according to the division different to Internet Protocol logically divided into separate levels.

See Network Communication Theory!

Why learn Internet Protocol socket must first learn it?

  First, C / S architecture is network-based communication

  Then the core network is a bunch of network protocol, which is the standard protocol. If you want to develop a software-based network communication, you must follow these standards.

 

 socket layer

 

 What socket that?

The socket is an application layer and the TCP / IP communication protocol middleware abstraction layer, which is a set of interfaces. In design mode, Socket is actually a facade pattern, it is the complexity of TCP / IP protocol hidden behind the socket interface, for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol . So, what I have no need to go in-depth understanding of tcp / udp protocol, socket has a good package for us, we just need to follow the provisions of the socket to program, write programs naturally follow tcp / udp standards.

Based on Socket TCP protocol

Sockets Category:

  File type socket family based: AF_UNIX (on Unix systems, all access file, the file system is the underlying socket file-based call to get the data, two sockets processes running simultaneously on the same machine, communication can be done by accessing the same files between systems)

  Based on the type of network sockets family: AF_INET (address family supports a wide variety in Python, but because we only care about network programming, so most of the time we only AF_INET)

Socket workflow:

Here we give an example to illustrate call:

If you give a friend a call, first dial telephone brought a friend after hearing the phone ring, then you and your friends established a link, you can speak. And other end of the exchange, hang up the end of the conversation. Life scenarios to explain this works. (If you go to a restaurant for dinner, where the owner is to assume the server, and you are the client, when you go to dinner, you must know the address of the restaurant, for yourself, the boss does not need your address .)

 

 note:

If you may encounter problems when restarting the server:

 

 This is because your server still exists time_wait four state waving in the occupied address (if not understand, please depth study 1.tcp three-way handshake, waving 2.syn flooding will have four highly concurrent server 3. The large number of time_wait state optimization method)

To solve the above-mentioned method:

1 # Add a socket configuration, ip and port reuse 
2 
. 3 Phone = socket (AF_INET, SOCK_STREAM)
 . 4 phone.setsockopt (SOL_SOCKET, the SO_REUSEADDR, 1) # is it added before the bind 
. 5 phone.bind (( ' 127.0.0.1 ' , 8080))

 

Guess you like

Origin www.cnblogs.com/s686zhou/p/11519570.html