I have a raspberry running a server that communicates with a server on my laptop. I've done this through simple socket connections on python.
Like so:
server
HOST = "192.168.0.115"
PORT = 5001
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
while conn:
data = conn.recv(1024)
print data
if not data:
break
conn.close()
client
__init__(self,addr,port,timeout):
self.addr = addr
self.port = port
self.timeout = timeout
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.addr,self.port))
#in a different file the call to send is made, passes list object as a parameter
send(self,data):
if self.socket:
self.socket.send(str(data))
setting a print statement in the client code results in (x,y,z), which is the expected output. However, upon receiving the data on server it appears in a pattern:
(x,y,z)(x,y,z)
(x,y,z)
(x,y,z)
(x,y,z)(x,y,z)
...
Why is the data being received as duplicates? Is it a property of TCP? If so, how can I counter this to receive the data as one sent string as I had initially.
TCP sends data as a stream. You can write data to that stream, and you can receive data from that stream. The important thing is that while it can be the case that one send corresponds to one receive, that's not at all guaranteed to be the case. You can send a big chunk and receive it in a bunch of smaller chunks, or you can send a bunch of smaller chunks and receive a big chunk, or anything in-between. You're in the latter situation.
To solve this, you need to layer some sort of framing protocol on top of TCP. There's two primary ways you could do this:
Prefix each message with the length of the message and then read that many bytes.
Put a delimiter between each message.
For your purposes (sending plain text without newlines), the latter with a newline as a delimiter would probably be fine. You can probably figure out how to do that, but essentially, do this repeatedly:
If all you're doing is printing, you can replace all that by just looping, receiving from the socket then writing to stdout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With