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