hrza
hrza

Reputation: 85

UDP client bug - Unable to send data bytes

I am writting a C# P2P Video Chat (Part of my exam on Faculty),a i am little stuck at send Data via udp. So here how it works. I have a Web_Capture library, and every time image is captured it sets PictureBox image to the captured one

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

So then the sendData method starts sending...

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);

        }  

When i debug, the socket exception pops out:

 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

So, does anyone have any idea, i will be thankfull if he support that idea with code, because i am nooby at c# :) thanks in advance. Marjan

Upvotes: 0

Views: 1005

Answers (1)

Mehran
Mehran

Reputation: 2037

You should specify reciever's ip and port, Here is a complete example

so you should change your implementation to

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);
}

Upvotes: 1

Related Questions