Home > other >  socket_multy_test
socket_multy_test

Time:04-03

# - * - coding: utf-8 - * -
The import socket
The import threading
Print (" Hello world ")
If __name__=="__main__ ':
# 1. Create a TCP server socket object
The parameters of # socket () function description:
# AF_INET: protocol parameters, the family AF_INET determines the address of the socket type for ipv4 addresses (32 bit) and the combination of the port number (16)
: # SOCK_STREAM socket type parameters, SOCK_STREAM determines the socket to accept data format for stream (stream)
Tcp_server_socket=socket. The socket (socket. AF_INET, socket. SOCK_STREAM)
# set port multiplexing, let the server after the program exits, the port number the immediate release of
Tcp_server_socket. Setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, True)
# 2. To the program binding port
Tcp_server_socket. Bind ((10000) "192.168.3.51)
# 3. Set up to monitor
# 128 the maximum waiting for the number of connection is established
# after listen this socket is only responsible for receiving a client connection request, cannot send and receive messages, send and receive messages using the returned the new socket to complete
Tcp_server_socket. Listen (128)
# 4. Waiting for the client to establish a connection request, only the client and the server connection is established successfully code will block, the code to continue to perform the
# specially and client communication socket: service_client_socket
# the client's IP address and port number: ip_port
Service_client_socket, ip_port=tcp_server_socket. The accept ()
# code execution to this connection is established successfully
# print (" the client connected, ", ip_port)
# 5. Receive the client sends data, the maximum number of bytes of data received was 1024
Def RCV (service_client_socket) :
While True:
# 4. Waiting for the client to establish a connection request, only the client and the server connection is established successfully code will block, the code to continue to perform the
# specially and client communication socket: service_client_socket
# the client's IP address and port number: ip_port
Service_client_socket, ip_port=tcp_server_socket. The accept ()
Print (" the client connected: "+ STR (ip_port) +" \ n ")
While True:
T=service_client_socket. Recv (1024)
Msg_length=len (t)
# service_client_socket. Send (bytes (t, encoding="utf8"))
Print (STR (ip_port) + "& gt; & gt;" STR + STR (msg_length) + (t) + "\ n")
Msg_send=input (" reply "+ STR (ip_port) +" : ")
Service_client_socket. Send (bytes (msg_send, encoding="utf8"))
Trd0=threading. Thread (target=RCV, args=(service_client_socket,))
Trd1=threading. Thread (target=RCV, args=(service_client_socket,))
Trd2=threading. Thread (target=RCV, args=(service_client_socket,))
Trd3=threading. Thread (target=RCV, args=(service_client_socket,))
Trd4=threading. Thread (target=RCV, args=(service_client_socket,))
Trd5=threading. Thread (target=RCV, args=(service_client_socket,))
Trd6=threading. Thread (target=RCV, args=(service_client_socket,))
Trd7=threading. Thread (target=RCV, args=(service_client_socket,))
Trd0. Start ()
Trd1. Start ()
Trd2. Start ()
Trd3. Start ()
Trd4. Start ()
Trd5. Start ()
Trd6. Start ()
Trd7. Start ()
"'
While true:
T=input (" input message: ")
Service_client_socket. Send (bytes (t, encoding="utf8"))
If t=="exit" :
True=False
Tcp_server_socket. Close ()
"'
While True:
Pass
"'
While True:
Recv_data=https://bbs.csdn.net/topics/service_client_socket.recv (1024)
The length of the # to get the data
Recv_data_length=len (recv_data)
# print (" recv data lenght is: ", recv_data_length)
# for binary data decoding
# recv_content=recv_data. Decode (" GBK ")
# print (" receive the client's data as follows: ", recv_content)
Print (" client ", recv_data)
# ready to send data
# send_data="https://bbs.csdn.net/topics/ok". Encode (" GBK ")
Send_data=https://bbs.csdn.net/topics/bytes (" & gt; & gt;" And encoding="utf8") + recv_data + bytes (" \ n ", encoding="utf8")
# 6. Send the data to the client
Service_client_socket. Send (send_data)
# close the service with the client socket, termination and client communication service
If recv_data="close" : https://bbs.csdn.net/topics/=
Service_client_socket. Close ()
# 7. Close the server socket, termination request to establish the connection and the client to provide the service
Tcp_server_socket. Close ()
"'
Name=input (" input ")
Print (" you input ", name)
  • Related