0

I have a client and server that communicate using TCP. I want to transfer data from the client to the server.

client:

import socket
V=''
for i in range(1,400)
    V=V+"%s %s %s" %s (i,i,i)

s = socket.socket(socket.AF_INT,socket.SOCK_STREAM)
s.connect((IP,PORT))
s.send(V)
s.close

and the server:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

while 1:
        connection, addr = s.accept() 
        V_recived = connection.recv(8192)

when I run these two scripts, I see that the size of V on the client computer is 4485 bytes and on the server computer the size of V_received changes between different runs: most of the time it is 1481 bytes and some time is is 4485 or less. For my understanding, the use of the TCP protocol supposed to guarantee that all my data will be transfer, therefore I dont understand what am I doing wrong...

Any ideas?

TomE8
  • 546
  • 1
  • 4
  • 14
  • TCP guarantees that the bytes stuffed into the stream at one end will fall out the other end in the same order and without loss or duplication. Expect nothing more, certainly not support for entities larger than a byte. – HABO Feb 22 '16 at 02:14

1 Answers1

2

You will get TCP data in chunks. The size of those chunks might depend on a lot, but you can never trust a package to get through in one part. You always need to see if there are more data incoming. As I am not very familiar with python, I don't know the exact implementation, but normally you would loop until you get 0 bytes and add up the packages you get while doing so.

Edit:

Try this:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)

connection, addr = s.accept() 
while 1:
    data = conn.recv(8192)
    if not data: break
    print "received data:", data

Found here: https://wiki.python.org/moin/TcpCommunication

Aslak Berby
  • 183
  • 1
  • 7
  • Hi, thanks for your answer. As far as I understand there is not a way to do so in Python, because it isn't needed. My understanding is based on this post [How to check if all data are received with a TCP Socket in Python](http://stackoverflow.com/questions/23732930/how-to-check-if-all-data-are-received-with-a-tcp-socket-in-python)... Any other ideas? – TomE8 Feb 21 '16 at 20:59
  • According to this: https://docs.python.org/3.0/library/socket.html it seems like the python `recv` is a more or less a wrapper around the OS function (It even refer to Unix manual page recv(2)). So, try to write a new `while` after accept. (I update my answer from here. https://wiki.python.org/moin/TcpCommunication ) – Aslak Berby Feb 21 '16 at 21:27
  • The answer you refer to even states that you are not guarantied to get n bytes and will have to do a loop until you get all data. There might be some libraries, like PHP's `file_get_contents`, but your example does not look like that. – Aslak Berby Feb 21 '16 at 21:39
  • Your example was the solution: my problem was that the line 'connection, addr = s.accept()' was inside the loop at the server script...thanks – TomE8 Feb 22 '16 at 08:08