Socket通讯
TCP连接
进行Socket数据通信要通过几个必要的步骤,首先进行TCP端到端的连接确认,确认连接成功后,发送相关的指令让服务器给客户端(也就是我们的电脑)返回我们所需要的数据集,其中会包含一些我们并不需要的数据,所以需要进行筛查操作 将我们所需要的目标数据给挑选出来以便客户端进行使用,结束时调用Close()断开连接。
Connect()
建立TCP连接
public bool Connect(string ipAddress, int port)
{
string ConnectString = "";
bool bRet = false;
try
{
if (_SK != null)
{
if (_SK.Connected)
{
_SK.Close();
}
_SK = null;
}
}
catch (Exception e)
{
}
try
{
_SK = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint tempRemoteIP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
EndPoint epTemp = (EndPoint)tempRemoteIP;
_SK.Connect(epTemp);
ThrWinsockListen = new Thread(new ThreadStart(ReceiveProc));
ThrWinsockListen.IsBackground = true;
ThrWinsockListen.Priority = ThreadPriority.AboveNormal;
ThrWinsockListen.Start();
sendThread = new Thread(new ThreadStart(SendDataProc));
sendThread.Start();
bRet = true;
}
catch (Exception e)
{
bRet = false;
}
return bRet;
}
IsConnected()
判断TCP是否已经成功连接
public bool IsConnected
{
get
{
if (this._SK != null)
{
return this._SK.Connected;
}
else
{
return false;
}
}
}
SendDataProc()
向服务器发送数据
private void SendDataProc()
{
bool keepalive = true;
Socket childClient = _SK;
int j = 0;
while (keepalive)
{
try
{
if (_SK == null || !_SK.Connected)
{
break;
}
if (QueryFlag)
{
string strbuffer = "GM,";
string sProID = "0,0";
strbuffer = strbuffer + sProID + "\r\n";
int len = strbuffer.Length;
Byte[] byteSend = new byte[len];
byteSend = System.Text.Encoding.Default.GetBytes(strbuffer);
int lengh = 0;
lock (byteSend)
{
for (int i = 0; i < 5; i++)
{
lengh = childClient.Send(byteSend);
Thread.Sleep(1);
}
}
if (lengh > 0)
{
JESQueryFlag = false;
}
else
{
SysLog.Info("发送数据失败");
break;
}
}
Thread.Sleep(1);
}
catch (SocketException se)
{
}
}
}
ReceiveProc()
从服务器接收数据
private void ReceiveProc()
{
bool keepalive = true;
Socket childClient = _SK;
while (keepalive)
{
try
{
if (_SK == null || !_SK.Connected)
{
break;
}
while (给一个控制是否开启接收的逻辑信号)
{
try
{
Byte[] byBuff1 = new Byte[1024];
childClient.Receive(byBuff1, byBuff1.Length, SocketFlags.None);
if (_SK == null || !_SK.Connected)
{
break;
}
string strBuff = Encoding.Default.GetString(byBuff1).TrimEnd('\0');
StringToDouble(strBuff);
}
catch (SocketException se)
{
SysLog.Error("Socket Error", se);
}
}
Thread.Sleep(1);
}
catch (SocketException se)
{
}
}
}
StringToDouble()
将接收到的字符串数据转换为double类型
public void StringToDouble(string strbuffer)
{
try
{
string[] strArr = strbuffer.Split(',');
double[] doubleArr = new double[strArr.Length];
for (int i = 0; i < strArr.Length; i++)
{
doubleArr[i] = Convert.ToDouble(strArr[i]);
}
}
List<System.Double> item = new List<System.Double>(doubleArr);
AppStatus.array.Add(item);
}
catch(Exception ex)
{
SysLog.Error("Error in StringToDouble:" + ex.ToString());
}
}
Close()
关闭TCP连接
public void Close()
{
try
{
if (_SK != null)
{
if (_SK.Connected)
{
_SK.Shutdown(SocketShutdown.Both);
Thread.Sleep(20);
_SK.Close();
}
_SK = null;
if (ThrWinsockListen != null)
{
for (int i = 0; i < 20; i++)
{
bool b1 = ThrWinsockListen.Join(1);
if (b1)
{
break;
}
}
if (ThrWinsockListen.IsAlive)
{
ThrWinsockListen.Abort();
}
}
if (sendThread != null)
{
for (int i = 0; i < 20; i++)
{
bool b1 = sendThread.Join(1);
if (b1)
{
break;
}
}
if (sendThread.IsAlive)
{
sendThread.Abort();
}
}
ThrWinsockListen = null;
sendThread = null;
}
}
catch (Exception e)
{
Socket线程退出
}
finally
{
_SK = null;
ThrWinsockListen = null;
sendThread = null;
}
}
|