11

I'd like to set up a system where I have a python client and server continuously sending / receiving data. All of the code examples I've found show how to send a single message to a socket, but not how to continuously be set up for sending/receiving data.

Right now my code is:

client.py

import socket
import time

while True:
    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        client_socket.connect(("192.168.0.250", 10220))
        data = "GET\nSONAR\n\n"
        print 'send to server: ' + data
        client_socket.send(data)
        client_socket.close()
    except Exception as msg:
        print msg

I'd like the code to be able to send commands multiple times a minute, but right now it doesn't seem to consistently send messages out, and i'm not sure why. Why isn't the control stream continuous?

server.py

import socket

host = '192.168.0.100'
port = 8220
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((address))
server_socket.listen(5)

while True:
    try:
        print "Listening for client . . ."
        conn, address = server_socket.accept()
        print "Connected to client at ", address
        #pick a large output buffer size because i dont necessarily know how big the incoming packet is                                                                                              
        output = conn.recv(2048);
        if output:
            print "Message received from client:"
            print output

        #conn.send("This is a response from the server.")                                                                                                                                            
        conn.close()
        #print "Test message sent and connection closed." 

This works fine on the first try, but I can't have the server automatically listen again for the next message -- it always hangs on "Listening for client . . .".

Any thoughts?

thanks!

2 Answers 2

12

This actually works fine for me, though I had to adjust the port in client.py to match the one in server.py. I also had to add an exception to handle KeyboardInterrupt in server.py so there would be a way to exit the program.

Listening for client . . . Connected to client at ('127.0.0.1', 53944) Message received from client: GET SONAR

Listening for client . . . Connected to client at ('127.0.0.1', 53945) Message received from client: GET SONAR

EDIT:

I took a shot at improving the architecture. I create one connection and use it to pass multiple messages, pausing between each one in the client to wait for the server to send an acknowledgement.

client.py:

import socket
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 8220))

for index in xrange(5):
    data = "GET\nSONAR%d\n\n" % index
    print 'send to server: ' + data
    client_socket.send(data)
    while client_socket.recv(2048) != "ack":
        print "waiting for ack"
    print "ack received!"

#send disconnect message                                                                                                                           
dmsg = "disconnect"
print "Disconnecting"
client_socket.send(dmsg)

client_socket.close()

server.py:

import socket
import sys

host = 'localhost'
port = 8220
address = (host, port)

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(address)
server_socket.listen(5)

print "Listening for client . . ."
conn, address = server_socket.accept()
print "Connected to client at ", address
#pick a large output buffer size because i dont necessarily know how big the incoming packet is                                                    
while True:
    output = conn.recv(2048);
    if output.strip() == "disconnect":
        conn.close()
        sys.exit("Received disconnect message.  Shutting down.")
        conn.send("dack")
    elif output:
        print "Message received from client:"
        print output
        conn.send("ack")
Sign up to request clarification or add additional context in comments.

3 Comments

This was part of a bigger project I'm working on, but it looks like this wasn't the issue... the issue must be somewhere else!
See my edit for what I think is motion toward a better client/server architecture.
Interesting -- good to learn from this example! Is there a best practice for when I should send multiple messages over a single socket versus when I should close/re-open the socket for consecutive messages? Also, your boolean 'connected' in the client is never used.
-3

you better to open two while loops with try block and Server Socket then u can get continuous messages from client and viceversa.

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.