目前,我的UDP客户端只向服务器发送两个命令(td & TEMP
)。 td
会提供当前时间和日期。 TEMP 10.30
会将温度10.30
转换为farenheit并将结果返回给客户。
问题:
当我输入td
命令fisrt时,它工作正常。之后,如果我输入TEMP
命令,它不会被转换为farenheit,但是当前的日期和时间(一些奇怪的回复如下:Message Returned from Server : 50.5480-09 18:45:53
)。那不是我想要的。另一方面,输入td
和TEMP
反之亦然似乎正常。我不知道我的逻辑在while循环中的问题在哪里。
UDPServer:
public static void main(String arg[]) throws Exception {
DatagramSocket serversocket = new DatagramSocket(9999);
UDPServer udpserver = new UDPServer();
byte[] receivedBuffer; // = new byte[1024];
byte[] sentBuffer; //= new byte[1024];
while (true) {
receivedBuffer = new byte[1024];
sentBuffer = new byte[1024];
DatagramPacket receivedpacket = new DatagramPacket(receivedBuffer, receivedBuffer.length);
System.out.println("Server Waiting for a message from Client.....");
serversocket.receive(receivedpacket);
String fromClient = new String(receivedpacket.getData());
// enter td command to display the curerct date and time
if (fromClient != null && fromClient.startsWith("td")) {
InetAddress clientIP = receivedpacket.getAddress();
System.out.println("Message received from client : " + fromClient + " at IP Address = "
+ clientIP.getHostAddress() + ", Host Name = " + clientIP.getHostName());
String toClient = udpserver.dateAndTime();
sentBuffer = toClient.getBytes();
DatagramPacket sendpacket = new DatagramPacket(sentBuffer, sentBuffer.length, clientIP, 8888);
serversocket.send(sendpacket);
System.out.println(" Reply Message is sent to client " + clientIP.getHostAddress());
}
// converting the TEMPERATURE into Farenheit
if (fromClient != null && fromClient.startsWith("TEMP") && !fromClient.startsWith("td")) {
InetAddress clientIP = receivedpacket.getAddress();
System.out.println("Message received from client : " + fromClient + " at IP Address = "
+ clientIP.getHostAddress() + ", Host Name = " + clientIP.getHostName());
float temp = Float.parseFloat(fromClient.substring(fromClient.indexOf(' ') + 1));
float tempInFaren = (float) (temp * 1.8 + 32.0);
//float toClient = tempInFaren ;
String convertIntoFarenheit = String.valueOf(tempInFaren);
sentBuffer = convertIntoFarenheit.getBytes();
DatagramPacket sendpacket = new DatagramPacket(sentBuffer, sentBuffer.length, clientIP, 8888);
serversocket.send(sendpacket);
System.out.println(" Reply Message is sent to client " + clientIP.getHostAddress());
}
try {
Thread.sleep(2000);
} catch (InterruptedException ie) {
}
}
}
//method for returning current date and time
public String dateAndTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = new Date();
String s = sdf.format(d);
return s;
}
UDPClient:
public static void main(String args[]) throws Exception {
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
BufferedReader inFromKeyboard = new BufferedReader(new InputStreamReader(System.in));
try {
DatagramSocket clientSocket = new DatagramSocket(8888);
InetAddress IPAddress = InetAddress.getByName("localhost");
while (true) {
System.out.println("Please enter the message to send to server: ");
String sentence = inFromKeyboard.readLine();
//sending time and date command
if (sentence.startsWith("td") && !sentence.startsWith("TEMP")) {
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println(
"Message Sent to Server : " + sentence + "\n Now waiting for reply from Server....");
clientSocket.receive(receivePacket);
String fromServer = new String(receivePacket.getData());
System.out.println("Message Returned from Server : " + fromServer);
}
//sending TEMP command
if (sentence.startsWith("TEMP") && !sentence.startsWith("td")) {
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println(
"Message Sent to Server : " + sentence + "\n Now waiting for reply from Server....");
clientSocket.receive(receivePacket);
String fromServer = new String(receivePacket.getData());
System.out.println("Message Returned from Server : " + fromServer);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
问题是用于在客户端接收数据的字节数组也包含先前接收的内容。当它从服务器收到响应时,只有一部分被覆盖,因此剩下的也会与实际响应一起打印出来。
可以有很多种方法可以解决这个问题。 您可以在UDP数据包中包含字符串的长度,也可以在每次从服务器获得响应后刷新缓冲区,以便下一个响应为空。还有不同的方法可以做到这一点。一种是将缓冲区的所有内容归零,或者每次只创建一个新的缓冲区( 你已经在服务器类 中执行了此操作)。
查看客户端代码中的更改。只有while循环的开始才需要进行一些修改。
<强> UDPClient 强>:
public static void main(String args[]) throws Exception {
byte[] sendData;
byte[] receiveData;
BufferedReader inFromKeyboard = new BufferedReader(new InputStreamReader(System.in));
try {
DatagramSocket clientSocket = new DatagramSocket(8888);
InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
while (true) {
/*********************************
create a new buffer each time */
sendData = new byte[1024];
receiveData = new byte[1024];
/*********************************/
System.out.println("Please enter the message to send to server: ");
String sentence = inFromKeyboard.readLine();
//sending time and date command
if (sentence.startsWith("td") && !sentence.startsWith("TEMP")) {
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println(
"Message Sent to Server : " + sentence + "\n Now waiting for reply from Server....");
clientSocket.receive(receivePacket);
String fromServer = new String(receivePacket.getData());
System.out.println("Message Returned from Server : " + fromServer);
}
//sending TEMP command
if (sentence.startsWith("TEMP") && !sentence.startsWith("td")) {
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9999);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
System.out.println(
"Message Sent to Server : " + sentence + "\n Now waiting for reply from Server....");
clientSocket.receive(receivePacket);
String fromServer = new String(receivePacket.getData());
System.out.println("Message Returned from Server : " + fromServer);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
如果您不想每次都创建一个缓冲区,也可以使用全零重新初始化缓冲区。您需要在每次迭代中执行此操作。为此,您需要导入Arrays类。
Arrays.fill( receiveData, (byte) 0 );
Arrays.fill( sendData, (byte) 0 );
答案 1 :(得分:1)
您始终在发送和接收原始数据,但您将其视为字符串。 getData()返回1024字节的缓冲区,其中只用50.5480
填充前7位。其余的都是前一次电话会议的垃圾。
当你在时间之前得到温度时,它似乎对你有用(它确实不起作用,但问题并没有表现出来),因为时间字符串比温度字符串长并且它完全覆盖了它。在你收到时间后,你会看到同样的不愉快的效果。
解决此问题的一种方法是在UDP消息中包含字符串的长度作为第一个条目,并在从响应构造字符串时使用此字符串长度。