Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeating/Continuous python server and client

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!

like image 517
nathan lachenmyer Avatar asked Apr 24 '14 20:04

nathan lachenmyer


1 Answers

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")
like image 139
Chris Avatar answered Nov 13 '22 11:11

Chris