0

I've been having a program where whenever someone hit's enter, without typing something, the program will stop taking input.

#Client
import socket
from time import sleep
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = 'localhost'
port = 9990

s.connect((host, port))

print('connecting')

global ab

try:
    ab = input('enter input')
except Exception as eb:
    ab = 'hello'
s.sendall(ab.encode())

while True:
    global hi
    try:
        hi = input('enter input')
    except Exception as b:
        hi = input('enter input')

    try:
        dataNew = s.recv(4024)
        s.sendall(hi.encode())

    except Exception as be:
        print('try again')

    if dataNew:
        print(dataNew)

    if not dataNew:
        print('error')
        break

Server below:

#Server
import socket

host = 'localhost'
port = 9990

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except Exception as e:
    print("Error creating socket")

try:
    s.bind((host, port))
    print('Binding socket')
    s.listen(10)
    print('listening')
except Exception as e:
    print('Error creating server')  

def mainInput():
    while True:
        try:

            dataNew = c.recv(4024)
            c.sendall(dataNew)
            if not dataNew:

                print('waiting or disconnected')
                break

            if dataNew:
                print(dataNew, a)

        except Exception as b:
            print('error')

def mainLoop():
    while True:
        global c
        global a
        c, a = s.accept()
        print('Connected by', a)
        mainInput()

mainLoop()

The problem is that entering a blank message breaks the program.

bastelflp
  • 9,362
  • 7
  • 32
  • 67

1 Answers1

0

The main problem here is that you're calling input() assuming that you will get something back. But if the user just presses enter, that function returns an empty string (input strips the newline). An empty string has zero characters in it. If you write zero characters to the socket, nothing is sent (TCP is a stream-oriented protocol and doesn't respect "message" boundaries so a send of zero bytes is essentially a no-op [although -- if the connection is known to be in a closed or shutdown state -- an exception can still be generated]). So the client sends nothing, then tries to read the echoed response from the server. But the server never received anything, so the server is still waiting in recv and will never get to its own sendall. So now the client is also waiting (forever) in recv.

Other problems in the code:

If you detect an error in the server setup, you should quit rather than just printing a message and continuing, since the subsequent operations depend on the results of the preceding operations.

Likewise, if you do experience an exception in the server, you should break from the loop rather than continuing as you will typically just get exception after exception if you continue attempting to operate on a socket in a disconnected state.

If recv returns an empty string, that means the other side has closed (or shutdown) the connection, so you should immediately exit the loop, rather than attempting a sendall on the string, since (even though the string is empty, as noted above) this may generate an exception.

Gil Hamilton
  • 11,973
  • 28
  • 51