- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个 PHP 脚本,它可以充当“主服务器”并促进两个 Java 游戏客户端之间的 P2P 连接。我正在使用允许主服务器端口访问的共享 Web 主机。
对于初学者来说,我想测试主服务器和 java 客户端之间的 UDP 套接字连接。这是我的 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);
?>
我访问 masterServer.php 来运行脚本,并在几秒钟内启动一个简单的 Java 应用程序,该应用程序应将 UDP 数据包发送到主服务器。以下是 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();
}
}
}
据我了解,PHP服务器没有收到UDP数据包。该脚本不会继续执行阻塞的 socket_recvfrom() 方法,并且不会将 UDP 数据包的内容写入输出文本文件。谁能帮帮我吗?
最佳答案
由于共享主机运行在路由器和防火墙后面,您的套接字将监听其内部 IP 地址“192.168.1.102”(如上图所示),该地址未连接到互联网,因此它无法接收从外部发送的任何数据内部网络。
解决方案由于您使用的是 UDP,因此您可以使用一种流行的方法,称为 UDP Puch Holing您可以使用它从互联网发送和接收数据。根据您的要求。
关于java - 简单的 PHP NAT 穿透服务器脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11474607/
缓存击穿、穿透、雪崩及解决方案 Redis是一种高性能的键值型数据库,它可以用来实现缓存功能,提高应用的响应速度和承载能力。但是,使用Redis缓存也会遇到一些常见的问题,比如缓存击穿、缓存穿透
1、前言 Redis作为一款高性能的缓存数据库,为许多应用提供了快速的数据访问和存储能力。然而,在使用Redis时,我们不可避免地会面对一些常见的问题,如缓存雪崩、缓存穿透和缓存击穿。本文将深入
本文分享自华为云社区《【高并发】什么是缓存穿透?击穿?雪崩?如何解决?》,作者:冰 河。 说到Redis,往往更多的场景是被用作系统的缓存,说到缓存,尤其是分布式缓存系统,在实际高并发场景下,稍有不慎
我想知道是否有可能实现一个只有 Javascript 的 NAT 穿透应用程序。它应该能够在浏览器中运行。 我正在寻找一种允许 P2P 连接的解决方案。服务器也会在那里,但仅用于在对等点之间进行介绍以
这个STACKBLITZ (SB) 显示了问题。 CSS 类 .WHYYYYY 显示了我面临的“两个”问题。 为了使 mat-menu 在悬停时打开,我需要在菜单按钮上设置 z-index:1050;
我是一名优秀的程序员,十分优秀!