不要意思,調了好久,其它都弄好了,就查這個了,要驗收,急用。需求是:1.一個客戶端連接兩個固定IP掃碼設備,給一個demo,2.需要是c#的 3.需要斷電重連。
這是我做的類,但是覺得有好多問題。所以希望大神,不要看我的了,直接給個demo吧,萬謝。
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;
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 = "";
public SocketClient(string ip, 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); ;
this.isConnect = true;
}
catch
{
this.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 remarks
/********************************************************************************************
* 當Socket.Conneted為false時, 如果您需要確定連接的當前狀態,請進行非阻塞、零位元組的 Send 呼叫。
* 如果該呼叫成功回傳或引發 WAEWOULDBLOCK 錯誤代碼 (10035),則該套接字仍然處于連接狀態;
* 否則,該套接字不再處于連接狀態。
* Depending on http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.connected.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
********************************************************************************************/
#endregion
#region 程序
// This is how you can determine whether 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);
//Console.WriteLine("Connected!");
isConnect1 = true; //若Send錯誤會跳去執行catch體,而不會執行其try體里其之后的代碼
}
catch (SocketException e)
{
// 10035 == WSAEWOULDBLOCK
if (e.NativeErrorCode.Equals(10035))
{
//Console.WriteLine("Still Connected, but the Send would block");
isConnect1 = true;
}
else
{
//Console.WriteLine("Disconnected: error code {0}!", e.NativeErrorCode);
isConnect1 = false;
}
}
finally
{
theSocket.Blocking = blockingState;
}
}
//Console.WriteLine("Connected: {0}", client.Connected);
#endregion
}
private void AcceptMsg()
{
while (true)
{
Thread.Sleep(100);
try
{
byte[] bytes = new byte[13];
int bytesRec = theSocket.Receive(bytes);
revMsg = Encoding.ASCII.GetString(bytes, 0, bytesRec);
isConnect1 = true;
}
catch
{
isConnect1 = false;
}
}
}
private void checkState()
{
while (true)
{
Thread.Sleep(100);
try
{
if (((theSocket.Poll(20, SelectMode.SelectRead)) && (theSocket.Available == 0)) || (!theSocket.Connected))
isConnect1 = false;
}
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;
}
}
}
}
}
}
uj5u.com熱心網友回復:
需要uj5u.com熱心網友回復:
解決一堆多的問題,可以考慮使用多執行緒,也可以考慮I/O復用 ,select這樣異步方式uj5u.com熱心網友回復:
win就iocplinux就select /poll/epoll
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/133360.html
標籤:網絡通信
