简体   繁体   中英

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. I am using a shared web host that allows port access for the master server.

For starters, I wanted to test UDP socket connections between the master server and a java client. Here is my PHP script, named "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. Here is the code for the Java application:

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

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. As per your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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