1

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()
0

1 Answer 1

3

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;-).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.