Question

I want the client program to wait for the server program to be running , before it sends out the message.

presently I have this code:

import socket
s=socket.accept()
s.connect(("192.168.1.34".8040))
f=open("tes.png","rb")
l=f.read(1024)
while(1):
         s.send(l)
         l.read(1024)
s.close()

Now if i run the client program without running the server program, i obviously get an error saying no route found.

How do I change this program in such a way that the client waits for the server to be running and then send the message.

Was it helpful?

Solution

Based on the traditional definition of client and server, the server has a publicly known address which is known to the client, but the client does not have a publicly known address which is known to the server.

The code you have shown appears to be a mixture of attempted client and server code, but the change you will need to make will be on the client side for the reason described above.

You can simply have the client perform the following steps.

  1. Attempt to connect to the server.
  2. If successful , then we are done and continue to talk to the server. If failed, wait for some timeout (maybe 1 second?) and then go back to step 1.

If your question is, "how do I get around the error that the client throws and try again", the answer is to use try-except.

Also, if the code you showed was supposed to be client code, the correct first line is s=socket.socket(), not s=socket.accept(), which will throw an error described by the comment.

For a simple example of working client-server code in Python, see my answer to another question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top