私のプログラムで少し問題がありました。ここ は、それがどのように動作するかを示します。C#UDPソケットは応答を待ち受けませんか?
- C#クライアントがJavaサーバーにデータを送信し
- Javaサーバがデータをチェック
- Javaサーバは、C#クライアントにコマンドを送り返す
- C#クライアントは、データを受信し、可能
にログインまたは登録するユーザー私はステップ3まで得ることができたが、今私は、ステップ4
で動けなくなります0私は、サーバーとクライアントとサーバー上でWiresharkを実行しました。 すべてのパッケージが正しく出入りしています。 サーバーは1つのパケットを受信し、1つを出します。 クライアントは1つを出して1を受け取ります。 しかし、コンソールでnetstatをチェックすると、開いているポートは見えません。 実際に私は全くUDPソケットを見ません。 パケットは入ってきますが、C#クライアントは聞こえないようですが、なぜですか?
ここはC#クライアントです。
// Opening a socket with UDP as Protocol type
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// The address of the server
IPAddress[] address = Dns.GetHostAddresses("192.168.0.87");
// The Endpoint with the port
IPEndPoint endPoint = new IPEndPoint(address[0], 40001);
// Defining the values I want
string values = "Something I send here";
// Encoding to byte with UTF8
byte[] data = Encoding.UTF8.GetBytes(values);
// Sending the values to the server on port 40001
socket.SendTo(data, endPoint);
// Showing what we sent
Console.WriteLine("Sent: " + values);
// Timeout for later, for now I just let the program get stuck
// socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
// Allowing the response to come in from everywhere
EndPoint response = new IPEndPoint(IPAddress.Any, 0);
// Buffer for server response (currently bigger then actually necessary for debugging)
byte[] responseData = new byte[1024];
//Receiving the data from the server
socket.ReceiveFrom(responseData, ref response);
// Outputing what we got, we don't even get here
Console.WriteLine("You got: " + Encoding.UTF8.GetString(responseData));
// Closing the socket
socket.Close();
デバッグでは、ユーザーが正常に認証された場合、「Test」という文字列を戻したいと思います。ここで
は私がインクルードは何、任意のアイデアや提案を確認してください一部の受信ではなく、で、私はC#クライアントと間違って何かをしたことを期待するJavaサーバー
// Printing to the server that the user username logged in successfully
System.out.println("User " + username + " logged in succesfully!");
// The byte buffer for the response, for now just Test
byte[] responseData = "Test".getBytes("UTF-8");
// The Datagram Packet, getting IP from the received packet and port 40001
DatagramPacket responsePacket = new DatagramPacket(responseData, responseData.length, receivePacket.getAddress(), 40001);
// Sending the response, tried putting Thread.sleep here didn't help
serverSocket.send(responsePacket);
のですか?
通常、送信したいものを送信したら、ソケットを閉じる必要があります。あなたは 'serverSocket'を閉じようとしましたか? – npinti
私はあなたがクライアントコードでBindの呼び出しを見逃していると思います。 – wolfcastle
@nptiniもう閉じなくなってもソケットにアクセスできませんが、UdpClientと新しいソケットを使ってみましたが、まだ動作しませんでした。 – user1137183