0

I'm trying to set up a small server where when the client logs in gets some messages.

The server code

import socket
#Networking
s = socket.socket()
print("Network successfully created")
port = 3642
s.bind(('',port))
print("Network has been binded to %s" %(port))
s.listen(5)
print("Waiting for connections")
while True:
 c, addr = s.accept()
 print("Got a connection from",addr)
 c.send(bytes("Thank you for connecting to me. Currently we","utf-8"))
 c.send(bytes("Working on the server","utf-8"))
c.close()

This is the client code

# Import socket module
import socket 
# Create a socket object
s = socket.socket() 
# Define the port on which you want to connect
port = 3642 
# connect to the server on local computer
s.connect(('MyIp..', port))
# receive data from the server
print(s.recv(1024))
# close the connection
s.close() 

Everything works fine such as the connecting and the first message gets printed, however I can't get the second message to get printed. The one that says working on the server. I have just began learning about sockets and barely know anything about them so the solution probably is obvious it's just I can't seem to figure it out. Thank you for any responses. (I would appreciate thorough responses)

asked Aug 10, 2016 at 19:38
4
  • Seems to work fine for me. But note that with TCP, message boundaries are not respected. Hence, both buffers you send will (or at least may) get consolidated into a single recv buffer (that's what I'm seeing). Other notes: (a) This is not valid python-3.x (on client-side, due to missing parentheses in the print statement); (b) You should put the c.close() inside the loop on the server side -- otherwise you're leaving all previously-accepted connections open until the program exits. Commented Aug 10, 2016 at 20:02
  • For a I printed it wrong sorry, I edited it now. 2nd I did put c.close() inside the loop. That doesnt do it either. I only see the first statement thats printed from the server but not the second one. Commented Aug 10, 2016 at 23:08
  • The very same code you pasted works perfectly fine locally (using '127.0.0.1' or the IP address of the machine on the local network as 'MyIP'). Commented Aug 11, 2016 at 5:28
  • If the two sent buffers happen to not get consolidated into a single buffer in the recv (which can happen based on timing, which OS you're running and other factors), then it makes sense that you would not see the second buffer because you're only making one recv call. If you want to receive everything the server sent, put the recv in a loop until it returns an empty string. (Empty string indicates end-of-file [i.e. socket closed by the other end].) Commented Aug 11, 2016 at 14:38

1 Answer 1

1

If the two sent buffers happen to not get consolidated into a single buffer in the recv (which can happen based on timing, which OS you're running and other factors), then it makes sense that you would not see the second buffer because you're only making one recv call. If you want to receive everything the server sent, put the recv in a loop until it returns an empty string. (Empty string indicates end-of-file [i.e. socket closed by the other end].) – Gil Hamilton

Sign up to request clarification or add additional context in comments.

Comments

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.