1

I have a simple client that sends data to a server, and a simple server that takes the data from the client and just sends it back to the client, and so the client receives it again from the server and prints it (look the code below). But when I don't write anything at the client, when I only press ENTER and nothing else at all, the server seems to freeze and the commands below

var0 = connectionSocket.recv(1024)

don't get executed at all.

EDIT: Let me rephrase the question, I don't want to make any change at the client's code. But only write/change the server's code that way that when nothing is written by the client but a "" is sent to the server, I don't want the server to stop working. Is this even possible and if yes, how?

I have this code for the server:

from socket import *
serverPort = 11000
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(('127.0.0.1', serverPort))
serverSocket.listen(10)
while 1:
    connectionSocket, addr = serverSocket.accept()
    var0 = connectionSocket.recv(1024)
    var0 = str(var0)
    if(var0==""):
        print("THIS COMMAND HERE IS WHAT I WANT TO BE EXCECUTED, BUT IT NEVER DOES")
    connectionSocket.send(str.encode(var0))
    connectionSocket.close()

And this code for the client:

import socket
serverName = "localhost"
serverPort = 11000
while 1:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((serverName, serverPort))
    var = input("Write a sentence: ")
    if(var==""):
        print("ODD ISN'T IT?")
    s.sendall(str.encode(var))
    data = s.recv(1024)
    data = str(data)
    print(data)
    s.close()
Albos Hajdari
  • 113
  • 1
  • 12
  • Did you check that you don't get a linefeed on the server side? It would not equal to an empty string even through there's nothing human-readable. – JussiV Apr 09 '18 at 11:49
  • you can easily remove the var0=str(var0) it still wont work. I think the problem is at the var0 = connectionSocket.recv(1024) , it just doesn't receive anything at all and neither it lets continue further – Albos Hajdari Apr 09 '18 at 11:52
  • See https://stackoverflow.com/questions/3363395/how-to-receive-a-socket-message-with-an-empty-data – t.m.adam Apr 09 '18 at 12:12

1 Answers1

0

you can do something like this on client side

import socket
serverName = "localhost"
serverPort = 11000
while 1:
    var = input("Write a sentence: ")
    var = var.strip()
    if var == "":
        continue
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((serverName, serverPort))
    s.sendall(str.encode(var))
    data = s.recv(1024)
    data = str(data)
    print(data)
    s.close()
user8190410
  • 1,264
  • 2
  • 8
  • 15
  • Thanks, but I want this validation to happen on the server side. Is that even possible? – Albos Hajdari Apr 09 '18 at 12:09
  • yes, remove continue from the while loop and use strip() to remove whitespaces (like \n,\t,' ', etc) from the input – user8190410 Apr 09 '18 at 12:12
  • You don't understand me. I have a task that says I can type whatever I want on the client, but the server must never stop working (until I close it purposely) . The client must be able to input whatever he wants, the server must always continue to work. Now I just want to press ENTER on the client side, and I will remove all the \n zt '' on the server side. BUT the problem is, when I only press ENTER and I don't write anything at all at the client, the server just seems that it cannot receive that data from the client and it stops working. And that's the problem (the server not working). – Albos Hajdari Apr 09 '18 at 12:33
  • see t.m.adam link. I think that might be the answer as you cannot send an empty data packet – user8190410 Apr 09 '18 at 12:37