- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
该服务器的作用类似于回显服务器。客户端向服务器发送 10 个数据包(间隔 1 秒)
当客户端从服务器接收数据包时,有时会出现数据包丢失的情况。
因此客户端必须等待最多一秒数据包到达。如果数据包未在 1 秒内到达,则客户端应继续发送其他数据包。
我如何使用 .setSoTimeout 来实现这一目标?
import java.io.*;
import java.net.*;
import java.util.*;
/*
* Client to process ping requests over UDP.
*/
public class PingClient
{
private static final int AVERAGE_DELAY = 100; // milliseconds
public static void main(String[] args) throws Exception
{
// Get command line argument.
int port = Integer.parseInt(args[1]);//specified as argument
// Create random number generator for use in simulating
// packet loss and network delay.
System.out.println("Port "+port);
// Create a datagram socket for receiving and sending UDP packets
// through the port specified on the command line.
DatagramSocket socket = new DatagramSocket(1234);
int i=0;
for(i=0;i<10;i++)
{
byte[] buf = new byte[1024] ;
Calendar cal=Calendar.getInstance();
String ping="Ping "+ i +" "+cal.getTimeInMillis()+"\r\n";
buf=ping.getBytes("UTF-8");
InetAddress address = InetAddress.getByName(args[0]);
System.out.println("Name "+args[1]);
DatagramPacket packet = new DatagramPacket(buf, buf.length,
address, port);
packet.setData(buf);
socket.send(packet);
Thread.sleep( 10* AVERAGE_DELAY);//1 sec
DatagramPacket server_response = new DatagramPacket(new byte[1024], 1024);
// Block until the host receives a UDP packet.
socket.setSoTimeout(1000); //I don't know how to use this
socket.receive(server_response);
// Print the recieved data.
printData(server_response);
}
}
private static void printData(DatagramPacket request) throws Exception
{
// Obtain references to the packet's array of bytes.
byte[] buf = request.getData();
// Wrap the bytes in a byte array input stream,
// so that you can read the data as a stream of bytes.
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
// Wrap the byte array output stream in an input stream reader,
// so you can read the data as a stream of characters.
InputStreamReader isr = new InputStreamReader(bais);
// Wrap the input stream reader in a bufferred reader,
// so you can read the character data a line at a time.
// (A line is a sequence of chars terminated by any combination of \r and \n.)
BufferedReader br = new BufferedReader(isr);
// The message data is contained in a single line, so read this line.
String line = br.readLine();
// Print host address and data received from it.
System.out.println(
"Received from " +
request.getAddress().getHostAddress() +
": " +
new String(line) );
}
}
最佳答案
With this option set to a non-zero timeout, a call to receive() for this DatagramSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the DatagramSocket is still valid.
所以,如果你想在 1 秒后没有收到响应的情况下发送数据包,你只需使用
socket.setSoTimeout(1000L);
boolean continueSending = true;
int counter = 0;
while (continueSending && counter < 10) {
// send to server omitted
counter++;
try {
socket.receive(packet);
continueSending = false; // a packet has been received : stop sending
}
catch (SocketTimeoutException e) {
// no response received after 1 second. continue sending
}
}
关于java - 数据报套接字上的 setSotimeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7860904/
我有一个twisted从这里代理:Python Twisted proxy - how to intercept packets . 它打印 HTTP 数据,我还想拦截并检查原始 IP 数据报。如何h
如果我在从客户端到服务器的 udp 套接字(在我的例子中是 QUdpSocket)上连续写入 2 次,我是否可能只收到一个数据报,其中包含我从客户端写入的两个数据?或者我确定我总是会得到 2 个单独的
我正在尝试使用原始套接字在 ruby 中编写自己的 UDP 实现,以用于教育目的。 这是我到目前为止所拥有的: require 'socket' addr = Socket.pack_sockad
我有一个非常基本的问题。想象一下,有一个简单的 udp 客户端,它正在监听传入的数据报。这些数据报只是字节[],但它们可能包含复杂的信息,如字符串、整数等。 是否有任何优雅的方法来解码数据报,例如使用
我必须开发一个分布式系统,该系统包括通过 TCP 套接字与来自其 IP 地址的某人进行聊天 session ,该系统连接到一个服务器,该服务器列出了过去 3 分钟连接到它的所有 ip。除了这个主要功能
使用Java编写基于UDP的客户端/服务器。 当服务器不断从套接字获取数据包时: socket.receive(packet); 即使客户端发送多个数据包,例如数据包 1、2、3 和 4,客户端也只能
您好,我想制作一个与“SendIP 发送任意 IP 数据包的命令行工具”类似的程序 我有一个程序可以发送“IPv4 + UDP”。我尝试通过 Wireshark 验证我的程序,但没有收到任何消息,我不
我正在尝试设计一个系统,其中有发送方和接收方,双方都可以发送和接收数据包。我为每个发送方和接收方使用 2 个数据报 channel ,并在每一侧使用 2 个线程来处理该问题。 从发送者向接收者发送数据
我的问题:使用 net.Read... 方法仅复制给定字节数组或 slice 大小的字节数。我当然不想每次都分配最大 64 kB 的 UDP 数据报。 是否有go方法来确定数据报的大小(在数据报头中)
考虑通过套接字发送的 100 个字节。使用 TCP 套接字,如果我调用 recv()长度为 50,我得到前 50 个字节,如果我再次调用它,我得到第二个 50 个字节。使用 UDP 套接字,如果我调用
有谁知道 Tibco rvd 数据报的可用 Javadoc(特别是类“com.tibco.tibrv.TibrvMsg”)?反编译类以获得签名并不难,但这并不能说明预期的最佳用法等。 我所需要的只是用
通过无连接数据报 Unix 套接字发送时出现 ECONNREFUSED 的可能原因是什么? 也欢迎任何有关如何调试此问题的建议,因为此问题是可重现的。 无论是否使用 sendto() 或 sendms
在为 Android 编写一个简单的 UDP 广播应用程序时,我遇到了一个奇怪的问题。 基本上,我的应用程序想要将数据流广播到本地网络上的设备。每个 UDP 数据包都有一个按顺序递增的数字,以便我知道
简单地说,我能够从连接的套接字接收数据,直到它启动后大约 10 秒。解决此问题的解决方案是,将数据发送到“客户端”(ARDrone)以保持数据流继续运行,否则它将停止向手机发送数据。但是,出于某种原因
这个问题在这里已经有了答案: How do I get amount of queued data for UDP socket? (2 个回答) 3年前关闭。 哪一个ioctl的 FIONREAD返
我正在向这个 Haskell 服务器发送简单的 UDP 数据包。对于数据包的来源,我使用“aspell -l en dump master”生成的纯文本文件。但是,任何超过 120,000 条消息的列
服务器: $address = '127.0.0.1'; $port = 46123; if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_
我有一个宽带雷达,我想打开。要打开它,我必须发送两个寄存器,如下所示: int reg0[3] = {0x00, 0xC1, 0x01}; int reg1[3] = {0x
我正在尝试使用 udp 套接字发送一些数据,并通过 Windows 环回适配器在同一个套接字上接收它们。在我的网络属性中,我将环回适配器设置为具有以下 ip 192.168.1.1 recvfrom
这个问题在这里已经有了答案: How does Skype work without port forwarding? (4 个答案) 关闭 9 年前。 我是网络新手...我有一个(愚蠢?:))关于
我是一名优秀的程序员,十分优秀!