Home > Enterprise >  How to send data in a looping with socket PYTHON
How to send data in a looping with socket PYTHON

Time:04-29

i need to send a data each every X seconds from a client to a server with SOCKET, so i try this code:

for i in range(100):
  localip = '127.0.0.1'
  port = 5010
  bufferSize = 1024
  client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
  client.connect((localip, port))

  pacote = '2B4F08FE0F3B'
  client.send(bytes(pacote, "utf-8"))

  client.close()

  time.sleep(10)

But in the serv i recive a package with nothing (' '). I need send 100x the same message to the serv, each 10 seconds. Somebody knows where is the mistake?

I have other Client.py where i put manually the data (with the input() ) and he works perfect, but now i need to do this automatically.

The client need to open a new connection to send each data.

CodePudding user response:

But in the serv i recive a package with nothing (' ').

This is because the socket gets closed after sending the message, i.e. client.close().

I need send 100x the same message to the serv, each 10 seconds. Somebody knows where is the mistake?

If the server expects the client to create a single connection and then send many data over this connection before closing it, then the client should do exactly this. Instead your client opens a new connection for each new message and then closes the connection immediately after the message.

This means either you must fix the server to match the clients behavior or fix the client to match the servers behavior.

  • Related