实际上我正在尝试在两个系统中实现客户端和服务器之间的双向数据传输。我可以从客户端发送到服务器,但反过来是不可能的。不同系统之间是否可以进行双向通信?
我的客户端程序:
import sys
from socket import socket, AF_INET, SOCK_DGRAM
import time
SERVER_IP = '172.16.142.29'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port{1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))
while True:
mySocket.send('cool')
time.sleep(.5)
(msg, addr) =mySocket.recvfrom(1234)
print(msg)
sys.exit()
我的服务器程序:
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname('0.0.0.0')
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
while True:
(data,addr) = mySocket.recvfrom(SIZE)
print data
mySocket.send("hai")
sys.ext()
提前致谢。
答案 0 :(得分:0)
客户计划:
import sys
from socket import socket, AF_INET, SOCK_DGRAM
import time
SERVER_IP = '172.16.142.29'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port{1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect((SERVER_IP,PORT_NUMBER))
while True:
mySocket.send('cool')
time.sleep(.5)
(msg, addr) =mySocket.recvfrom(SIZE)
print(msg)
sys.exit()
服务器程序:
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname('0.0.0.0')
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
while True:
(data,addr) = mySocket.recvfrom(SIZE)
print data
mySocket.sendto("hai",addr)
sys.ext()