2
data = self.SOCKET.recv(16384)
users = data.decode().split('&')

print(users)

I am working on chat program. When I received a small amount of data (around 100 characters), I can see all data. But when I received more much data (around is 10000 characters), I can't see the complete data. I can see only some section. After that, when my friend received an amount of data of 10000 characters with other computer, he can see all data. I thought it is depend on ethernet and wifi. So my friend tried it with wifi. He again can receive all data. Is it depend computer? and should we go into receive buffer with hand? what are differences?

*100 and 10000 character are for example.

mrCarnivore
  • 4,638
  • 2
  • 12
  • 29
yellowpisagor
  • 146
  • 3
  • 13
  • `recv` has no idea how much data is expected, only how much is in the latest packet. It is up to the protocol how much to wait for. It may need to call `recv` several times to get all the data. – stark Feb 05 '18 at 14:52
  • 1
    Possible duplicate of [Python Socket Receive Large Amount of Data](https://stackoverflow.com/questions/17667903/python-socket-receive-large-amount-of-data) – 001 Feb 05 '18 at 14:53

2 Answers2

3

Use this code to receive the data:

while True:
    data = self.SOCKET.recv(512)
    if (len(data) < 1):
        break
    print(data.decode())

self.SOCKET.close() # close the socket connection

This should work. I cannot be more specific since I don't know the context of your program. Don't copy paste the code, but adjust accordingly. Get back with a feedback if it doesn't work.

MiniGunnR
  • 5,590
  • 8
  • 42
  • 66
  • 1
    Thanks for your helping. You gave me main idea. I can't use `len(data)<1` because my last data always can has be much character. If there isn't any data back, `print(len(data))` or `print(len(data.decode())) ` don't give any value. I understand my data is last by special term of append. – yellowpisagor Feb 05 '18 at 16:44
  • This fails if the sender doesn't send the next packet before the receiver calls the next recv. The protocol needs to be defined in a way that tells the receiver the number of bytes to wait for. – stark Feb 05 '18 at 18:20
  • @stark is correct. So instead of `len(data) < 1`, you need to identify another logic that will represent the end of the data transfer. – MiniGunnR Feb 05 '18 at 20:26
0

A very common solution would be to add a null byte at the end of whatever your sending. So:

send(msg.encode() + b'\x00')

on server and

char = data = b""
while char != b"\x00":
    char = recv(1)
    data = data + char
data = data.decode().strip("\x00")

Just spitballing though!