开发者

Python socket server and client, keeps losing connection

I am expanding on a GUI program for a remotely controlled robot. The idea is to program a simple GUI client in Python that connects to a remote server, also written in Python. The client would send simple messages to the server, the server would receive the message, and then transmit it via serial to an Arduino Mega that is connected via USB.

I hav开发者_JAVA技巧e gotten the code to work, kind of.

Right now, I can connect one time from my client Python GUI, send one message, then it loses connection. Then I must halt the server, restart it, and then send another single message.

What is happening here?

Here is the server script, adopted from another source.

import serial
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 9000))
sock.listen(1)
print "Listening on TCP 9000"
motor = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
print "Connected to Arduino Mega at: /dev/ttyUSB0"
while(1):
    print "Waiting For Connection..."
    connection, addr = sock.accept()
    connection.setblocking(0)
    print "Connected by", addr[0]
    while(1):
        try:
            sockdata = connection.recv(1)
            break
        except:
            pass
    if sockdata == 'X':
        print "Exiting"
        break
    if sockdata == '0':
        print "Stopping"
        motor.write(sockdata)
    if sockdata == '1':
        print "Forward"
        motor.write(sockdata)
    if sockdata == '2':
        print "Left"
        motor.write(sockdata)
    if sockdata == '3':
        print "Right"
        motor.write(sockdata)
    if sockdata == '4':
        motor.write(sockdata)
        print "Backwards"
    else:
        pass

And here is my client code, minus the resource file of course.

from socket import *
from PythonCard import model
HOST = ''
PORT = 9000
ADDR = (HOST,PORT)

Client = socket (AF_INET,SOCK_STREAM)
Client.connect((ADDR))

class MainWindow(model.Background):
    def on_FwdBtn_mouseClick(self, event):
        Client.send('1')
    def on_LftBtn_mouseClick(self, event):
        Client.send('2')
    def on_RitBtn_mouseClick(self, event):
        Client.send('3')
    def on_RevBtn_mouseClick(self, event):
        Client.send('4')
    def on_StpBtn_mouseClick(self, event):
        Client.send('0')
    def on_GetPing_mouseClick(self, event):
        Client.send('~')


app = model.Application(MainWindow)
app.MainLoop()

EDIT

After looking into the below fixes or at least some of them, here is my code for the server now, the client remains the same.

import serial
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 9001))
sock.listen(1)
print "Listening on TCP 9001"
motor = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
print "Connected to Motor Controller: /dev/ttyUSB0"
print "Waiting For Connection..."
connection, addr = sock.accept()
connection.setblocking(0)
while(1):

    print "Connected by", addr[0]
    while(1):
        try:
            sockdata = connection.recv(1024)
            break
        except:
            pass
    if sockdata == 'X':
        print "Exiting"
        break
    if sockdata == '0':
        print "Stopping"
        motor.write(sockdata)
    if sockdata == '1':
        print "Forward"
        motor.write(sockdata)
    if sockdata == '2':
        print "Left"
        motor.write(sockdata)
    if sockdata == '3':
        print "Right"
        motor.write(sockdata)
    if sockdata == '4':
        motor.write(sockdata)
        print "Backwards"
    else:
        pass

This works a little better, but still not right I don't think. When I run this I get the server running, and after starting my GUI, I can watch my terminal window on the server and will get the actual commands as they come through

"Connected from 127.0.0.1" "Forward" "Connected from 127.0.0.1" "left" "Connected from 127.0.0.1" "right" "Connected from 127.0.0.1"

It looks as though each time I send a command I get reconnected. I want to maintain a connection once I start my GUI, not disconnect and reconnect each time I send a command. Sorry if I sound kind of stupid in this, but I just started with Python about three weeks ago.


You are calling accept() inside the loop, which will block for another connection. The original connection will be dereferenced and automatically closed when/if another connection comes in. Normally, you spawn an independent handler (fork, thread, or add to async event handler) to handle new connections that does the work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜