Java客户端PHP服务器UDP打孔示例代码
我正在从事一个需要ea p2p服务器的项目,但我还没有找到任何java客户端php服务器示例代码。我理解udp打孔的工作原理,但我无法在代码中得到任何东西 我所尝试的: javaJava客户端PHP服务器UDP打孔示例代码,java,php,sockets,udp,hole-punching,Java,Php,Sockets,Udp,Hole Punching,我正在从事一个需要ea p2p服务器的项目,但我还没有找到任何java客户端php服务器示例代码。我理解udp打孔的工作原理,但我无法在代码中得到任何东西 我所尝试的: java public class TheSocket { public static String response = "hello"; public static String request; public static String webServerAddress; public static ServerSo
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
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'];
?>
上面代码的问题是,除非我向前移植,否则它无法到达套接字
如果您有任何问题,请发表评论 我也面临着类似的问题。并试图以类似的方式解决这个问题 在我看来,你代码的某些部分是错误的。 Java中的套接字是为TCP而设计的,但标题上写着UDP。因此,你应该使用DatagramSockets。 但后来我也陷入了困境。HTTP请求也使用tcp,因此在tcp会话关闭后,使用HTTP打开端口可能会导致端口损坏。(只是猜测)
公共类主{
公共静态void main(字符串[]args){
尝试
{
字符串httpRequest=“GET/index.php HTTP/1.1\n”+
“主持人:”;
InetAddress IPAddress=InetAddress.getByName();
DatagramSocket clientSocket=新DatagramSocket();
字节[]发送数据=新字节[1024];
字节[]接收数据=新字节[1024];
字符串语句=httpRequest;
sendData=句子.getBytes();
DatagramPacket sendPacket=新的DatagramPacket(sendData,sendData.length,IPAddress,80);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=新的DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedcentence=新字符串(receivePacket.getData());
System.out.println(“来自服务器:”+ModifiedEntence);
clientSocket.close();
}catch(异常e){e.printStackTrace();}
}
}
上面的代码理论上发出HTTP over UDP请求。因此,显示的端口将是UDP端口。在我的例子中,我没有从PHP服务器得到任何响应,而是停留在clientSocket.recieve(..)上。我猜是因为我的Web服务器的防火墙阻止了udp数据包。
如果任何人都能使用该代码,我会这样做:
我希望这能有所帮助。如果有人能让它完全工作,我也会对它感兴趣:)你已经有编写的代码了吗?如果是这样的话,请提供代码并详细说明,到底什么不起作用我没有任何代码,否则我会添加它欢迎加入堆栈溢出,请在本网站阅读。在提问之前,一定要明确并尽最大努力,不要将堆栈溢出视为教程的存储库。我建议您实现一个简单的
PHP服务器
。然后,您可以询问如何将Java客户端
连接到您的特定PHP服务器
,该服务器将提供给这里的人一些想法。@teocci该图描述了连接路径,这将允许我在不使用端口转发的情况下通过,我所要求的只是关于如何做到这一点的示例代码:)正如我告诉你的,这不是一个教程库。如果您想要一个例子,请访问Github或在google上搜索。如果你想提出一个恰当的问题,请在本网站上阅读。
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();}
}
}