小規模のクライアント/サーバーアプリケーションを作成しました。それはクライアントからサーバーへの1つの文字列を送信する必要があり、サーバーはそれをコンソールに表示する必要があります。しかし、サーバーは常にデータを待ちます。データがサーバーに届かないようです。何が間違っていたのですか?receive()メソッドがクライアントからデータを受信しないのはなぜですか?
public class UDPClient {
public static void main(String[] args) throws IOException {
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAdress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAdress, 4445);
clientSocket.close();
}
}
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket serverSocket = new DatagramSocket(4445);
byte[] receiveData = new byte[1024];
while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
}
}
}
メインメソッドでサーバーとクライアントの両方がなぜですか? – Stultuske
どこにあるのでしょうか?申し訳ありませんが、私はネットワークプログラミングの初心者です。 –
@Stultuske mainメソッドで2つのクラスを提供すると、この質問は_excellent_ MCVE(最小、完全、検証可能な例、これに対して+1)になります。私はまずコードを読んでエラーを見ませんでした。だから私は骨抜きになった。私は私のIDEに2つの空のクラスを作成し、メインメソッドを貼り付けました。 30秒以内に問題を再現しました。それは素晴らしいことです。 WireSharkを使った非常に短い調査の後、私はそれを解決しました。 – blafasel