[英]Client Server Socket Programing in Python
I have the Client Server Socket program on python. 我在python上有Client Server Socket程序。 In both the Client and Server I use the loopback address. 在客户端和服务器中,我都使用环回地址。 But kindly assist how to use this code and apply on different Client Server machines Eg (Server IP 192.168.1.4 & Client IP 192.168.1.5) 但请协助如何使用此代码并将其应用于其他客户端服务器计算机(例如,服务器IP 192.168.1.4和客户端IP 192.168.1.5)
# Server program
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
UDPSock.bind(addr)
while 1:
data,addr = UDPSock.recvfrom(buf)
if not data:
print "Client has exited!"
break
else:
print "\nReceived message '", data,"'"
UDPSock.close()
# Client program
from socket import *
host = "localhost"
port = 21567
buf = 1024
addr = (host,port)
UDPSock = socket(AF_INET,SOCK_DGRAM)
def_msg = "===Enter message to send to server===";
print "\n",def_msg
while (1):
data = raw_input('>> ')
if not data:
break
else:
if(UDPSock.sendto(data,addr)):
print "Sending message '",data,"'....."
UDPSock.close()
Instead of 'localhost'
, use '192.168.1.5'
(the client's address) in the server code, '192.168.1.4'
(the server's address) in the client code. 在服务器代码中使用'192.168.1.5'
(客户端的地址), '192.168.1.4'
在客户端代码中使用'192.168.1.5'
(服务器的地址),而不是'localhost'
。
Normally a server wouldn't need to know the client's address beforehand, but UDP's knottier than TCP (the more usual, stream-oriented approach to socket communication) in many ways;-). 通常,服务器不需要事先知道客户端的地址,但是在许多方面,UDP的危害要大于TCP(套接字通信的更常见的,面向流的方法);-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.