Client Server application with PHP and Python

此生再无相见时 提交于 2019-12-10 11:34:21

问题


I am trying to send data from PHP Client to Python Server.

CLIENT

<?php

$host = "127.0.0.1";
$port = 2000;

$output="datatatatatatta" ;

$socket1 = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");

socket_connect ($socket1 , $host,$port ) ;

socket_write($socket1, $output, strlen ($output)) or die("Could not write output\n");

socket_close($socket1) ;


?>

SERVER

import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

host= 'VAC01.VACLab.com'
port=int(2000)
s.bind((host,port))
s.listen(1)

conn,addr =s.accept()

print (conn,addr)

data=conn.recv(100000)
data=data.decode("utf-8")

s.close


FILE = open("c:/vinod/vin.txt","w")
FILE.write(str(data))
FILE.close()

On the client side I get the following error ,.

Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\xampp\socket.php on line 10

Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\xampp\htdocs\xampp\socket.php on line 12
Could not write output 

Is this possible to connect this way ?


回答1:


You bind to a host that most likely does not resolve to 127.0.0.1 but connect to 127.0.0.1. That cannot work. You have two options:

  1. Use 127.0.0.1 (or localhost) on both sides
  2. Use the hostname VAC01.VACLab.com on both sides


来源:https://stackoverflow.com/questions/10008396/client-server-application-with-php-and-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!