1 Replies - 10695 Views - Last Post: 06 October 2012 - 05:00 AM Rate Topic: -----

#1 sergio55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 28-September 12

Simple UDP client/server - Client program problem

Posted 28 September 2012 - 09:25 AM

Hello, I'm making simple client and server programs. The client just sends the message "HELLO" to the server and the server just sends back the time since epoch. However, the problem I'm having is how to setup my client so that it stops sending the same msg after it gets a response, while on the other hand, send another msg only if it doesn't get a response from the server. I'm very new to socket programming and Python so I apologize if my code is horrible x.x...

As of right now, the client continuously sends a msg and spams the server.

Server:
import socket
import time
server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)	
server_socket.bind(('', 6666)) 				code

print "UDPServer Waiting for client on port 6666"

while True:
	dataFromClient, address = server_socket.recvfrom(256)
	if dataFromClient == "HELLO":
		epochTime = str(time.time())
		print epochTime
		server_socket.sendto(epochTime, address)
	else:
		print "( " ,address[0], " " , address[1] , " ) said : ", dataFromClient



Client:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)	#This creates socket

while 1:
	data="HELLO"
	client_socket.sendto(data, ("localhost",6666))
	print "Sending request"
	recv_data, addr = client_socket.recvfrom(256)
	print recv_data
	
client_socket.close()



Is This A Good Question/Topic? 0
  • +

Replies To: Simple UDP client/server - Client program problem

#2 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: Simple UDP client/server - Client program problem

Posted 06 October 2012 - 05:00 AM

Hi,
It looks to me that your client is set to just repeat one task, which is to send the message 'hello'. Even though you are closing your connection on line 11, your client will never hit that point because you have used the loop while 1:. This means that your loop will go on forever, thus your problem. You may want to try using an if statement so that this only happens once. Something like:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)	#This creates socket
done = False
running = True #good for testing whether your sending loop is running or not in more complex stuff
while running:
        if done == False:
	    data="HELLO"
            done = True
        else:
            running = False
            break #get out of the loop so that you can end the socket.

       client_socket.sendto(data, ("localhost",6666))
       print "Sending request"
       recv_data, addr = client_socket.recvfrom(256)
       print recv_data
	
client_socket.close()


I didn't test this code, but something along those line should be what you are looking for.
Onto your other question about sending a something to the server if it doesn't get a response, you will probably need to use threads for this task. If I were coding something like this, I would have the socket.listen() function running and a timer thread which will tell the program when a response has not been received for a period of time. The program would then send a message to the server.
Also, if you haven't already, I highly recommend reading through this tutorial. I found this when I was learning to use sockets and I think it really helped me to get the fundamentals of a server/client down.

Good luck, if you have any other questions let us know!

This post has been edited by Eric115: 06 October 2012 - 05:01 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1