Java客户端PHP-Server UDP打孔示例代码 [英] Java-Client PHP-Server UDP Hole Punching example code

查看:116
本文介绍了Java客户端PHP-Server UDP打孔示例代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要ea p2p服务器的项目,但我还没有找到任何java-client php-server示例代码。我理解udp打孔如何工作的概念,但我无法在代码中使用任何东西。

I'm working on a project that will require ea p2p server, but I haven't found any java-client php-server example code. I understand the concept of how udp hole punching works but I can't get anything to work in code.

我尝试了什么:

TheSocket.java

TheSocket.java

public class TheSocket {

public static String response = "hello";
public static String request;
public static String webServerAddress;

public static ServerSocket s;

protected static ServerSocket getServerSocket(int port)throws Exception{
    return new ServerSocket(port);
}

public static void handleRequest(Socket s){
    BufferedReader is;
    PrintWriter os;

    try{
        webServerAddress = s.getInetAddress().toString();
        is = new BufferedReader(new InputStreamReader(s.getInputStream()));

        request = is.readLine();

        System.out.println(request);

        os = new PrintWriter(s.getOutputStream(), true);
        os.println("HTTP/1.0 200");
        os.println("Content-type: text/html");
        os.println("Server-name: TheSocket");
        os.println("Content-length: " + response.length());
        os.println("");
        os.println(response);
        os.flush();
        os.close();
        s.close();

    }catch(Exception e){
        System.out.println("Failed to send response to client: " + e.getMessage());
    }finally{
        if(s != null){
            try{
                s.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    return;
}
}

Main.java

Main.java

public class Main {

public static void main(String[] args)throws Exception{
    TheSocket.s = TheSocket.getServerSocket(6789);
    while(true){
        Socket serverSocket = TheSocket.s.accept();
        TheSocket.handleRequest(serverSocket);
    }
}

PHP-CONNECT.php - 获取其他用户端口,我手动连接并使用网页上显示的端口。

PHP-CONNECT.php - to get the other users port, I manually connect and use the port shown on webpage.

<?php
    echo $_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT'];
?>

上面代码的问题是,除非我向前移动,否则它无法进入套接字。

The issue with the code above, is that it cant make it to the socket unless I port forward.

如果您有任何问题请注释!

Comment if you have any questions!

推荐答案

我正面临着类似的问题。并试图以类似的方式解决它。

I was facing a similar problem. And was trying to solve it in a similar way.

您的代码的某些部分对我来说是错误的。
Java中的套接字用于 TCP ,但标题为 UDP 。因此你应该使用DatagramSockets。
但是接下来我们也陷入了困境。 HTTP请求也使用tcp,因此在关闭tcp会话后,使用HTTP打开端口可能会导致端口损坏。 (只是一个猜测)

Some parts of your code look wrong to me. Sockets in Java are made for TCP but the title says UDP. Therefore u should use DatagramSockets. But then we come to the point where i stuck too. HTTP-Requests use tcp as well, so opening the port with HTTP might lead to a corrupt port, after tcp session was closed. (Just a guess)

public class Main {

    public static void main(String[] args) {

        try
        {

            String httpRequest = "GET /index.php HTTP/1.1\n" +
                    "Host: <PHP SERVER NAME HERE>";

            InetAddress IPAddress = InetAddress.getByName(<PHP SERVER IP HERE>);

            DatagramSocket clientSocket = new DatagramSocket();
            byte[] sendData = new byte[1024];
            byte[] receiveData = new byte[1024];
            String sentence = httpRequest;
            sendData = sentence.getBytes();

            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 80);
            clientSocket.send(sendPacket);
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);

            String modifiedSentence = new String(receivePacket.getData());
            System.out.println("FROM SERVER:" + modifiedSentence);

            clientSocket.close();

        }catch(Exception e){e.printStackTrace();}

    }


}

上面的代码理论上会发出HTTP over UDP请求。这样显示的端口就是UDP端口。在我的情况下,我没有从PHP服务器得到任何响应,并坚持在clientSocket.recieve(..)。我想因为我的网络服务器的防火墙阻止了udp数据包。
如果代码适用于任何人,我会这样做:

The Code above theoretically sents a HTTP over UDP request. So that the displayed Port will be the UDP one. In my case i didnt get any response from the PHP Server and stuck at clientSocket.recieve(..) . I guess because the firewall of my webserver is blocking udp packets. If the code works by anyone i would proceed like this:


  1. 将所有访问ips和端口保存到数据库并列出它们

  2. 将上述DatagramPackets中的数据写入另一个客户端。

我希望这可能会有所帮助。如果有人能够完全工作,我也会对它感兴趣:)

I hope this may help. If anyone can get it completly working i would also be interested in it :)

这篇关于Java客户端PHP-Server UDP打孔示例代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆