Python 为什么这个服务器程序不能向客户端发送任何东西?
我基本上是在尝试制作一个聊天应用程序,但在这里我无法从服务器向客户端发送任何内容。我如何纠正这个问题? 服务器程序:Python 为什么这个服务器程序不能向客户端发送任何东西?,python,python-2.7,sockets,python-sockets,Python,Python 2.7,Sockets,Python Sockets,我基本上是在尝试制作一个聊天应用程序,但在这里我无法从服务器向客户端发送任何内容。我如何纠正这个问题? 服务器程序: from socket import * host=gethostname() port=7777 s=socket() s.bind((host, port)) s.listen(5) print "Server is Ready!" while True: c, addr= s.accept() print c print addr while
from socket import *
host=gethostname()
port=7777
s=socket()
s.bind((host, port))
s.listen(5)
print "Server is Ready!"
while True:
c, addr= s.accept()
print c
print addr
while True:
print c.recv(1024)
s.sendto("Received",addr)
s.close()
客户端程序:
from socket import *
host=gethostname()
port=7777
s=socket()
s.connect((host, port))
while True:
s.send(( raw_input()))
prin s.recv(1024)
s.close()
它在服务器程序中的s.sendto
处给了我一个错误,它说:
File "rserver.py", line 14, in <module>
s.sendto("Received",addr)
socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
文件“rserver.py”,第14行,在
s、 发送至(“已接收”,地址)
socket.error:[Errno 10057]不允许发送或接收数据的请求,因为套接字未连接,并且(使用sendto调用在数据报套接字上发送时)未提供地址
您无法使用连接套接字发送或接收对象,因此问题仅限于
使用-
c.sendto("Received", addr)
而不是
s.sendto("received", addr)
第二个问题是您没有从套接字接收消息。。。这是工作代码
server.py-
from socket import *
host=gethostname()
port=7777
s=socket()
s.bind((host, port))
s.listen(5)
print "Server is Ready!"
while True:
c, addr= s.accept()
print c
print addr
while True:
print c.recv(1024)
#using the client socket and make sure its inside the loop
c.sendto("Received", addr)
s.close()
client.py
from socket import *
host=gethostname()
port=7777
s=socket()
s.connect((host, port))
while True:
s.send(( raw_input()))
#receive the data
data = s.recv(1024)
if data:
print data
s.close()
您是否尝试使用接受返回的套接字?将s.sendto(“Received”,addr)更改为c.send(“Received”)。也不起作用。s.send(“Received”)套接字中第20行的文件“rserver.py”。错误:[Errno 10057]不允许发送或接收数据的请求,因为套接字未连接,并且(使用sendto调用在数据报套接字上发送时)未提供地址