私はUDP接続について学ぶためにUDP接続についての簡単なプログラムを書こうとしています。私はいくつかの基本的なことを実装しましたが、送信したものを送信して返そうとすると、次のような問題に直面します。
私がこれをするとき; 文字列を送信する
サーバーに「asd」を返すとasdxxxxxxxxxxが返され、サーバーで取得したものを印刷しようとすると[B @ 5f186fab
どうすればこの問題を解決できますか?
より明確にするために、私はあなたに数行のコードを送ります、
クライアントで;
Scanner in = new Scanner(System.in);
String result = in.nextLine();
// send request
byte[] buf = new byte[1000];
String read = result;
InetAddress address = InetAddress.getByName("localhost");
DatagramPacket packet = new DatagramPacket(result.getBytes(), result.getBytes().length, address, 4445);
socket.send(packet);
// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// display response
String received = new String(packet.getData(), 0, packet.getLength());
System.out.println("Quote of the Moment: " + received);
サーバー内;
byte[] buf = new byte[1000];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
byte[] received = packet.getData();
System.out.println(received.toString());
// figure out response
// send the response to the client at "address" and "port"
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(received, received.length, address, port);
socket.send(packet);
皆さん、ありがとうございました
編集1 バッファに問題があると思いますが、解決方法がわかりません。