First, the server's wording:
1. Create SOCKET Sockets: Network programming Interface socket (family = af_inet, type = Socket_strem,proto = 0, Fileno = None) provides a variety of socket family. Af_inet is the default family and requires binding IP and ports. 127.0.0.1 is a special IP address that represents the native address. If you bind to this address, the client must be running at the same time in order to connect, which means that the external computer cannot connect.
Different protocols family use different address forms, usually we use the af_inet-----address is stored in (ip,port) Form. In type types, we commonly use two types of socket_strem, streaming sockets, which represent connection-based TCP sockets, Socket_dgram, datagram sockets, and non-connection-based (UDP) interfaces. If not set, the default is Socket_strem.
2. Bind binding Usage: socket.bind (address) binds the socket to address, the address form is set according to family. Either client or server, the socket is created with a local file.
3. Listen Monitor Usage: socket.listen ([backlog]) enable socket to receive connection request, listen (Self,backlog = None), backlog needs to be greater than 0, specify the connection request that can be cached Number.
4. Accept the request connection waiting for an incoming connection, will return a new socket representing the connection, will return an address (host and port), can be received with two things, the previous one represents a new socket, the next is the receiving address.
Write the method: Connet_socket,client_addr = Srv.accept () Connet_socket is the new socket, and then connet_socket start the next transfer. Connet_socket,client_addr, the former represents the reception of the new socket, the latter is the address, specifically see the program 10th line.
5. How to receive data: Socket.recv (Bufsize[,flags]) receives data from the socket, returns the bytes, and is the received content. BUFSIZE Specifies the maximum number of data to receive at one time, and if no data is received, the program blocks until there is data or the remote terminal disconnects.
6. How to send data: Socket.send (bytes[, flags]) your socket must be connected to a remote socket, the return value is the number of sends, can determine whether your data is sent, if not, continue to send the remainder of the data
1 ImportSocket2hostname ='127.0.0.1' #Set host name3Port = 6666#set the port number to ensure that the port number is not used, you can view it in cmd4Addr =(Hostname,port)5SRV = Socket.socket ()#Create a socket6 srv.bind (addr)7Srv.listen (5)8 Print("waitting Connect")9 whileTrue:TenCONNECT_SOCKET,CLIENT_ADDR =srv.accept () One Print(CLIENT_ADDR) ARecevent = CONNECT_SOCKET.RECV (1024) - Print(Str (recevent,encoding='GBK')) -Connect_socket.send.send (Bytes ("Hello, data transfer is complete, here is Gaby-yan--server", encoding='GBK')) theConnect_socket.close ()
Second, the client's wording:
The client's wording is relatively simple, only 1. Create Socket 2. Establish a connection to connect 3. Sends a Send 4. Receive recv
This is due to the difference in their delivery programming framework.
1 ImportSocket2hostname ='127.0.0.1'3Port = 77774Addr =(Hostname,port)5 6Clientsock = Socket.socket ()## Create a socket7Clientsock.connect (addr)#Establish a connection8 9Say = input ("Enter the message you want to send:")TenClientsock.send (Bytes (say,encoding='GBK'))#Send Message OneRecvData = CLIENTSOCK.RECV (1024)#Receive Message RecvData is in bytes form. A Print(Str (recvdata,encoding='GBK'))#We can't read bytes, so we convert it to str. -Clientsock.close ()
Note: Run the code for the server before running the client code.
The TCP socket notation in Python