2

I have read several different SO posts on using python as a TCP client and am not able to connect to a server sending data via TCP at 1hz which is hosted locally. The connection parameters I am using are:

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname("127.0.0.1")
port=1234
address=(ip,port)
client.connect(address)
while True:
    print("test1")
    data = client.recv(1024)
    print("test2")
    print(data)

I believe that it is failing on the second line of the while statement but do not know why because it hangs and I am not given an error. Below are links to the SO articles, I have read and I have attached a screenshot from a TCP client tool that I am able to connect to the data server with. I'm expecting the data to stream in my print statement, is this not how it works? Whats the best way to make a persistent connection to a TCP connection with python?

Researched: (Very) basic Python client socket example,Python continuous TCP connection,Python stream data over TCP

enter image description here

risail
  • 509
  • 5
  • 14
  • 37
  • Your client program is not connecting to the server in the first place, i.e. you are missing a `client.connect(address)`. And without connecting you cannot receive data. – Steffen Ullrich Jan 22 '18 at 21:58
  • missed that line in my copy and paste...Connection parameter should have been in the OP – risail Jan 22 '18 at 22:01
  • Your client works almost perfectly when I run it. Perhaps the problem is with your server? Try testing your client against another server. I used netcat. In one window run `nc -l 1234`. In another window run your client. By "almost perfectly," I mean that it entered an infinite loop after the connection closed. If `recv` ever returns `''`, you should exit the loop. – Robᵩ Jan 22 '18 at 22:05
  • I think you are correct that my server is the problem I just rebooted my machine and it works, I need to find out what is happening/wrong with the ports. – risail Jan 22 '18 at 22:07

1 Answers1

3

Working with sockets: In order to communicate over a socket, you have to open a connection to an existing socket (a "client"), or create an open socket that waits for a connection (a "server"). In your code, you haven't done either, so recv() is waiting for data that will never arrive.

The simple case is connecting as a client to a server which is waiting/listening for connections. In your case, assuming that there is a server on your machine listening on port 1234, you simply need to add a connect() call.

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname("127.0.0.1")
port=1234
address=(ip,port)
client.connect(address)  ## <--Add this line.
while True:
    print("test1")
    data = client.recv(1024)
    print("test2")
    print(data)
bill.lee
  • 2,207
  • 1
  • 20
  • 26
  • sorry had that line in my code missed copying it when I posted the original question. – risail Jan 22 '18 at 22:02
  • Is the "sending" port already open, ready to send the text you want to print? If not, then I'll augment my answer to describe how a server needs to be implemented. – bill.lee Jan 22 '18 at 22:13