Python Socket Not Receiving Without Sending

Python socket not receiving without sending

TCP sockets are streams of data. There is no one-to-one correlation between send calls on one side and receive calls on the other. There is a higher level correlation based on the protocol you implement. In the original code, the rule was that the server would send exactly what it received until the client closed the incoming side of the connection. Then the server closed the socket.

With your change, the rules changed. Now the server keeps receiving and discarding data until the client closes the incoming side of the connection. Then the server sends "ok" and closes the socket.

A client using the first rule hangs because its expecting data before it closes the socket. If it wants to work with this new server rule, it has to close its outgoing side of the socket to tell the server its done, and then it can get the return data.

I've updated the client and server to shutdown parts of the connection and also have the client do multiple recv's in case the incoming data is fragmented. Less complete implementations seem to work for small payloads because you are unlikely to get fragmentation, but break horribly in real production code.

server

import socket

HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)

conn, addr = s.accept()
print('Connected by', addr)

while True:
data = conn.recv(1024)
if not data: break
conn.sendall(b'ok')
conn.shutdown(socket.SHUT_WR)
conn.close()

client

import socket

HOST = 'localhost'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
s.shutdown(socket.SHUT_WR)
data = b''
while True:
buf = s.recv(1024)
if not buf:
break
data += buf
s.close()
print('Received', repr(data))

Python Socket is not receiving messages sent to it

modify this:

                    else:
host1 = addr[0]
port1 = addr[1]
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s1:
s1.connect((host1, port1))
with open(data["name"], 'r') as userfile:
data1 = userfile.read()
s1.sendall(bytes(base64.b64encode(bytes(data1,'utf-8')),'utf-8'))
s1.close

into this:

                else:
with open(data["name"], 'r') as userfile:
data1 = userfile.read()
conn.sendall(bytes(base64.b64encode(bytes(data1,'utf-8')),'utf-8'))
conn.close

you already have a socket connected to that host and port no need to create others (also because i can see that HOST is equal to host1)

python socket, client not receiving data in order

information_message = client.recv(1024).decode('ascii')
while True:
if information_message == 'PASSWORD':
client.send(password.encode('ascii'))
print(information_message)
if information_message == 'USERNAME':
client.send(username.encode('ascii'))
print(information_message)
break

The last break statement causes your client to exit the loop immediately after the first information_message was processed, i.e. after PASSWORD. At the moment the server writes USERNAME to the socket, this socket is thus already closed by the client. While the send might still succeed (it might also fail with Broken Pipe already) the following recv will fail since the socket is closed.

What you likely want to do is something like this:

while True:
try:
information_message = client.recv(1024).decode('ascii')
except:
break

if information_message == 'PASSWORD':
client.send(password.encode('ascii'))
print(information_message)
if information_message == 'USERNAME':
client.send(username.encode('ascii'))
print(information_message)

Python Socket Programming client not receiving all packets

Your code is discarding some incoming messages because you are calling recv() without using what it returns.

I would rewrite this:

while True:
if(c.recv(1024)!=None):
msg = c.recv(1024).decode()
c2.send(bytes(msg,"utf-8"))

as:

while True:
msg = c.recv(1024)
if(msg != None):
msg = msg.decode()
c2.send(bytes(msg, "utf-8"))

Python UDP Server Not Receiving Data

SOLUTION:
Looks like there was a firewall issue preventing the port from being monitored. The following were the commands that I took to add the port:

--Adds the port to the firewall
firewall-cmd --permanent --add-port=7800/udp
--restart the firewall service for the port to take effect
systemctl restart firewalld

Python: Socket not reading byte input

First, check the return values of send and recv. There is no guarantee all bytes were sent (try sendall) and and recv returns 0 (socket closed) or 1-length bytes requested, not necessarily the total number requested.

As far as your main bug, you've sent the character zero ('0') and your server receives it as the length with:

msg_length = conn.recv(HEADER).decode(FORMAT)

The result is a length of 0. msg then becomes an empty string with:

msg = conn.recv(msg_length).decode(FORMAT)

msg.encode() == b'' (an empty string) not b'0' a length 1 string containing a zero character.

Instead, you could abort when you get a length of 0. Or the normal way to disconnect is to close the connection. Then the server receives b'' (a length zero recv) which indicates a closed connection.



Related Topics



Leave a reply



Submit