#TCP, server-side code, import socket ' ' first imports Socke module ' SK = Socket.socket () ' Create a Socket object, Def __init__ (self, family=af_inet , Type=sock_stream, Proto=0, Fileno=none): The default parameter means, Family=af_inet, the protocol family default IPv4, Type=sock_stream, the default is TCP, the latter two are not involved in the " ' Sk.bind ((' 127.0.0.1 ', 9999)] ' We are bound to our local address via socket this service, (Ip,port) as a server, you need to set up your own information so that others know how to connect you, Def bind (self, Address): # Real signature unknown;
Restored from __doc__ Bind the socket to a. ' Sk.listen () #等待别人来连接你 ' Def listen (self, backlog=none): # Real signature unknown;
Restored from __doc__ Enable a server to accept connections.
Backlog=none, not currently involved, but it should be about the number of connections? ' conn,addr = sk.accept () ' This function will stagnate here, wait for others to connect, and if the connection succeeds, it will return the connection information, Def accept (self): Accept ()-> (socket object, address info) a incoming connection.
Return a new socket representing the connection, and the address of the client. For IP sockets,The address info is a pair (hostaddr, port). Waiting to come to the connection, returns a (new socket, address information) on behalf of the new connection, ' Print (addr) conn.send (' Hello '. Encode (' Utf-8 ')) ' sends data in string form, returns the number of bytes sent, Def send (sel F, data, Flags=none): # Real signature unknown; Restored from __doc__ send (data[, flags])-> The count send a data string to the socket. For the optional Flags argument, this is the Unix manual. Return the number of bytes sent;
This could be less than Len (data) If the network is busy. ' ret = CONN.RECV (1024) #1024 to accept 1024 bytes ' def recv (self, buffersize, flags=none): # Real signature unknown; Restored from __doc__ recv (buffersize[, flags])-> data ' Print (Ret.decode (' Utf-8 ')) Conn.close () # Hang Power The words sk.close () # Off the phone ' here I's two sockets, respectively, 1. The socket used by the server for listening, 2. The newly established connection sockets require us to turn def close (self) on: # this Functio n should not reference any globals.
Issue #808164. self._closed = True If self._io_refs <= 0: #一个套接字可能涉及多个远方多个数据的收发, soThere are other connections to be judged, self._real_close () "
#TCP, client-side code
import socket
SK = Socket.socket ()
sk.connect (' 127.0.0.1 ', 9999)
'
def Connect (self, address): # Real signature unknown; Restored from __doc__
Connect (address) '
ret = SK.RECV (1024)
print (Ret.decode (' Utf-8 ')
) Sk.send (' You are also good '. Encode (' Utf-8 '))
Sk.close ()