gpt4 book ai didi

java - udp如何从服务器获取整数?

转载 作者:太空宇宙 更新时间:2023-11-04 13:17:13 24 4
gpt4 key购买 nike

我将实现一个基于 UDP 的客户端/服务器应用程序,并且我是 Java 世界的新手。服务器在 UDP 协议(protocol)上实现简单的请求-确认协议(protocol)。对于接收到的带有可选参数的字符串标识符(命令),服务器返回指定的结果。例如,“thread”返回 1 到 5 之间的随机整数,length 返回 5 到 25 之间的随机整数。

问题是我的实现是否正确以及如何解决我的问题(长度 nr 始终在 1 到 5 之间)。我认为我的问题是在服务器类中没有检查 if 语句。是否有其他方法可以将命令发送到服务器,我在这里尝试通过发送字符串然后尝试使用 if 语句进行检查?

这就是我的代码(服务器):

public class server {

public static void main(String[] args) throws IOException, InterruptedException {

DatagramSocket skt;
try {
skt = new DatagramSocket(1252);

byte [] buffer = new byte[1000];

while(true){
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
skt.receive(request);
String arrayMsg = new String(request.getData());
System.out.println(arrayMsg);

//check if the client asks for threadnr
if(arrayMsg.equals("thread")){
int threadnumber = (int) (Math.random()*5)+1;
String threadnum = Integer.toString(threadnumber);
byte [] b = threadnum.getBytes();
DatagramPacket reply = new DatagramPacket(b, b.length, request.getAddress(), request.getPort());
skt.send(reply);;}

//check if the client asks for lengthnr
else if(arrayMsg.equals("length")){
int lengthnumber = (int) (Math.random()*25)+5;
String lengthnum = Integer.toString(lengthnumber);
byte [] b = lengthnum.getBytes();
DatagramPacket reply = new DatagramPacket(b, b.length,request.getAddress(), request.getPort());
skt.send(reply);}
}}
catch (SocketException e) {
System.out.println("UDP Port 9876 is occupied.");
System.exit(1);
}}}

那是我的客户:

public class client {

public static void main(String[] args) throws UnknownHostException, IOException {

DatagramSocket skt = new DatagramSocket();
InetAddress host = InetAddress.getByName("localhost");
int serverSocket = 1252;
byte [] senddata = new byte [1000];
byte [] getdata = new byte[1000];

//thread send request
String th = "thread";
senddata = th.getBytes();
DatagramPacket requestth = new DatagramPacket(senddata, senddata.length, host, serverSocket);
skt.send(requestth);

//thread get reply
DatagramPacket replyth = new DatagramPacket ( getdata, getdata.length);
skt.receive(replyth);
String arrayth = new String(replyth.getData());
int threadnr=Integer.parseInt(arrayth.trim()); //convert thread string to int and trim it (whitespace)
System.out.println("Thread received " + threadnr);

//length send request
String lg = "length";
senddata = lg.getBytes();
DatagramPacket requestlg = new DatagramPacket(senddata, senddata.length, host, serverSocket);
skt.send(requestlg);

//length get reply
DatagramPacket replylg = new DatagramPacket ( getdata, getdata.length);
skt.receive(replylg);
String arraylg = new String(replyth.getData());
int lengthnr=Integer.parseInt(arraylg.trim()); //convert thread string to int and trim it (whitespace)
System.out.println("Length received " + lengthnr);


skt.close();

我得到的只是这样的:主题已收到 3收到的长度1

(长度应该在 5 到 25 之间,在我的例子中,它永远不会大于 5)

最佳答案

代码解析第一条消息的响应而不是最后一条消息的响应,因此您会得到随机的 1-5。我建议为变量使用更好的名称,这样您会更快地发现问题。

关于java - udp如何从服务器获取整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33446466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com