Reputation: 169
I have the Client Server Socket program on python. 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)
# 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()
Upvotes: 1
Views: 1201
Reputation: 881635
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.
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;-).
Upvotes: 3