0

I start learning socket programming and I created a simple server that should replay everything I send to him back to me.
And its work work and I manage to telnet to it and write things. but I have two problems with my code:

  1. First, every sigh I hit on the keyboard get immediately send and I get it back. it doesn't wait until I press enter.

  2. Second, when I print to the client 'Welcome to the server. Type something and hit enter\n\n' the client see the cmd imput cursor in the middle of the new line, and not in the start of it.

here is part of my code:

#server in local host
class ClientConnection(threading.Thread):
    def __init__(self,conn):
        threading.Thread.__init__(self)
        self.conn = conn

    def run(self):
        clientthread(self.conn)

#create a new thread for each connection
def clientthread(conn):
    conn.send('Welcome to the server. Type something and hit enter\n\n'.encode())
    while True:
        data = conn.recv(2048).decode()
        replay = 'OK....' + data
        if not data:
            break
        conn.sendall(replay.encode())
    conn.close()

...  #socket get close in the end

How do I make the server response only if I the client press enter? I try to check if the recive = '\n' but it doesn't seem to work. I will appreciate any help and tips

edit: my second question answard simply

3
  • 1
    Every line should end with \r\n. Commented Dec 7, 2013 at 7:52
  • Thanks, you solve my secend question simply. Commented Dec 7, 2013 at 8:12
  • I think server should send something only after reading client's request. Try commenting out the line conn.send('Welcome to the server. Type something and hit enter\n\n'.encode()) and see if it works. Commented Dec 7, 2013 at 8:34

1 Answer 1

1

Yes, the client will often send you data as the user types rather then only when the user hits return. Thus, it is up to you to check if the data has a return, and only respond if it does.

data = ""
while True:
    chunk = conn.recv(2048).decode()
    if not data:
        break
    # try to separate the chunk by newline, to see if you got one.
    while True:
        split = chunk.split("\r\n", num=1)
        data += split[0]
        if len(split) == 1:
            break
        # Now we have a completed newline, so send the response
        replay = 'OK....' + data
        conn.sendall(replay.encode())
        data = ""
conn.close()
Sign up to request clarification or add additional context in comments.

2 Comments

Disclaimer: This code is untested and hacked up late at night, so think of it like pseudo-code instead of code and make sure you test and understand it carefully.
Thanks you help me a lot. I got the idea and did something similar, but not two whiles :-)

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.