1

私は基本的にチャットアプリを作ろうとしていますが、ここではサーバーからクライアントに何も送信できません。これを修正するにはどうすればよいですか? サーバープログラム:

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
4

1 に答える 1

2

接続ソケットを使用してオブジェクトを送受信できないため、問題はそこだけです....

使用する -

c.sendto("Received", addr) 

それ以外の

s.sendto("received", addr)

2番目の問題は、ソケットからメッセージを受信して​​いないことです...これが作業コードです

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()
于 2016-06-20T09:57:30.287 に答える