0

Absolute newbie here, and I can't quite seem to find the answer to my question. Running python 2.7.

My code for the server is as follows:

#UDPPingerClient.py
from socket import *

#Create a UDP socket
clientSocket = socket(AF_INET, SOCK_DGRAM)
#Assign IP address and port number to socket
clientSocket.bind(("127.0.0.1",9501))

#Set a timeout value of 1 second
clientSocket.settimeout(1)

msg = "test"

#the server info
sIP = "127.0.0.1"
sPort = 12007
addr = (sIP,sPort)

a = 10

# the server will automatically drop some messages
# so we send 10 to make sure it gets there and then
# listen for a response from the server
while a > 0:
    clientSocket.sendto(msg,addr)
    try:
        received, server = clientSocket.recvfrom(1024)
        print received
    except timeout:
        print ('an error occured')

    a = a - 1

The server code:

# UDPPingerServer.py 
# We will need the following module to generate randomized lost packets 
import random 
from socket import * 

# Create a UDP socket 
# Notice the use of SOCK_DGRAM for UDP packets 
serverSocket = socket(AF_INET, SOCK_DGRAM) 
# Assign IP address and port number to socket 
serverSocket.bind(("127.0.0.1", 12007)) 

while True: 
    # Generate random number in the range of 0 to 10 
    rand = random.randint(0, 10) 
    # Receive the client packet along with the address it is coming from 
    message, address = serverSocket.recvfrom(1024) 
    # Capitalize the message from the client 
    message = message.upper()
    12 # If rand is less is than 4, we consider the packet lost and do not respond
    if rand < 4:
        continue 
    # Otherwise, the server responds 
    serverSocket.sendto(message, address)

Thus far I haven't been able to get a reply from the server. The most I've been able to accomplish is sending once and timing out before getting this error:

an error occured <-- output from exception

Traceback (most recent call last):
  File "C:/Python27/UDPPingerClient.py", line 23, in <module>
    received, server = clientSocket.recvfrom(1024)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

The reproducibility on this one is 100%, this is the outcome every time I run the server file and then the client file. Same thing with the firewall on or off. I have a feeling this has to do with the exception but I can't quite wrap my head around why.

cancub
  • 23
  • 2
  • 5
  • Works fine on my machine, both on Windows 8 and Linux 3.4. – phihag Oct 12 '13 at 22:10
  • Given that UDP is not connexion-oriented, the error message doesn't make sense. It may be that a problem occurs at a different level (e.g. in some arguably-broken firewall logic) and gets reported wrongly. – Armin Rigo Oct 13 '13 at 06:18

1 Answers1

0

this is the output i got:

foggy@dew ~ $ python UDPPingerClient.py 
TEST
TEST
an error occured
TEST
TEST
TEST
an error occured
TEST
an error occured
TEST

exactly ten messages, some have timeouted others were passed back. besides that extra 12 above rand line in Server (and that doesn't bother the interpreter) i don't see anything wrong with the code.

FoggyDew
  • 211
  • 1
  • 2
  • 9
  • try investigating on that error code further. it is probably network related issue, that drops your udp msgs. it's a linux here btw. – FoggyDew Oct 12 '13 at 22:15
  • Thanks, I guess this just kind of confirms that the error exists outside of python.[This guy](http://stackoverflow.com/questions/15734219/simple-python-udp-server-trouble-receiving-packets-from-clients-other-than-loca) had the same issue that I get running the code he has. – cancub Oct 12 '13 at 22:16
  • please try commenting out those two lines in server with `if rand: < 4 continue` since you're not seeding the prng, if your system's one is somehow borked that may be the cause for server to always drop the connection. – FoggyDew Oct 12 '13 at 23:40
  • Nah, it's not that. I've tried running the incredibly basic client-server connection that the other user talks about in the link in the above comment and I'm getting nothing on the receiving end. Just a straightforward sendto recvfrom and the packets are lost every time. – cancub Oct 13 '13 at 15:21