2012-04-24 10 views
1

私は、UDPソケットを扱うための私の基本クラスに以下のコード(以下の抜粋)を持っています。私はWinRTのサポートを追加しています(WINRTの#defineによって制御されています)。私は.Netのためにすべての作業をしていますが、私のWinRTの実装で何か問題が起きています。データを送信すると、サーバー側で受信されていません。どんな考えが間違っているのでしょうか?私はクライアント側で何のエラーも表示されません、ただ何もサーバー上に表示されます。同様の方法でDataWriterを使用して、私のTCPソケットクラスを正常に稼働させました。UDP DatagramSocketの正しい使い方

 public UdpSocketClient(IPEndPoint remoteEndPoint, int localPort) 
     { 
      this.remoteEndPoint = remoteEndPoint; 
#if WINRT 
      this.socket = new DatagramSocket(); 
      this.socket.MessageReceived += ReceiveCallback; 

      // Bind to any port 
      Logger.Debug("{0}: UdpSocketClient created. Binding to port {1}.", this, (localPort == 0) ? "[any]" : localPort.ToString()); 
      IAsyncAction bindAction = this.socket.BindEndpointAsync(new HostName("localhost"), localPort == 0 ? "0" : localPort.ToString()); 
      bindAction.AsTask().Wait(); 
      Logger.Trace("{0}: Bind Complete to port {1}", this, this.socket.Information.LocalServiceName); 

      // Get IOutputStream 
      Logger.Trace("{0}: Getting output stream to {1}.", this, remoteEndPoint); 
      IAsyncOperation<IOutputStream> asyncOutput = this.socket.GetOutputStreamAsync(remoteEndPoint.address, remoteEndPoint.port.ToString()); 
      asyncOutput.AsTask().Wait(); 
      Logger.Trace("{0}: Got output stream.", this); 

      // Create DataWriter 
      dataWriter = new DataWriter(asyncOutput.GetResults()); 
#else 
      ... 
#endif 
     } 


     public void SendBuffer(ByteBuffer buffer, int wait = 0) 
     { 
#if WINRT 
      Logger.Trace("{0}: Sending buffer. Wait = {1}", this, wait); 
      for (int i = 0; i < buffer.WriteSize(); i++) 
       dataWriter.WriteByte(buffer.Buffer()[i]); 
      DataWriterStoreOperation op = dataWriter.StoreAsync(); 
      if (wait != 0) op.AsTask().Wait(wait); 
      else op.AsTask().Wait(); 
      Logger.Trace("{0}: Sending complete.", this); 
#else 
     ... 
#endif 
     } 

いくつかの関連するログは:

04/23 19:08:57.504 DEBUG: Area Sync: UdpSocketClient created. Binding to port [any]. 
04/23 19:08:57.505 TRACE: Area Sync: Bind Complete to port 59518 
04/23 19:08:57.506 TRACE: Area Sync: Getting output stream to 71.227.179.128:1302. 
04/23 19:08:57.507 TRACE: Area Sync: Got output stream. 

04/23 19:08:57.604 TRACE: Area Sync: Sending contact packet. 
04/23 19:08:57.604 TRACE: Area Sync: Sending buffer. Wait = 0 
04/23 19:08:57.605 TRACE: Area Sync: Sending complete. 
+1

スタート:私はConnectAsyncのAPIを使用するのであれば、それは動作します。 –

+0

私はパケットを送信していないと確信しています。私は同じ.Netコードを実行し、それは動作します。 –

答えて

1

私はそれを考え出しました。明らかに私のバインドコードが好きではない。ドキュメントには、バインドにnullを渡すことができるはずだが、私はいつも例外がある。クライアントが実際にネットワーク上にパケットを送信しているかどうかを確認するために、パケットスニファを使用して

 public UdpSocketClient(IPEndPoint remoteEndPoint, int localPort) 
     { 
      this.remoteEndPoint = remoteEndPoint; 
#if WINRT 
      this.socket = new DatagramSocket(); 
      this.socket.MessageReceived += ReceiveCallback; 

      Logger.Trace("{0}: Connecting to {1}.", this, remoteEndPoint); 
      IAsyncAction connectAction = this.socket.ConnectAsync(remoteEndPoint.address, remoteEndPoint.port.ToString()); 
      connectAction.AsTask().Wait(); 
      Logger.Trace("{0}: Connect Complete. Status {1}, ErrorCode {2}", this, connectAction.Status, 
       connectAction.ErrorCode != null ? connectAction.ErrorCode.Message : "None"); 

      dataWriter = new DataWriter(this.socket.OutputStream); 
#else 
      ... 
#endif 
     } 
関連する問題