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.

0

1 Answer 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.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi falsetru, It would have been great if that would have been the problem, I missed that line out when I typed the code in so the error is the same.
@AimSkyward, I see. Show the full traceback.
Hey falsetru, Just added in
@AimSkyward, Did you run php script before run python script? What is the ouput of netstat -an | grep -i listen | grep 8000 ?
The php script runs initially when i get the user interface up, although the html code loops until user input is detected, the php code is then called. This part of the php code loops continuously. running netstat gives me nothing just goes onto a new command line.
|

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.