简体   繁体   English

通过服务器脚本进行简单的PHP NAT打孔

[英]Simple PHP NAT Punch Through Server Script

I am trying to write a PHP script that can serve as a "master server" and facilitate a P2P connection between two Java game clients. 我正在尝试编写一个可以充当“主服务器”并促进两个Java游戏客户端之间的P2P连接的PHP脚本。 I am using a shared web host that allows port access for the master server. 我正在使用共享的Web主机,该主机允许主服务器的端口访问。

For starters, I wanted to test UDP socket connections between the master server and a java client. 首先,我想测试主服务器和Java客户端之间的UDP套接字连接。 Here is my PHP script, named "masterServer.php" 这是我的PHP脚本,名为“ masterServer.php”

<?php
error_reporting(E_ALL);
set_time_limit(40); // Allow script to execute for at most 40 seconds.
$myFile = "output.txt";
$fh = fopen($myFile, 'w');

if ($socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP))
{

if(socket_bind($socket,0, 2005))
{
    $clientAddress = 0;
    $clientPort = 0;
    fwrite($fh, "Start at: ".time());
    fwrite($fh, "Waiting for socket at ".time());
    if(socket_recvfrom($socket, &$udp_buff, 23, MSG_WAITALL, &$clientAddress, &$clientPort)) // BLOCKING METHOD
    {
        fwrite($fh, print_r($udp_buff, true));
    }
    else
    {
        echo(socket_strerror(socket_last_error()));
        die();
    }
}
else
{
    echo(socket_strerror(socket_last_error()));
    die();
}
}
else
{
echo(socket_strerror(socket_last_error()));
die();
}

fwrite($fh, "End at: ".time());
fclose($fh);
?>

I visit masterServer.php to get the script running and within a few seconds, I launch a simple Java application that should send UDP packets to the master server. 我访问masterServer.php以使脚本运行,并在几秒钟内启动了一个简单的Java应用程序,该应用程序应将UDP数据包发送到主服务器。 Here is the code for the Java application: 这是Java应用程序的代码:

package comm;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UDPSocket 
{
public static void main (String[] asdf)
{

    try 
    {

        String host = <SERVER ADDRESS>;
        int port = 2005;

        byte[] message = "Java Source and Support".getBytes();

        // Get the internet address of the specified host
        InetAddress address = InetAddress.getByName(host);

        // Initialize a datagram packet with data and address
        DatagramPacket packet = new DatagramPacket(message, message.length,
                address, port);

        // Create a datagram socket, send the packet through it, close it.
        DatagramSocket dsocket = new DatagramSocket();
        dsocket.send(packet);
        dsocket.close();

    } 
    catch (SocketException e)
    {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

As per my understanding, the PHP server is not receiving the UDP packet. 据我了解,PHP服务器未接收到UDP数据包。 The script does not continue past the blocking socket_recvfrom() method and does not write the contents of the UDP packet to the output text file. 该脚本不会继续执行阻塞的socket_recvfrom()方法,并且不会将UDP数据包的内容写入输出文本文件。 Can anyone help me out? 谁能帮我吗?

在此处输入图片说明

Since Shared hosting are run behind routers and firewall your socket will listen to its internal ip address "192.168.1.102"(as mention in figure above) which is not connected to internet so it can't recive any data sent from outside the internal network. 由于共享主机在路由器和防火墙后面运行,因此您的套接字将监听其内部IP地址“ 192.168.1.102”(如上图所示),该地址未连接到Internet,因此它无法接收从内部网络外部发送的任何数据。

SOLUTION Since you are using UDP you can use a popular method known as UDP Puch Holing with which you can send and receive data from internet. 解决方案由于您正在使用UDP,因此可以使用一种流行的方法,称为UDP Puch Holing ,您可以使用该方法从Internet发送和接收数据。 As per your requirement. 根据您的要求。

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

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