我正在尝试运行一个基本的 UDPCLient-Server 程序。当我编译我的 UDPClient 程序时,它会导致以下错误。虽然我的服务器一编译成功。
有人可以指导发生了什么问题吗?
错误:
UDPClient.java:13: error: cannot find symbol
new DatagramPacket(m,m.length(),aHost,serverPort);
^
symbol: method length()
location: variable d of type byte[]
1 error
我的代码:
import java.net.*;
import java.io.*;
public class UDPClient{
public static void main(String args[]){
// args give message contents and server hostname
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket();
byte[] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 6789;
DatagramPacket request =
new DatagramPacket(m,m.length(),aHost,serverPort);
aSocket.send(request);
byte[] buffer = new byte[1000];
DatagramPacket reply = new DatagramPacket(buffer,buffer.length);
aSocket.receive(reply);
System.out.println("Reply: " + new String(reply.getData()));
}
catch (SocketException e){
System.out.println("Socket: " + e.getMessage());
}
catch (IOException e){
System.out.println("IO: " + e.getMessage());
}
finally { if (aSocket != null) aSocket.close(); }
}
}