0

I am trying to set up a basic TCP socket between two applications on the same hardware. I already have a working socket between another laptop (acting as a server) and the python script but can't seem to get the other one working. The background is that I need to run a user interface on a laptop that is linked via a wired LAN (no internet required) to a Rpi running the network (this is where the python script and html code for the user interface live). I need to access user input from the user interface in the python script so I am going to use a socket between the two scripts (PHP and Python) to send data. The Rpi is already acting as a client to a separate system and data happily comes in via its socket.I was going to create the new socket in the PHP script (which I guess then becomes a server) and read from this socket as a client in the Python script. Unfortunately it is not working and I keep getting:

Traceback (most recent call last):
File "/var/www/Pipe_Sock_v3.py",line 33, in <module>
  tcpConfigSock.connect(ADDRCONFIG)
File "/usr/lib/python2.7/socket.py", line 224, in meth
  return getattr(self._sock,name)(*args)
error: [Errno 111] Connection refused

The PHP socket create code is as follows:

$host = 'aaa.aaa.aaa.aaa'   #both PHP and python script at same IP address on Rpi
$port = 8000;
$sock = socket_create(AF_INET,SOCK_STREAM,0);
socket_bind($sock,$host,$port);
socket_listen($sock);
socket_accept($sock);

Python code (existing socket code is also included):

HOST = 'bbb.bbb.bbb.bbb'    #IP address of system I am already getting data from.
HOSTCONFUG = 'aaa.aaa.aaa.aaa'  #Rpi IP Address
PORTCONFIG = 8000           #PHP socket port
BUFSIZCONFIG = 4092
ADDRCONFIG = (HOSTCONFIG,PORTCONFIG)   #PHP script link
PORTSYS = 6000              #existing system port
BUFSIZSYS = 4092
ADDRSYS = (HOST, PORTSYS)   #existing system link

tcpSysSock = socket(AF_INET, SOCK_STREAM)    #existing system socket
tcpConfigSock = socket(AF_INET, SOCK_STREAM) #non functioning PHP socket
tcpSysSock.connect(ADDRSYS)
tcpConfigSock.connect(ADDRCONFIG)

I have checked out many other posts, books and trawled the net but I can't seem to spot where I am going wrong. As I understand it TCP networks can function with a server and client on the same system as long as separate sockets are provided for each. Any help would be appreciated, been stuck on this for some time now.

4

1 回答 1

2

Your PHP code is missing call to socket_accept:

socket_accept($sock);

Without socket_accept, php script will exit after socket_listen without waiting for client connection.

于 2013-10-20T10:03:31.990 回答