UDP客户端到客户端,然后在python中与服务器通信

时间:2018-07-25 15:02:44

标签: python networking udp localhost client-server

我需要从该客户端向另一个相同的客户端发送一条消息,然后第二个客户端将该消息发送到服务器,并且输出的顺序与(从服务器到客户端2到客户端1)的顺序相同。即时通讯使用UDP,但无法将消息从client1发送到client2,因为client2 recv()函数会产生一个错误的错误参数。救命。 这是我的客户

import socket
import datetime
s=('',)
msg=''
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
address = ('127.0.0.1' , 5009)

def recv():
    data, addr = sock.recvfrom(1024)
    orgdata = data.decode('utf-8')
    print('Reply : ', orgdata)
    print('Address , Port : ', addr)
    now = datetime.datetime.now()
    print('Timestamp: ', now.strftime("%Y-%m-%d %H:%M:%S"))

def send(msg,addr):
    sock.sendto(msg,addr)

while s[0]!='stop' or msg!='stop':
    msg=input('Enter your message:')
    if(msg == 'stop'):
        print('Connection Terminated')
        break
    else:
        byt=msg.encode('utf-8')
        send(byt,address)
        #recv()

这是我的服务器。

import socket
import datetime
s=('',)
msg=''
info=''
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
address = ('127.0.0.1' , 5009)
sock.bind(address)
def recv():
    data, addr = sock.recvfrom(1024)
    orgdata = data.decode('utf-8')
    print('Reply : ', orgdata)
    print('Address , Port : ' , addr)
    now = datetime.datetime.now()
    print('Timestamp: ' , now.strftime("%Y-%m-%d %H:%M:%S"))
    return addr

def send(msg,addr):
    sock.sendto(byt,addr)

while s[0]!='stop' or msg!='stop':
    s=recv()
    msg=input('Enter your message:')
    if (msg == 'stop'):
        print('Connection Terminated')
        break
    else:
        byt=msg.encode('utf-8')
        send(byt,s)

我如何将消息从client1发送到client2,然后再将其发送到服务器,并按照服务器到client2的顺序将消息答复给client1

0 个答案:

没有答案