简体   繁体   中英

TCP socket programming using python in different computer systems

I am trying to run TCP client and server socket program using Python in different system(system A=windows7(python2.7) & system B=windows10(python 3.6)). server program is running in system B but when client is executed in system A it(client prg) terminates after few seconds displaying message:

ERROR 10060::connection failed because connected party didn't properly respond after a period of time or established connection failed because connected host has failed to response

CLIENT program for lower to uppercase

from socket import *
import socket
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(('192.168.43.130',9067))
sentence = input('Input lowercase sentence:')
clientSocket.send(sentence.encode())
modifiedSentence = clientSocket.recv(1024).decode()
print ('From Server:', modifiedSentence)
clientSocket.close()

SERVER PROGRAM lower to uppercase

from socket import *
import socket
serverSocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
serverSocket.bind(('',9067))
serverSocket.listen(1)
print ('The server is ready to receive')
while 1:
    connectionSocket,  addr = serverSocket.accept()
    sentence = connectionSocket.recv(1024).decode()
    print("String from client-->",sentence)
    capitalizedSentence = sentence.upper()
    print("String in server-->",capitalizedSentence)
    connectionSocket.send(capitalizedSentence.encode())
    print("-------------------------")
    connectionSocket.close()

TCP is a streaming protocol and you cannot assume you get the bytes you send in the same 'chunks' out of the receiving end. You'll need to decide on a framing solution. Often used approaches are using an agreed-upon termination character (such as \\0 or newline) or first sending the length of a message followed by exactly that number of bytes of message payload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM