0

I have a simple program designed to send raw text data over a TCP socket. I'm thinking this is a buffering problem; but I am not sure. No data appears to be sent.

from threading import Thread as thread
from sys import argv
from socket import socket
from tty import setraw
from sys import stdin
from sys import stdout
def rx(so):
    while True:
        stdout.write(so.recv(1).replace("\n", "\n\r"))
setraw(stdin.fileno())
so = socket()
so.connect((argv[1], int(argv[2])))
thread(target=rx, args=(so, )).start()
while True: so.send(stdin.read(1))

EDIT to clarify: Keyboard data is not being sent.

nope
  • 21
  • 3

1 Answers1

0
  1. Minor quibble:

    As user207421 said, "\n\r" is wrong. The correct DOS CR/LF sequence is '\r\n'.
    Who knows - maybe you don't need that "replace()" at all.

  2. First Step: verify you can send/receive anything:

    Q: Do you see data returned from your socket when you type a few characters, then hit "return"?

    Please confirm "Yes" or "No".

  3. Python socket: Try disabling Nagle:

    Try something like this:

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    
  4. Raw keyboard input:

    If you've verified steps 2 and 3 (you're able to send and receive data, and socket buffering isn't an issue), then you've isolated the problem to "stdin buffering".

    Look here for additional suggestions: unbuffered read from stdin in python

  5. Additional suggestion:

    For troubleshooting, can also try sending socket data from a different program: "telnet", "curl", a second Python script, etc. etc.

Please post back and let us know what you find!

paulsm4
  • 114,292
  • 17
  • 138
  • 190