我刚刚开始使用Python和Python进行编码网络对我来说是一个全新的话题!我无法找出问题所在,我说,我已经尝试了所提供的所有解决方案用户!
请帮我这个代码!
服务器代码:
from socket import *
import time
HostName = ''
PortNum = 2015
Buff = 5000
ServAdd = (HostName, PortNum)
ServSoc = socket(AF_INET, SOCK_STREAM)
ServSoc.bind(ServAdd)
ServSoc.listen(5)
while 1:
CliAdd = ServSoc.accept()
TempSoc = ServSoc.accept()
while 1:
CliData = TempSoc.recv(Buff)
print CliData
time.sleep(20)
TempSoc.send('Hai')
TempSoc.close()
ServSoc.close()
客户代码:
from socket import *
import time
HostName = 'localhost'
PortNum = 2015
Buff = 5000
ServAdd = (HostName, PortNum)
CliSoc = socket(AF_INET, SOCK_STREAM)
CliSoc.connect(ServAdd)
while 1:
Data = raw_input('Enter Data to sent to the Server: ')
CliSoc.send(Data)
ServData = CliSoc.recv(Buff)
print ServData
time.sleep(10)
CliSoc.close()
错误讯息是
Enter Data to sent to the Server: hello
Traceback (most recent call last):
File "C:/Python26/Progs/TCPClient.py", line 15, in <module>
ServData = CliSoc.recv(Buff)
error: [Errno 10053] An established connection was aborted by the software in your host machine
答案 0 :(得分:0)
尝试使用以下服务器代码。您的客户端似乎没问题。您必须先运行server.py来侦听相关端口。然后运行你的client.py。你在服务器端做的是创建一个侦听套接字(在你的情况下是&#34; ServSoc&#34;)并接受连接。连接不是套接字对象,因此使用close()
函数没有意义。以下服务器代码接受循环中的连接并检查客户端连接。因此,当客户端断开连接时它不会崩溃。
服务器代码:
from socket import *
import time
HostName = ''
PortNum = 2015
Buff = 5000
ServAdd = (HostName, PortNum)
ServSoc = socket(AF_INET, SOCK_STREAM)
ServSoc.bind(ServAdd)
ServSoc.listen(5)
while 1:
conn, addr = ServSoc.accept()
if conn:
print "connection established with client:",addr
CliData = conn.recv(Buff)
print CliData
time.sleep(1)
conn.send('Hai')
ServSoc.close()