eNN
eNN

Reputation: 173

Python3 server not responding to client request

I am setting up a basic python application that will listen for UDP packets at a specific port.

I am using an example code found online to begin to familiarize myself with UDP and socket connection.

When I invoke client.py and then server.py - the server does not respond and the terminal remains idle - any solutions for this problem? Below is the basic code I am working with

Client.py

import socket

UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789
Message = b"Hello, Server"

clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT_NO))

Server.py

import socket 
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789

serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))

while True:
#data, addr = serverSock.recvfrom(1024)
data, addr = serverSock.recvfrom(1024)
print ("Message: ", data)

Upvotes: 0

Views: 215

Answers (1)

John Zwinck
John Zwinck

Reputation: 249093

When I invoke client.py and then server.py

Well that's your problem--by invoking the client which sends, then later invoking the server which receives, you are preventing the two from communicating. The server needs to be running at the moment the client sends.

Upvotes: 1

Related Questions