[英]Socket programing: java.lang.NumberFormatException: For input string: “1”
I'm sending a message through UDP sockets and packets: The mesajes has the following format: 我正在通过UDP套接字和数据包发送消息:mesajes具有以下格式:
"MESSAGE" + SEPARATOR + "IDCLIENT"
Here is the code on the Client : 这是客户端上的代码:
String msg = "MESSAGE" + SEPARATOR + id;
byte[] msgBytes = msg.getBytes();
DatagramPacket p = new DatagramPacket(msgBytes, msgBytes.length, serverIP, SERVER_PORT);
DatagramSocket socket = new DatagramSocket();
socket.send(p);
Here is the code on the Server : 这是服务器上的代码:
DatagramSocket socket = new DatagramSocket ( SERVER_PORT );
byte[] buffer = new byte[BUFFER_MSG];
DatagramPacket p = new DatagramPacket (buffer, buffer.length);
socket.receive(p);
// Here comes where my problem appears
String msg = new String ( p.getData() );
msg.trim(); //this is suposed to solve my problem, but it doesn't
String[] msgParts = msg.split(SEPARATOR);
if (msgParts.length == 2)
{
String infoMsg = msgParts[0];
int id = Integer.parseInt( msgParts[1] ); // Here the get the NumberFormatException
}
// Do more...
I don't understand the error messege: java.lang.NumberFormatException: For input string: "1"
. 我不理解错误消息: java.lang.NumberFormatException: For input string: "1"
。 Because the String "1"
should be converted to the Integer 1
, as far as I know. 据我所知,因为字符串"1"
应转换为整数1
。
PS: I have ommited the try-catch staments for IOException
for clarity. PS:为了清楚起见,我省略了IOException
的try-catch语句。
Change msg.trim();
更改msg.trim();
to msg = msg.trim();
到msg = msg.trim();
– Elliott Frisch – Elliott Frisch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.