1

Hi i got a problem with my socket server or the client the problem is i can only send one message from the client to the server then the server stops receiving for some reason i want it to receive more then one.

Server.py

import socket 
host = ''
port = 1010
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.bind((host, port)) 
s.listen(1) 
conn, addr = s.accept() 
print ("Connection from", addr)
while True: 
 databytes = conn.recv(1024)
 if not databytes:
 break
 data = databytes.decode('utf-8')
 print("Recieved: "+(data))
 if data == "dodo": 
 print("hejhej")
 if data == "did":
 response = ("Command recived") 
 conn.sendall(response.encode('utf-8'))
conn.close()

client.py

import socket 
host = '127.0.0.1'
port = 1010 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((host, port)) 
print("Connected to "+(host)+" on port "+str(port)) 
initialMessage = input("Send: ") 
s.sendall(initialMessage.encode('utf-8'))
while True:
 response = input("Send: ") 
 if response == "exit": 
 s.sendall(response.encode('utf-8')) 
s.close()
asked Jun 28, 2015 at 18:17
5
  • The server is sending only after the while loop, there is an indentation error: move conn.sendall(response.encode[...]1 space right... Commented Jun 28, 2015 at 19:37
  • Please, FIX your indentation so we know where the problem resides... Commented Jun 28, 2015 at 20:44
  • Ok i have fixed the indentation problem now i think. but i still got the same output as described in the question. Which is i can only send one message from the client to the server. Commented Jun 29, 2015 at 3:49
  • put everything after s.listen(1) into a while loop. Commented Jun 29, 2015 at 6:07
  • It dident work with putting everything after s.listen(1) into a while loop. I think the problem may lie in the loop its self either in the client or the server. im suspecting that it breaks after the first message is sent from the client. Commented Jun 29, 2015 at 15:58

1 Answer 1

2

There is nothing wrong with your code, but the LOGIC of it is wrong, in the Client.py file and particularly in this loop:

while True:
 response = input("Send: ") 
 if response == "exit": 
 s.sendall(response.encode('utf-8')) 

This will not send to your Server side anything but string exit because of this:

if response == "exit":

So you are asking your Client.py script to only send anything the user inputs matching the string exit otherwise it will not send.

It will send anything at the beginning before this while loop since you wrote:

initialMessage = input("Send: ") 
s.sendall(initialMessage.encode('utf-8'))

But after you are inside that while loop then you locked the s.sendall to only send exit string

You have to clean up your code logic.

msw
43.7k9 gold badges92 silver badges118 bronze badges
answered Jul 2, 2015 at 18:51
Sign up to request clarification or add additional context in comments.

1 Comment

Tnx for your answer. I have already figured this out. The code is working.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.