0

I want to create a simple communication between a server and a client using sockets. The cliend is supposed to send a message and then the server sends a message to the client.

This is my Client code :

import socket
s = socket.socket()
HOST = '127.0.0.1'
s.connect((HOST, 1234))
s.send('Hi')
print ('Client send')
print s.recv(1024)
s.close

This is my Server's code :

import socket
s = socket.socket()
HOST = '127.0.0.1'
s.bind((HOST, 1234))
s.listen(5)
while True:
    c, addr = s.accept()
    c.send('Hi client')
    c.close()

But it only prints "Client send " .

2
  • Works for me Python 2.7.9. BTW s.close should be s.close() Commented Mar 11, 2015 at 14:12
  • It says " Traceback (most recent call last): File "C:\Users\Angelo\Desktop\CLIENT.py", line 7, in <module> print s.recv(1024) error: [Errno 10054] An existing connection was forcibly closed by the remote host " Commented Mar 11, 2015 at 14:19

3 Answers 3

1

In your server, after having sent 'Hi client' you must wait for the client to have read the message.

You could do either of two things:

Update: tried it on my system (MacOSX). Started two python interpreters. Pasted the server code verbatim in one; server is now up and running and accepting connections.

In the other python interpreter, the client shell, I did the following

>>> import socket
>>> HOST = '127.0.0.1'    
>>> def test():
...    s = socket.socket()
...    s.connect((HOST, 1234))
...    s.send('Hi')
...    print s.recv(1024)
...    s.close()       # <== Note function call here!
...                                                                                                                                 
>>> test()
Hi client
>>> test()
Hi client
>>> test()
Hi client
>>> test()
Hi client

This demonstrates that - at least on my system - the code works as anticipated.

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

1 Comment

I did this : while True: c, addr = s.accept() c.send('Hi client') print c.recv(1024) c.close()
0

I can't reprocude your problem and therefore vote to close your question.

Here's the code I used:

import socket
import threading

HOST = '127.0.0.1'
PORT = 12345

def client():
    s = socket.socket()
    s.connect((HOST, PORT))
    s.send('Hi')
    print ('Client send')
    print s.recv(1024)
    s.close()

def server():
    s = socket.socket()
    s.bind((HOST, PORT))
    s.listen(5)
    c, addr = s.accept()
    c.send('Hi client')
    c.close()

if __name__ == "__main__":
    server = threading.Thread(target=server)
    server.start()
    client()
    server.join()

Here's the output I got:

$ python test.py
Client send
Hi client

If the problem persists, please add additional details to your question about why and how your setup still not works. Maybe the problem is just about how you run the programs. Please add details about this as well.

Comments

0

The underlying problem is that you treat sockets as if they were message-busses (one message at a time, always received fully), where in fact they are streams of bytes.

Nobody guarantees you that sending X bytes of data will reach the opposite side as that. It could be X1, X2, X3, Y1 where X1+X2+X3=X, and Y1 being the beginning of the next message Y. And all other kinds of combinations and errors.

To remedy this, real-world client-server apps use protocols. Take HTPP for example: a request starts always with HTTP, and it ends with two newlines. Within this block, there is the Content-Length header telling the client how many bytes to read then - regardless of chunk sizes.

So to really solve your problem, you either have to write a full fledged protocol, or built upon libraries that do that for you - e.g. Twisted, Nanomsg or ZeroMQ

Comments

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.