有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaClient PHPServer UDP打孔示例代码

我正在从事一个需要ea p2p服务器的项目,但我还没有找到任何java客户端php服务器示例代码。我理解udp打孔的工作原理,但我无法在代码中得到任何东西

enter image description here

我所尝试的:

小盒。爪哇

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;
}
}

梅因。爪哇

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
    echo $_SERVER['REMOTE_ADDR'].':'.$_SERVER['REMOTE_PORT'];
?>

上面代码的问题是,除非我向前移植,否则它无法到达socket

如果您有任何问题,请发表评论


共 (1) 个答案

  1. # 1 楼答案

    我也面临着类似的问题。并试图以类似的方式解决这个问题

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


    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上。接收(…)。我猜是因为我的Web服务器的防火墙阻止了udp数据包。 如果任何人都能使用该代码,我会这样做:

    1. 将所有访问IP和端口保存到数据库,并将它们列出给其他客户端
    2. 将上述数据包中的ur数据写入另一个客户端

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