2012-05-04 25 views
2

私のプログラムで少し問題がありました。ここ は、それがどのように動作するかを示します。C#UDPソケットは応答を待ち受けませんか?

  1. C#クライアントがJavaサーバーにデータを送信し
  2. Javaサーバがデータをチェック
  3. Javaサーバは、C#クライアントにコマンドを送り返す
  4. 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); 

のですか?

+0

通常、送信したいものを送信したら、ソケットを閉じる必要があります。あなたは 'serverSocket'を閉じようとしましたか? – npinti

+1

私はあなたがクライアントコードでBindの呼び出しを見逃していると思います。 – wolfcastle

+0

@nptiniもう閉じなくなってもソケットにアクセスできませんが、UdpClientと新しいソケットを使ってみましたが、まだ動作しませんでした。 – user1137183

答えて

1

私は、あなたがクライアントソケットでバインドする呼び出しが不足していると思います。

// Allowing the response to come in ON port 40001 
EndPoint response = new IPEndPoint(IPAddress.Any, 40001); 

socket.Bind(response); // bind to port 40001 on some local address 
+0

それはトリックをした、ありがとう。完全に忘れてしまった... – user1137183

2

おそらく問題はありませんが、通常、UDP応答は元の要求の発信(送信元)ポートに返されます。応答を固定ポートに送り返しています。

+0

ありがとうございます!エラーはC#の部分にありましたが、socket.Bind()を追加するだけでうまくいきました。クライアントは今正しく聞いています。 – user1137183

関連する問題