Home > other >  Under 100 points, a socket, a client connect to multiple servers, need to use multiple threads or as
Under 100 points, a socket, a client connect to multiple servers, need to use multiple threads or as

Time:10-22

Don't mean, adjustable for a long time, the other are fixed, to check this, to acceptance, urgent need, demand is: 1. A client connection two fixed IP code equipment, give a demo, 2. The need is c # 3. Need power reconnection,

This is what I do, but think there are many problems, so I hope the great god, don't look at me, give a demo directly, thanks,
using System;
using System.Collections.Generic;
using System.Linq;
Using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Using System.Net;

The namespace PLCWF
{
Public class SocketClient
{
Private TcpClient client;
Public bool isConnect=false;
Public bool isConnect1=true;

Public Socket theSocket=null;
Private string remoteHost="192.168.60.101";
Private int remotePort=10000;

Private Thread checkState_th;
Private Thread checkState_th1;
Private Thread client_th;
Private Thread reconnect_th;
Public string revMsg="";


IP public SocketClient (string, int port)
{
IPAddress IPAddress=IPAddress. Parse (IP);
IPEndPoint remoteEP=new IPEndPoint (ipAddress, port);
TheSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
Try
{
TheSocket. Connect (remoteEP); ;
Enclosing isConnect=true;
}
Catch
{
Enclosing isConnect=false;
}

Client_th=new Thread (new ThreadStart (AcceptMsg));
Client_th. IsBackground=true;
Client_th. Start ();

CheckState_th=new Thread (new ThreadStart (checkState));
CheckState_th. IsBackground=true;
CheckState_th. Start ();

CheckState_th1=new Thread (new ThreadStart (IsSocketConnected));
CheckState_th1. IsBackground=true;
CheckState_th1. Start ();

Reconnect_th=new Thread (new ThreadStart (reconnect));
Reconnect_th. IsBackground=true;
Reconnect_th. Start ();
}

Private void IsSocketConnected ()
{
Thread.sleep (100);
# region few
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* when Socket Conneted to false, if you need to determine the current state of the connection, please non-blocking, Send zero bytes,
* if the call succeeds to return or trigger WAEWOULDBLOCK error code (10035), was still is the socket connection state;
* otherwise, no longer in the socket connection state,
* Depending on http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.connected.aspx? Cs - save - lang=1 & amp; Cs - lang=csharp# code snippet - 2
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# endregion

# region process
//This is how you can determine been a socket is still connected.
While (true)
{
Thread.sleep (100);
Bool blockingState=theSocket. Blocking;
Try
{
Byte [] TMP=new byte [1].

TheSocket. Blocking=false;
Int a=theSocket. Send (TMP, 1, 0).
//the Console. WriteLine (" Connected!" );
IsConnect1=true;//if the Send error would jump to carry out the catch, and will not perform its after the code in the try
}
The catch (SocketException e)
{
//10035==WSAEWOULDBLOCK
If (e.N ativeErrorCode. Equals (10035))
{
//the Console. WriteLine (" Still Connected, but the Send order to block ");
IsConnect1=true;
}

The else
{
//the Console. WriteLine (" Disconnected: the error code {0}!" , e.N ativeErrorCode);
IsConnect1=false;
}
}
The finally
{
TheSocket. Blocking=blockingState;
}
}
//the Console. WriteLine (" Connected: {0} ", the client Connected);
# endregion
}

Private void AcceptMsg ()
{
While (true)
{
Thread.sleep (100);
Try
{

Byte [] bytes=new byte [13].
Int bytesRec=theSocket. The Receive (bytes);
RevMsg=Encoding. The ASCII. Get string (bytes, 0, bytesRec);
IsConnect1=true;
}
Catch
{
IsConnect1=false;
}
}
}


Private void checkState ()
{
While (true)
{
Thread.sleep (100);
Try
{
If (((theSocket Poll (20, SelectMode. SelectRead)) & amp; & (theSocket. The Available==0)) | | (! TheSocket. Connected))
IsConnect1=false;
}
The catch (SocketException)
{
IsConnect1=false;
}

}
}


Private void reconnect ()
{
While (true)
{
Thread.sleep (100);
If (isConnect1==false)
{
Try
{
TheSocket. Disconnect (true);
TheSocket. Close ();
IPAddress IPAddress=IPAddress. Parse (remoteHost);
IPEndPoint remoteEP=new IPEndPoint (ipAddress, remotePort);
TheSocket=new Socket (AddressFamily. InterNetwork, SocketType Stream, ProtocolType. Tcp);
TheSocket. Connect (remoteEP); ;
IsConnect=true;
}
Catch
{
IsConnect=false;
}

}
}
}

}
}

CodePudding user response:

to

CodePudding user response:

Solve the problem of more than a heap of, can consider to use multithreading, also can consider to I/O multiplexing, select this asynchronously

CodePudding user response:

Win is iocp
Linux is the select/poll/epoll
  • Related