Operating environment : Pycharm 2021.3.1/python 3.9

 TCPClient.py
from socket import * # TCP client 2022/02/26 # Define server IP And port number serverIP = '127.0.0.1'
serverPort = 997 # Create client socket clientSocket = socket(AF_INET, SOCK_STREAM) #
Create three handshakes with the server-side welcome socket TCP connect clientSocket.connect((serverIP, serverPort)) #
input message message = input('Please input your message : ') #
Connect socket communication with server and send message clientSocket.send(message.encode()) # receive modifiedMessage =
clientSocket.recv(1024) # Output on console print(modifiedMessage.decode()) # close
clientSocket.close()
TCPServer.py
from socket import * # TCP Server side 2022/02/26 # Defines the server welcome socket port number serverPort = 997 #
Create welcome socket serverSocket = socket(AF_INET, SOCK_STREAM) # Bind port number and welcome socket
serverSocket.bind(('', serverPort)) print('The server is ready to listen !') #
Welcome to wait and listen to the customer knocking serverSocket.listen(1) # Listen to at least 1 Enter the cycle when a customer while 1: # Create connection socket
connectionSocket, clientAddress = serverSocket.accept() print('The server is
ready to accept !') # receive message message = connectionSocket.recv(1024)
print('-------Received message: ' + message.decode() + '-------') # handle
modifiedMessage = message.upper() print(modifiedMessage.decode()) # send out
connectionSocket.send(modifiedMessage) print('------- I MADE IT ! -------') #
Close connection socket connectionSocket.close()
Operation results :

 

Technology
Daily Recommendation
views 267
views 232
views 231