0

我正在写一个 C# P2P 视频聊天(我的教师考试的一部分),我有点卡在通过 udp 发送数据。所以这里是如何工作的。我有一个 Web_Capture 库,每次捕获图像时,它都会将 PictureBox 图像设置为捕获的图像

 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# 的新手 :) 提前致谢。马里安

4

1 回答 1

1

您应该指定接收者的 ip 和端口,这是一个完整的示例

所以你应该改变你的实现

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);
}
于 2012-03-24T21:39:12.107 回答