Hello!
I have a program in c# in which i create a socket, binding it, begin listening and then using beginaccept! but then when I try to close\shutdown the socket I get exceptions from the beginaccept AsyncCallback method!
private void start_listening()
{
main_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint iplocal = new IPEndPoint(IPAddress.Any, 11150);
main_socket.Bind(iplocal);
main_socket.Listen(5);
main_socket.BeginAccept(new AsyncCallback(OnClientConnect), null);
}
private void Disconnect_Click(object sender, EventArgs e)
{
main_socket.Shutdown(SocketShutdown.Both);
main_socket.Close();
}
public void OnClientConnect(IAsyncResult asyn)
{
try
{
clients[connected_clients] = new Client("CHANGEME", "127.0.0.1", this);
clients[connected_clients].Socket = main_socket.EndAccept(asyn);
clients[connected_clients].WaitForData();
main_socket.BeginAccept(OnClientConnect, null);
}
catch (SocketException se)
{
MessageBox.Show(se.Message);
}
}
many thanks!