I create socks 5. During the processing of the link, an error appears.

class StateObject { public Socket workSocket = null; //сокет клиента public const int bufferSize = 64; //max размер буфера public byte[] buffer = new byte[bufferSize];//буфер public string sb = null;//принятные данные в виде строки } class server { StateObject stateGL = new StateObject(); private Socket socket_client; private IPEndPoint ip; public server() { try { IPAddress localAddress = IPAddress.Parse("127.0.0.1"); Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipEndpoint = new IPEndPoint(localAddress, 2200); listenSocket.Bind(ipEndpoint); listenSocket.Listen(1); listenSocket.BeginAccept(new AsyncCallback(ReceiveCallback), listenSocket);listenSocket.LocalEndPoint); while (true) { } } catch (Exception e) { Console.WriteLine("Произошла ошибка: {0}", e.ToString()); Console.ReadKey(); } } public void ReceiveCallback(IAsyncResult AsyncCall) { System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] messageRecv = new byte[64]; string Recv = null; Socket listener = (Socket)AsyncCall.AsyncState; Socket client = listener.EndAccept(AsyncCall); //браузер while (true) { int length = client.Receive(messageRecv); Recv = Recv + Encoding.UTF8.GetString(messageRecv, 0,length); if (length < 64) { stateGL.sb = Recv; break; } } //начало запросов clientConnectServer("127.0.0.10", 2200); Console.WriteLine("Закрытие соединения"); client.Close(); listener.BeginAccept(new AsyncCallback(ReceiveCallback), listener); } //создание сокета клиента public void clientConnectServer(string ip, int port) { this.ip = new IPEndPoint(IPAddress.Parse(ip), port); this.socket_client = new Socket(this.ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); Connect(); } //подключение к серверу public void Connect() { try { this.socket_client.BeginConnect(this.ip, new AsyncCallback(ConnectCallBack), this.socket_client); } catch (Exception x) { Console.WriteLine("Error connect server1"); Console.ReadKey(); } } //метод для завершения операции private void ConnectCallBack(IAsyncResult ar) { Socket handle = (Socket)ar.AsyncState; try { byte[] message = { 5, 1, 2 }; //привествие Send(message, message.Length); this.socket_client.EndConnect(ar); } catch (Exception e) { Console.WriteLine("2" + e.Message); Console.ReadKey(); this.socket_client.EndConnect(ar); } } //отправка сообщения public void Send(byte[] message, int length) { try { this.socket_client.BeginSend(message, 0, length, SocketFlags.None, new AsyncCallback(SendCallBack), this.socket_client); } catch (Exception x) { Console.WriteLine("Error connect server2"); Console.ReadKey(); } } private void SendCallBack(IAsyncResult ar) { try { Socket handle = ar.AsyncState as Socket; handle.EndSend(ar); } catch (Exception) { Console.WriteLine("SendCallBack"); } Receive(); } public void Receive() { StateObject state = new StateObject(); try { state.workSocket = this.socket_client; state.workSocket.BeginReceive(state.buffer, 0, StateObject.bufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallBack), state); }catch(Exception e) { Console.WriteLine("1" + e.Message); Console.ReadKey(); state.workSocket.EndReceive(null); } } private void ReceiveCallBack(IAsyncResult ar) { try { StateObject state = ar.AsyncState as StateObject; Socket handl = state.workSocket; if (handl.Connected) { int bytes = handl.EndReceive(ar); if (bytes > 0) { if (state.buffer[0] == 5 && state.buffer[1] == 2) Console.WriteLine("Верное привествие"); ip4_6_nameHost(); } } } catch (Exception) { Console.WriteLine("Error receive"); Console.ReadKey(); } } private void ip4_6_nameHost() { byte[] privestrieNext = new byte[64]; try { Send(privestrieNext, 5 + name.Length + Port.Length); } catch (Exception x) { Console.WriteLine("IPIPIP"); Console.ReadKey(); } } } 

}

The exception is thrown on this line:

 catch (Exception e) { Console.WriteLine("2" + e.Message); Console.ReadKey(); this.socket_client.EndConnect(ar); } 
 Объект IAsyncResult не был возвращен соответствующим асинхронным 

method of this class

What can be wrong?

  • Uh ... Too much code. Try to shorten the program to the absolutely necessary minimum that reproduces the problem. For example, the functions proverka and portName certainly not important. - VladD
  • reduced Left the most important thing - Vla00
  • @ Vla00: Hmm. What does while (true) { } do? You hang the flow. - VladD
  • if i take away this cycle. Then the program will close immediately upon startup - Vla00
  • one
    @ Vla00, you have an infinite loop in the constructor. You can't even create a class object. - Veikedo

1 answer 1

See it.

You have EndConnect be called on the same socket_client as BeginConnect . And during this time your socket_client may change if clientConnectServer is called again. (This is actually the error message.)

So you need in ConnectCallBack use a handle instead of this.socket_client .

However, it will not go anyway: you have an infinite loop in the constructor, the program will not work correctly. Try switching from outdated, but rather complex constructions with the BeginXX / EndXX / IAsyncResult to the modern async / await interface. Here is an example .