Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. vSystem.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer,Int32 offset,Int32 size,SocketFlags socketflags,Endpoint& remoteEP) [modified]

vSystem.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer,Int32 offset,Int32 size,SocketFlags socketflags,Endpoint& remoteEP) [modified]

Scheduled Pinned Locked Moved C#
csharpsysadminhelptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrithula8
    wrote on last edited by
    #1

    Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,

    int recv = server.ReceiveFrom(data, ref tmpRemote);

    I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,

    IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
    EndPoint tmpRemote = (EndPoint)sender;

    Any ideas why it throws the exception and how to solve it?

    modified on Thursday, March 12, 2009 1:02 AM

    J M 2 Replies Last reply
    0
    • M mrithula8

      Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,

      int recv = server.ReceiveFrom(data, ref tmpRemote);

      I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,

      IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
      EndPoint tmpRemote = (EndPoint)sender;

      Any ideas why it throws the exception and how to solve it?

      modified on Thursday, March 12, 2009 1:02 AM

      J Offline
      J Offline
      Jimmanuel
      wrote on last edited by
      #2

      mrithula8 wrote:

      Any ideas why it throws the exception and how to solve it?

      Nope. That info is contained in the SocketException object that gets thrown. You need to catch it and either print it or use the debugger to view it and find out what's going on.

      M 1 Reply Last reply
      0
      • M mrithula8

        Hi, I'm having trouble getting the UDPClient to work with C#. The udpClient has connected and sent a signal to 127.0.0.1 on port 8050 with no exceptions thrown. On the below line of code,

        int recv = server.ReceiveFrom(data, ref tmpRemote);

        I receive the below exception error, at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint& remoteEP) RemoteIPEndPoint is defined using the following line of code,

        IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8050);
        EndPoint tmpRemote = (EndPoint)sender;

        Any ideas why it throws the exception and how to solve it?

        modified on Thursday, March 12, 2009 1:02 AM

        M Offline
        M Offline
        mcldev
        wrote on last edited by
        #3

        Your exception is probably something to do with the buffer, offset, and size. here are some guidelines that may help you understand and solve your problem: The "buffer" must be large enough to accomodate the largest UDP datagram you expect to receive. Make sure you have a buffer that is large enough for you application. For example byte[] buffer = new buffer[1024]; Next, "offset" is the index to the buffer. First recv this is probably zero (0). Next recv it may be advanced by the value from the datagram PDU, e.g. a size of payload. The key here is offet MUST be with the bounds of the buffer 0 to n. You should advance the offset by the bytes recv'd like this (with error checking of ocurse):

        int nbytes = recv(buffer, offset, size, ...);
        offset += nbytes

        "Size" is determines how much. Let's say your first read got a 12 byte Header and in this header you extract a length. Well that means you would have used 12 for the first recv, and the if the payload size from the Header was 146 bytes, then your second recv would be for that amount. BUT you must make sure the offset advances before the second recv. Make sure you process recv errors, such as:

        int nbytes = recv(buffer, offset, size, ...);

        if (nbytes > 0)
        offset += nbytes;
        else
        ProcessSocketError();

        Good luck

        1 Reply Last reply
        0
        • J Jimmanuel

          mrithula8 wrote:

          Any ideas why it throws the exception and how to solve it?

          Nope. That info is contained in the SocketException object that gets thrown. You need to catch it and either print it or use the debugger to view it and find out what's going on.

          M Offline
          M Offline
          mrithula8
          wrote on last edited by
          #4

          Hi, I used the SocketException to catch the exception It displays the exception that the existing connection was forcibly closed by the remote host.How do i find where the connection is getting closed?Can you give some suggestions //client

          private void SendMessage()
          {
          try
          {
          listBox6.Items.Add("Connecting....");
          IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
          Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
          sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
          listBox6.Items.Add("Connected");
          byte[] data = new byte[1024];
          String snd = "hello";
          data = Encoding.ASCII.GetBytes(snd);
          listBox6.Items.Add("Transmitting...");
          sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello
          listBox6.Items.Add("Sent...");
          EndPoint tmpRemote = (EndPoint)ipep;
          listBox6.Items.Add("Message received from {0}:");
          listBox6.Items.Add(ipep.ToString());
          data = new byte[1024];
          int recv = sock.ReceiveFrom(data,0,data.Length, SocketFlags.None, ref tmpRemote);
          String zz=Encoding.ASCII.GetString(data, 0, recv);
          listBox6.Items.Add(zz);
          if (zz == "Welcome to the Server")
          {
          lb9.Text = "Active";
          lb9.BackColor = Color.Green;
          }
          else{
          lb9.Text = "Inactive";
          lb9.BackColor=Color.Red;
          }
          }

                  catch (SocketException e)
                  {
                      //Console.WriteLine("Error..... " + e.StackTrace);
                      MessageBox.Show(e.Message);
          
                  }
              }
          

          //server

          public static void start_server()
          {
          IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
          Socket newsock = new Socket(AddressFamily.InterNetwork , SocketType.Dgram, ProtocolType.Udp);
          newsock.Bind(ipep);
          Console.WriteLine("Waiting for a client...");
          while (true)
          {
          try
          {
          EndPoint tmpRemote = (EndPoint)ipep;
          byte[] data = new byte[1024];
          Console.WriteLine("hai");
          int

          J 1 Reply Last reply
          0
          • M mrithula8

            Hi, I used the SocketException to catch the exception It displays the exception that the existing connection was forcibly closed by the remote host.How do i find where the connection is getting closed?Can you give some suggestions //client

            private void SendMessage()
            {
            try
            {
            listBox6.Items.Add("Connecting....");
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, 1);
            listBox6.Items.Add("Connected");
            byte[] data = new byte[1024];
            String snd = "hello";
            data = Encoding.ASCII.GetBytes(snd);
            listBox6.Items.Add("Transmitting...");
            sock.SendTo(data, 0,data.Length, SocketFlags.None, ipep);//sent hello
            listBox6.Items.Add("Sent...");
            EndPoint tmpRemote = (EndPoint)ipep;
            listBox6.Items.Add("Message received from {0}:");
            listBox6.Items.Add(ipep.ToString());
            data = new byte[1024];
            int recv = sock.ReceiveFrom(data,0,data.Length, SocketFlags.None, ref tmpRemote);
            String zz=Encoding.ASCII.GetString(data, 0, recv);
            listBox6.Items.Add(zz);
            if (zz == "Welcome to the Server")
            {
            lb9.Text = "Active";
            lb9.BackColor = Color.Green;
            }
            else{
            lb9.Text = "Inactive";
            lb9.BackColor=Color.Red;
            }
            }

                    catch (SocketException e)
                    {
                        //Console.WriteLine("Error..... " + e.StackTrace);
                        MessageBox.Show(e.Message);
            
                    }
                }
            

            //server

            public static void start_server()
            {
            IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8050);
            Socket newsock = new Socket(AddressFamily.InterNetwork , SocketType.Dgram, ProtocolType.Udp);
            newsock.Bind(ipep);
            Console.WriteLine("Waiting for a client...");
            while (true)
            {
            try
            {
            EndPoint tmpRemote = (EndPoint)ipep;
            byte[] data = new byte[1024];
            Console.WriteLine("hai");
            int

            J Offline
            J Offline
            Jimmanuel
            wrote on last edited by
            #5

            MSDN says that you have to call Socket.Bind(...) before you call Socket.ReceiveFrom(...). I see where you're doing that in your server code but not your client. I'd try adding a call to that function to the client. If you still have lots of problems getting Sockets to work then I'd suggest you take a look at the UdpClient[^] class. It simplifies a lot of things and makes sockets easier to work with.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups