user791953
user791953

Reputation: 639

How do you maintain continous client / server communication?

I am trying to have a client connect to my server, and have a stream of communication between them. The only reason the connection should break is due to network errors, or unless the client wants to stop talking.

The issue I am running into is keeping the handler in a tight loop, and parsing the JSON.

My server code is :

#!/usr/bin/env python

import SocketServer
import socket
import json
import time

class MyTCPServer(SocketServer.ThreadingTCPServer):
    allow_reuse_address = True

class MyTCPServerHandler(SocketServer.BaseRequestHandler):

    def handle(self):

        while 1:
            try:
                networkData = (self.request.recv(1024).strip())

                try:
                    jsonInputData = json.loads(networkData)
                    print jsonInputData

                    try: 
                        if jsonInputData['type'] == 'SAY_HI':
                            print "HI"
                    except Exception, e:
                        print "no hi"
                        pass

                    try: 
                        if jsonInputData['type'] == 'GO_AWAY':
                            print "Going away!"
                    except Exception, e:
                        print "no go away"
                        pass

                except Exception, e:
                    pass
                    #time.sleep(0.001)
                    #print "JSON Error", e

            except Exception, e:
                #time.sleep(0.001)
                pass
                #print "No message", e

server = MyTCPServer(('192.168.1.115', 13373), MyTCPServerHandler)
server.serve_forever()

My client code is simple :

#!/usr/bin/env python

import socket
import json
import time
import sys

hostname = '192.168.1.103'
port = 13373

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((hostname,port))

except Exception, e:
    print "Error, could not open socket: ", e

data  = {'type':'SAY_HI'}

sock.send(json.dumps(data))

data  = {'type':'SAY_BYE'}

sock.send(json.dumps(data))

Sometimes I'll see the messages being sent, "SAY_HI" and "SAY_BYE", but most of the times, no data is being displayed on the server side.

Upvotes: 0

Views: 146

Answers (1)

Nick Bastin
Nick Bastin

Reputation: 31339

This question is really not clear, but calling self.request.recv(1024) is very likely not what you want to do. You're eliminating all of the nice application-level handling that TCP will happily do for you. If you change that to self.request.recv(8) or a similarly very small number (such that recv() returns whenever it receives data, and doesn't try to fill your buffer), you may get better results.

Ultimately this is super-simplistic change, even if it works, that will not work in a larger context. You will need to be handling exceptions from your json parser on the server side and waiting for more data until an entire well-formed message is received.

This is a hopelessly more complex subject than will be handled generally in any SO answer. If you're going to be doing any amount of raw sockets programming, you absolutely must own a copy of Unix Network Programming, Volume 1.

Upvotes: 1

Related Questions