0

I am currently trying to learn networking with python. I am really new to this topic so I replicated some examples from somewhere like here

I want to achieve a continous data transfer with TCP. This means I want to send data as long as some condition is met. So I slightly modified the example to this code below:

My Setup is Win10 with Python 3.8

My client.py copied and modified form above:

# Echo client program
import socket

HOST = '192.168.102.127'    # The remote host
PORT = 21         
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
i=0 #For counting how often the string was sent
while True: #for testing this is forever
    s.sendall(b'Hello, world')
    data = s.recv(1024)#
    print(i)
    i=i+1
    print('Received', repr(data))

My server.py:

# Echo server program
import socket

HOST = ''                 # Symbolic name meaning all available interfaces
PORT = 21             
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen(1)
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data: break
            conn.sendall(data)

The error I am getting is

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine after i=5460 (in multiple tries) on the Client side and

ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host on the Server side

The longer my text message is, less messages got sent before the error.

This leads me to believe I sent the data to some sort of buffer which is (over-)written to until the error is thrown. When looking for possible solutions I only found different implementations, which did not cover my problem or used other software.

As stated in some answers for similar questions, I disabled my firewall and stopped my antivirus, but with no noticable difference. When looking up the error, there is also the possibilty of protocol errors but I do not expect that to be a problem. When reading into the socket/TCP documentation, I found somewhere that TCP is not really designed for this kind of problem, but rather for

client connects to server
           |
           V
client sends request to server
           |
           V
server sends request answer
           |
           V
server closes connection.

Is this really true? But I cannot believe that for every data that is sent a new socket must be connected, like in this question. This solution is also really slow. But if this is the case, what could I use alternatively?

To illustrate the bigger picture:

I have a some other code which is giving me status data (text) at 500Hz. In Python, I am processing this data and sending the processed data to an Arduino with Ethernet shield. This data is "realtime" data, so I need the data sent to the arduino as fast as possible. Here the client is Python and the Server is the Arduino with the Ethernet module. The connection and everthing is working fine, only the continous sending of data is my problem.

schauma
  • 3
  • 2
  • 1
    The problem is most likely that your code assumes one end sending 1024 bytes means the other end will receive 1024 bytes. TCP does __not__ guarantee that, but it may happen for a while, or never, or occasionally or 99.9% of the time, or anywhere between those ranges. – DisappointedByUnaccountableMod Mar 18 '20 at 21:51
  • Make the server print a message when it hits the `break` - which is what is probably happening – DisappointedByUnaccountableMod Mar 18 '20 at 22:11
  • @barny I tried adding a print statement before the break but it was never hit when testing. Do you have any other ideas? – schauma Mar 18 '20 at 23:14
  • @barny Do you think his client or his server assumes that? Or both? I don't see where his code assumes that or where that would be an issue here. – David Schwartz Mar 18 '20 at 23:20
  • Are you really using port 21? Most platforms won't let you listen on port 21 without special permissions. Do you get the "Connected by" output from the server when you start the client? – David Schwartz Mar 18 '20 at 23:21
  • @barny after thinking about your comment, I am not sure what exactly you mean. Could you clarify it in simple terms please? – schauma Mar 18 '20 at 23:22
  • @DavidSchwartz Yes I did. – schauma Mar 18 '20 at 23:23
  • @schauma He's mistaken. Assuming you are running exactly the code you showed us, there's no such issue. The server just sends back exactly what it receives and doesn't care how data is framed. The client is always trying to receive more than it will be sent, so it won't overflow any buffers no matter how the responses get framed. The client always sends before it receives, so it can never wait forever. (Again, assuming you showed us the *exact* code you have the issue with. Very small changes could affect this. It's quite subtle.) – David Schwartz Mar 18 '20 at 23:25
  • You added the server side error message. Tried a different port? Tried running client on same machine as server? What do you see at the disconnection point when you trace network using e.g. wireshark? Which one produces the disconnection error first? – DisappointedByUnaccountableMod Mar 19 '20 at 08:00
  • I see the exact same error, running both on localhost - but change port to 10021 and it goes forever, or at least a lot lot longer hasn't failed yet. – DisappointedByUnaccountableMod Mar 19 '20 at 08:30
  • @barny Thanks for your help. I realised I was trying to send my data on the ftp port. I tried it with another port and it seems to work fine. It looks like I mixed these 2 up. Thanks again. – schauma Mar 19 '20 at 10:24
  • Smells like firewall network protection doing stateful packet inspection and concluding "hello world" isn't an FTP request (port 21). Seems to work fine on port 22, and 20, 80, .... – DisappointedByUnaccountableMod Mar 19 '20 at 10:25
  • No reason port 21 should be any different from port 22 if you don't have ftp/telnet servers running - maybe you should contribute to https://bugs.python.org/msg207497 - try disabling Base Filtering Engine service? – DisappointedByUnaccountableMod Mar 19 '20 at 10:58
  • Since my problem is now working, I am not sure whether I am willing to ponder around any more. Maybe on the weekend I will look into that. – schauma Mar 19 '20 at 13:03

0 Answers0