Home > Net > After the Bind Socket client using the Tcp protocol asynchronous connection to exit the program cann
After the Bind Socket client using the Tcp protocol asynchronous connection to exit the program cann
Time:11-23
Public void ReadyConnect (string serverIPAddress, string clientIPAddress, int remoteport, int Localport) { Var serveripAddress=IPAddress. Parse (serveripAddress); Var clientipAddress=IPAddress. Parse (clientipAddress); TcpRemoteEndPoint=new IPEndPoint (serveripAddress, remoteport); TcpLocalEndPoint=new IPEndPoint (clientipAddress, Localport); _TcpSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp); _TcpSocket. Bind (tcpLocalEndPoint);
BeginConnect (); }
Public void BeginConnect () { If (_TcpSocket. Connected) { TcpProtocolUtils. ShutDownSocket (_TcpSocket); _TcpSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp); _TcpSocket. Bind (tcpLocalEndPoint); } TcpNetLogger. The Log (" Tcp started trying to connect... "); Var tcpConnectionSaea=new SocketAsyncEventArgs (); TcpConnectionSaea. RemoteEndPoint=tcpRemoteEndPoint; TcpConnectionSaea.Com pleted +=OnTcpConnectComplete; if (! _TcpSocket. ConnectAsync (tcpConnectionSaea)) { OnTcpConnectComplete (this, tcpConnectionSaea); } } Because the project need, before the code above is the client's connection to a local binding IP and port number, but use found, exit the program, I Socket. Shutdown (SocketShutdown. Both); Socket. The Close (); The two step operation, But the socket after the restart program. The error will be reported to AddressAlreadyInUse
CodePudding user response:
This code logic, didn't understand
CodePudding user response:
The same socket instance, at the same time do even remote server to monitor and client?
CodePudding user response:
I don't understand the c # network programming, but also can not have such SAO operation?
CodePudding user response:
The client to monitor? What is this operation? You this wrong view, AddressAlreadyInUse is refers to the port was taken, the connection is released, the port will not immediately be recycled, so will be out this wrong, you should do at the monitor end port multiplexing,