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,

CodePudding user response:

reference 4 floor ying1234 response:
client listening? 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,

In some cases may be both sides each other server client, so "the client to monitor" no problem, but he must be wrong code

CodePudding user response:

refer to stumble into a man of all ages 5 floor response:
Quote: refer to 4th floor ying1234 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,

In some cases may be both sides each other server client, so "the client to monitor" no problem, but he must be wrong code

My client not only Bind the monitoring, and because the server there is hardware program, they only want to be sure to caress the iP and port number, the equivalent of my TCP connection is 1 to 1, the server only listen to a client,

CodePudding user response:

refer to express it in 6 floor wood response:
Quote: refer to stumble into a man of all ages 5 floor response:

Quote: refer to 4th floor ying1234 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,

In some cases may be both sides each other server client, so "the client to monitor" no problem, but he must be wrong code

My client not only Bind the monitoring, and because the server there is hardware program, they only want to be sure to caress the iP and port number, the equivalent of my TCP connection is 1 to 1, the server only listen to a client,

Understand, do you want to use the client specified IP + port, to connect to the server, and not let the client operating system using random port to connect to the server, right?
So what is the purpose? The server will monitor your past port, he did not want the port is rejected?
I use netty done before, but c # socket if do it, you have to carefully research, because the bind is commonly used in the server start listening, you caused the problem may be the reason, the bind after taking up a port, and the connect, if the operating system and randomly assigned to a port?
  •  Tags:  
  • C#
  • Related