简体   繁体   English

PHP到Python TCP Socket拒绝连接

[英]PHP to Python TCP Socket refused connection

I am trying to set up a basic TCP socket between two applications on the same hardware. 我试图在同一硬件上的两个应用程序之间设置一个基本的TCP套接字。 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. 我已经在另一台笔记本电脑(充当服务器)和python脚本之间有一个工作套接字,但似乎无法让另一台工作。 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). 背景是我需要在笔记本电脑上运行用户界面,该笔记本电脑通过有线局域网(无需互联网)链接到运行网络的Rpi(这是用户界面的python脚本和html代码所在的位置)。 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. 我需要从python脚本中的用户界面访问用户输入,所以我将在两个脚本(PHP和Python)之间使用套接字来发送数据。 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. Rpi已经充当了一个单独系统的客户端,数据通过它的socket快乐地进入。我打算在PHP脚本中创建新的套接字(我想它会成为服务器)并从这个套接字作为客户端读取在Python脚本中。 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: PHP套接字创建代码如下:

$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): Python代码(还包括现有的套接字代码):

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. 据我所知,只要为每个服务器和客户端提供单独的套接字,TCP网络就可以在同一系统上与服务器和客户端一起工作。 Any help would be appreciated, been stuck on this for some time now. 任何帮助将不胜感激,现在已经坚持了一段时间。

Your PHP code is missing call to socket_accept : 您的PHP代码缺少对socket_accept调用:

socket_accept($sock);

Without socket_accept , php script will exit after socket_listen without waiting for client connection. 没有socket_accept ,php脚本将在socket_listen之后退出而不等待客户端连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM