文字列に変換された整数(トランザクションID)を受信すると、Java UDPサーバープログラムのスニペットでエラーが発生します。別のクライアントプログラムから:スレッド "main"の例外java.lang.NumberFormatException:入力文字列: "100"
CLIENT:
// Sending transaction ID to server
sendData = String.valueOf(transID).getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, sportno);
clientSocket.send(sendPacket);
がSERVER:
// Receive client's transaction ID
receiveData = new byte[1024]; // 1024 byte limit when receiving
receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket); // Server receives packet "100" from client
String clientMsg = new String(receivePacket.getData()); // String to hold received packet
int transID = Integer.valueOf(clientMsg); // Convert clientMsg to integer
System.out.println("Client has sent: " + transID); // Printing integer before iteration
クライアントは、私が得る文字列から整数変換時に、うまくパケットを送信している間このエラー:
Exception in thread "main" java.lang.NumberFormatException: For input string: "100"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at server.main(server.java:46)
同様の結果を得たInteger.parseInt(clientMsg)も使用しました。しかし、プログラムは整数に変換しようとしていない限り、パケットを文字列値として印刷することができました。
さらに、これは端末でこれを示していましたが、このエラーメッセージをこのポストに貼り付けたときには、 "100"の後に大量の ""が続きました。 receiveData変数からの空のバイト。
これらの空の値が私のプログラムが失敗した理由ですか?そうでない場合、プログラムが失敗する他の理由は何ですか?さらに、このエラーを防ぐために、文字列に変換せずにトランザクションID整数をサーバーに送信する方法はありますか?
"これらの空の値は私のプログラムが失敗した理由ですか?はい。 –
https://stackoverflow.com/a/16309654/2107818 – abdurrahimefe
をご覧ください。https://stackoverflow.com/questions/237159/whats-the-best-way-to-check-if-a-string-represents- -internet-in-java –