2012-03-24 66 views
0

我正在寫一個C#P2P視頻聊天(我的考試的一部分在教師),我有點卡住通過udp發送數據。所以這裏它是如何工作的。我有一個Web_Capture庫,每一個拍攝圖像時都會將PictureBox的圖像捕獲的一個UDP客戶端錯誤 - 無法發送數據字節

private void webCamCapture_ImageCaptured(object source, WebCam_Capture.WebcamEventArgs e) 
     { 
      myCamera.Image = e.WebCamImage; 
      sendData(ref ipep2); // send it immediately 
     } 

所以後來SendData方法開始發送...

private void sendData(ref IPEndPoint sender) 
     { 
      byte[] data; 

      if (friendsClient == null) 
      { 
       friendsClient = new UdpClient(); 

      } 

      MemoryStream myStream = new MemoryStream(); 
      myCamera.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
      data = myStream.GetBuffer(); 
      friendsClient.Send(data, data.Length,sender); 

     } 

當我調試,插座異常彈出:

System.Net.Sockets.SocketException was unhandled by user code 
    Message=The requested address is not valid in its context 
    Source=System 
    ErrorCode=10049 
    NativeErrorCode=10049 

是這樣,沒有人有任何想法,我會心存感激,如果他支持與代碼的想法,因爲我在C#:)在此先感謝我nooby。馬裏安

+0

什麼是價值'ipep2'? – 2012-03-24 21:45:15

回答

1

應指定reciever的IP和端口,Here is a complete example

,所以你應該改變你的實現

private void sendData(ref IPEndPoint reciever) 
{ 
    byte[] data; 

    Socket sending_socket = new Socket(AddressFamily.InterNetwork, ocketType.Dgram, ProtocolType.Udp); 

    MemoryStream myStream = new MemoryStream(); 
    myCamera.Image.Save(myStream, System.Drawing.Imaging.ImageFormat.Jpeg); 
    data = myStream.GetBuffer(); 
    sending_socket.SendTo(data, reciever); 
} 
+0

我做了一個UDPListener和UDPPictureSender,並且像一個魅力一樣工作,我減少了很多不必要的代碼行!謝謝,你救了我的傍晚:)!祝一切順利 – hrza 2012-03-24 22:40:23