我通过 DatagramChannel 发送消息如下:
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
String request_message = "request";
buf.put(request_message.getBytes());
buf.flip();
int bytesSent = udpserver.send(buf, successor_port_1); // udpserver is a DatagramChannel
然后我阅读了服务器上的消息:
ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
String message = new String(buf.array(), Charset.forName("UTF-8"));
System.out.println(message); //print message for testing purposes
if (message == "request"){
//code here does not get executed
}
问题是,我的代码没有输入“if”语句,即使 message = “request” 似乎也得到了我的 print 语句的确认。
任何帮助,将不胜感激!