TCP protocol ip+ Port
The application layer protocol does things differently, but is inseparable from the exchange of data, which is essentially receiving and sending ~
Why do you have to shake hands three times?
The target machine may not exist, and the sender may be down during the handshake.
Client ———— Server, the first to send the request is the client, the client sends the information of the port is random. The port on the server side is fixed
First, the receiving end, the equivalent of the server
Receive port to request port number, service to start normal operation start listening to the port
Address Cluster socket families
It's a thing of the network layer.
Socket.af_unix UNIX native inter-process communication
Socket.af_inet IPV4
Socket.af_inet6 IPV6
A process and B processes want to communicate, without the case of a network.
A first dump,b and then load. Slow through the hard disk, the default in memory a process is not able to access the B process data, want to quickly communicate with the help of Af_unix, local launch a socket in the seven layer protocol to turn a circle
A process passed to B
Socket Types
Socket. Sock_stream #for TCP
Socket. Sock_dgram #for UDP
Socket. Sock_raw
#原始套接字, ordinary sockets cannot handle network messages such as ICMP, IGMP, etc., while Sock_raw can;
Second, Sock_raw can also handle special IPV4 messages, and with the original sockets, the IP header can be constructed by the user through the IP_HDRINCL socket option.
It's awesome. Can forge IP addresses, DDoS attacks, flood attacks, send a large number of requests resulting in service shutdown
A access B to three handshake, a first, after a to change their IP to send a request, B's response to a
Causes the B-end of the connection to appear pending, a and then change the IP address to send the request: Hang up again.
Socket. Sock_rdm
#是一种可靠的UDP形式, which guarantees the delivery of the datagram but does not guarantee the order. Sock_ram is used to provide low-level access to the original protocol,
Use when you need to perform certain special operations, such as sending ICMP packets. Sock_ram is typically used only by advanced users or by programs that are run by administrators.
Socket. Sock_seqpacket #废弃了
Example: Start the server listening port first, and then start the client:
#ServerImportSocketserver=socket.socket () Server.bind ("localhost", 7000))#just bind the port you want to listen toServer.listen ()#Listen, I'm going to listen to this port.Print("I started listening on port 7000---") conn,addr= Server.accept ()#wait for the phone, Conn is the connection instance, addr is the source IP address randomPrint("here's the request.")#data = SERVER.RECV (1024x768) #接收1024#Another phone call to switch, Server.switch () to know the order, you can give each request mark is the above Conndata = CONN.RECV (1024)Print("recv:", data) Conn.send (Data.upper ()) Server.close ()" "output: I started listening on port 7000---request came recv:b ' HelloWorld '" "
#ClientImportsocketclient=Socket.socket ()#declares the socket type and simultaneously generates the socket object#defining the protocol type by default is Family=af_inet, Type=sock_streamClient.connect (("localhost", 7000)) Client.send (b"HelloWorld") Data= CLIENT.RECV (1024)Print("recv:", data) client.close ()#output: recv:b ' HELLOWORLD '
Python,socket programming