The development of a C/s project involves the problem of regular reconnection of the client after the server is stopped. During the development process, an error is reported as follows:
After the socket is disconnected, it can only be reconnected asynchronously, and can only be connected to different endpoints. Beginconnect must be called on the thread that will not exit before the operation is completed.
After searching for relevant information on the Internet, the problem was not solved. A whim occurred and the universal list type was used to solve the problem. The methods are as follows:
Add global variables first
Private _Socket As New List(Of Socket)
Add several more in the process of connection_ Socket.Clear():
Private Sub ConnectServer()
_Socket.Clear()
Dim SockClient As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
_Socket.Add(SockClient)
Dim Address As IPAddress = IPAddress.Parse(_ServerIPString)
Dim Endpoint As New IPEndPoint(Address, _ServerPort)
Try 'Catching exceptions
_Socket(0).Connect(Endpoint) 'initiate the connection, where the first index in the List is used instead of the local SockClient
Catch ss As SocketException
Dim strErrorMsg As String = ss.SocketErrorCode.ToString
strErrorMsg = strErrorMsg.Replace("TimedOut", "Connection timeout!")
strErrorMsg = strErrorMsg.Replace("ConnectionRefused", "Connection rejected!")
If SockClient.Connected Then SockClient.Close()
_Socket.Clear()
MessageBox.Show(strErrorMsg, "Connection exception", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
Catch s As Exception
If SockClient.Connected Then SockClient.Close()
_Socket.Clear()
MessageBox.Show(s.Message, "exception", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End Try
End Sub
be accomplished
Test results:
After repeatedly disconnecting the server, call the connectserver process again to connect again. The connection is successful and no error is reported.
Read More:
- socket java.net.BindException: bind failed: EACCES (Permission denied)
- Caused by: java.net.SocketException: Software caused connection abort: socket write error
- [Solved] net core HTTP Error 500.31 – Failed to load ASP.NET Core runtime HTTP Error 500.30 – ASP.NET Core
- [Solved] Compile the program error: undefined reference to `cv::dnn::dnn4_v20210301::Net::~Net()‘
- [Solved] Asp.Net Core IIS Error: HTTP Error 500.30 – ASP.NET Core app failed to start
- package golang.org/x/net/XXX: unrecognized import path “golang.org/x/net/xxx“…
- [Solved] Docker Error: driver failed programming external connectivity on endpoint
- [Solved] Docker: Error response from daemon: driver failed programming external connectivity on endpoint mysq
- [Solved] docker: Error response from daemon: driver failed programming external connectivity on endpoint mysql-test …
- Bin File Programming Failed, could not find core in coresight setup, erase error
- [Solved] Disconnected from the target VM, address : ‘127.0.0.1:6847’ , transport : ‘socket’
- C++Qt development – Qtsocket programming error: undefined reference to `_imp__WSAStartup
- [Solved] ERROR: for jms_koko Cannot start service koko: driver failed programming
- [Solved] Docker Start nginx error: driver failed programming external connectivity on endpoint nginx
- [Solved] Docker Error: Error response from daemon: driver failed programming external connectivity on endpoint
- Net use Errors Explanation
- Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen is reported when docker port mapping or starting the container
- runtime error ‘9’: subscript out of range error in VBA programming
- Solution of socket write error caused by pressing F5 to refresh page by ehcache user
- [Solved] Redis Error: Creating Server TCP listening socket *:6379: bind: No error