Been following (http://codeidol.com/csharp/csharp-network/Asynchronous-Sockets/Using-Asynchronous-Sockets/) to help me learn the ins and outs of Asynchronous communcation.

However, when I pass my "Server" socket to my callback function and when it gets there it says its disconnected. My BeginConnect and EndConnect work ok so far.

Below is the appropriate code. What do I do? I get an error of "System.ComponentModel and error code 10042" where the SocketErrorCode was "protocol option" and "'server.EnableBroadcast' threw an exception of type 'System.Net.Sockets.SocketException'"
and "An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call""

What should I do?

Thanks for the help!

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(serverToBind), portToBind);

            try
            {
                sock.Bind(iep);
                sock.Listen(5);
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            
           
           // sock.BeginAccept(new AsyncCallback(CallAccept), sock);

            Console.WriteLine("Started");

            while (true)
            {
                sock.BeginAccept(new AsyncCallback(CallAccept), sock);
            }
        }

       

        private static void CallAccept(IAsyncResult iar)
        {
            //Console.WriteLine("\t --> Client Connected! <--");
            Socket server = null;
            Socket client = null;

            try
            {
                 server = (Socket)iar.AsyncState;
                 client = server.EndAccept(iar);
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        

            Console.WriteLine("\t --> Client Connected! <--");

            //Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 57422);
            //sock.Bind(iep);
            //sock.Listen(5);

            server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ProcessInitialResult), server);

            Console.WriteLine("Now attempting to receive data");

        }

Recommended Answers

All 4 Replies

When you call BeginAccept() you're passing the master socket to it as the socket that will get the new connection request. You're essentially overwriting your master socket after the first connection. Create a slave socket for every connection. That may solve your problem.
Could also be that the other end is closing the connection.

I threw your while loops inside the try statement.
it would also be better if you blocked until a client connects, this would relieve some system resources, but it doesn't seem like this is that big of a project.

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(serverToBind), portToBind);
            try
            {
                sock.Bind(iep);
                sock.Listen(5);
                while (true)
                {
                    sock.BeginAccept(new AsyncCallback(CallAccept), sock);
                    Console.WriteLine("Waiting for Client Connection");
                }
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }       

        private static void CallAccept(IAsyncResult iar)
        {
            //Console.WriteLine("\t --> Client Connected! <--");
            Socket workSocket = (Socket)iar.AsyncState;  // This sets the current iar socket to a worksocket
            Socket server = workSocket.EndAccept(iar); 

            try
            {
                server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ProcessInitialResult), server);
                 Console.WriteLine("\t --> Client Connected! <--");
                 Console.WriteLine("Now attempting to receive data");
            }
            catch (System.Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

I hope this helps...

Hi,

As a follow up to th same thread. I have a code that hardcoded the port number, whereas I need the port number to be dynamically selected (random number). Can anybody have a quick look please and send me any suggestions:

Cheers,
Billy

the peace of code:
IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback,5555)

create 2 variables One for the IPAddress to connect to, and a variable for the port number. As to how you do that it's up to you.

/// overwrite these numbers to change which IP and port you connect to.
int portNumber = 5555;
string Connection = "127.0.0.1";
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(connection), port);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.