Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help Needed, with sockets and Ethernet stuff


  • Please log in to reply
4 replies to this topic

#1 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 24 November 2010 - 12:15 AM

Hello there,
I am working on my server class (for the N+) and I always get an exception with the following code:

        public byte[] Receive(int size)
        {
            byte[] result = null;

            if (allow)
                using (Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    socket1.Bind(new IPEndPoint(IPAddress.Any, _port));
                    socket1.Listen(10);

                    using (Socket socket2 = socket1.Accept())
                    {
                        int totalBytesReceived = 0;
                        int availableByteCount = 0;

                        do
                        {
                            availableByteCount = socket2.Available;

                            if (availableByteCount > 0)
                            {
                                totalBytesReceived += availableByteCount;

                                result = new byte[size];
                                int readByteCount = socket2.Receive(result, result.Length, SocketFlags.None);
                            }

                        } while (availableByteCount > 0);
                    }
                }

            return result;
        }
It happens after sending data a lot of times, or if I send it too fast.

The exception on N+'s side reads : "An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll"

On my C# Form app the exception reads: "An unhandled exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll"

Anyone know how I can fix it?

Thanks

#2 AlfredBr

AlfredBr

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationConnecticut, USA

Posted 24 November 2010 - 01:29 AM

Can you tell me more about this line?

int readByteCount = socket2.Receive(result, result.Length, SocketFlags.None);

It seems to me that socket2.Receive() will always try to read 'result.Length' bytes even if there is only 'availableByteCount' bytes remaining. Or is the second parameter the maximum number of bytes you are willing to receive?

#3 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 24 November 2010 - 02:32 AM

Can you tell me more about this line?

int readByteCount = socket2.Receive(result, result.Length, SocketFlags.None);

It seems to me that socket2.Receive() will always try to read 'result.Length' bytes even if there is only 'availableByteCount' bytes remaining. Or is the second parameter the maximum number of bytes you are willing to receive?

I think it is the amount that I am 'willing to receive' as you said, but I could be wrong I am not sure.

#4 Omar (OZ)

Omar (OZ)

    Advanced Member

  • Members
  • PipPipPip
  • 564 posts

Posted 24 November 2010 - 03:11 AM

An interesting addition to this... When I try to connect to the netduino using Tera Term it gives me the same error.

#5 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 27 November 2010 - 02:04 AM

It would help if you posted the stack trace of the exception. Taking a wild guess, I would say the problem is probably due to the fact that you are setting up a new socket to listen to the given port on every call to Receive(). When you do this quickly enough, you're probably getting an exception from the system that stems from your attempt to reuse a port before the operating system has had a chance to tear down the previous one. The code should probably be restructured so that socket1 stays around for the lifetime of your server, and socket2 stays around for the lifetime of your connection. Looking over it, I see several other issues with this code. You return as soon as socket2.Available==0, which is not the same as the sender being finished. Also, you only return the results of the last call to socket2.Receive(). You had probably intended that this routine should return the aggregate of all the data received rather than just the data from the last call to Receive(). Also you have another issue where the byte array returned is always of length "size", so your caller has no way of knowing how much data was actually received.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.