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.

2
  • 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. Commented Feb 5, 2018 at 14:52
  • 1
    Possible duplicate of Python Socket Receive Large Amount of Data Commented Feb 5, 2018 at 14:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 is correct. So instead of len(data) < 1, you need to identify another logic that will represent the end of the data transfer.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.