java为什么在PHP和安卓中尝试使用socket时连接被拒绝?
为什么我会得到java。网ConnectException:/192.168.1.5:6789-连接被拒绝
我正在使用php和安卓 java代码。在问这个问题之前,我检查了像java.net.ConnectException: Connection refused这样的问题。虽然有一些有用的东西,但它们不能解决我的问题
情况是
1-我正在使用XAMPP
2-windows防火墙已关闭
3-当我运行php脚本时,我可以通过检查netstat-a看到端口6789被设置为监听
4-java“安卓”代码:
try
{
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);//"192.168.1.5" the PC which XAMPP is runing on
socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);//6789
}
catch (UnknownHostException e1)
{
e1.printStackTrace();
}
catch (IOException e1)
{
e1.printStackTrace();
}
5-php代码
<?php
set_time_limit( 0 );
$address = '127.0.0.1';
$port = 6789;
$sock = socket_create( AF_INET, SOCK_STREAM, 0 ); // 0 for SQL_TCP
socket_bind( $sock, 0, $port ) or die( 'Could not bind to address' );
socket_listen( $sock );
while (true) {
$client = socket_accept( $sock );
$input = socket_read( $client, 1024000 );
socket_write( $client, $response );
socket_close( $client );
}
socket_close( $sock );
6-我运行php脚本,它不断加载,这意味着它正在侦听,并且处于while块中
7-我启动安卓应用程序,收到通知
java.net.ConnectException: /192.168.1.5:6789 - Connection refused
最后,我确定PC(服务器)的IP地址是正确的,并且在清单中设置了internet权限
提前谢谢
共 (0) 个答案