using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using TechSysWCS.Model;
using System.Threading;
namespace TechSysWCS.Helper
{
class TcpClientHelper
{
public TcpClientHelper(string strIP, int nPort)
{
m_strIP = strIP;
m_nPort = nPort;
m_buffRece = new byte[1024];
m_enumRunFlag = enumRunFlag.Init;
Task task = new Task(runTask);
task.Start();
}
/// <summary>
/// 释放对象
/// </summary>
public void Destroy()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
m_tcpClient.Close();
}
m_tcpClient = null;
m_enumRunFlag = enumRunFlag.End;
Thread.Sleep(100);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="strMsg"></param>
public void setSendMsg(byte[] buffSend)
{
this.m_buffSend = buffSend;
if (m_enumRunFlag == enumRunFlag.Sleep)
{
m_enumRunFlag = enumRunFlag.Send;
}
}
/// <summary>
/// 获取消息
/// </summary>
/// <returns></returns>
public byte[] getReceMsg()
{
return m_buffRece;
}
/// <summary>
/// 异步连接请求
/// </summary>
private async void Connect()
{
if (m_tcpClient == null)
{
m_tcpClient = new TcpClient();
try
{
await m_tcpClient.ConnectAsync(m_strIP, m_nPort);
}
catch (Exception ex)
{
Console.WriteLine("Connect Exception: {0}", ex.Message);
Close();
}
}
}
/// <summary>
/// 发送消息
/// </summary>
private async void sendMsg(byte[] byteData)
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
try
{
NetworkStream stream = m_tcpClient.GetStream();
await stream.WriteAsync(byteData, 0, byteData.Length);
}
catch (Exception ex)
{
Console.WriteLine("sendMsg Exception: {0}", ex.Message);
Close();
}
}
else
{
m_enumRunFlag = enumRunFlag.Init;
}
}
/// <summary>
/// 接收消息
/// </summary>
private async void ReceMsg()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
Array.Clear(m_buffRece, 0, m_buffRece.Length);
m_nReceLen = 0;
try
{
NetworkStream stream = m_tcpClient.GetStream();
m_nReceLen = await stream.ReadAsync(m_buffRece, 0, m_buffRece.Length);
}
catch (Exception ex)
{
Console.WriteLine("ReceMsg Exception: {0}", ex.Message);
Close();
}
}
else
{
m_enumRunFlag = enumRunFlag.Init;
}
}
/// <summary>
/// 异步执行任务
/// </summary>
private void runTask()
{
while (m_enumRunFlag != enumRunFlag.End)
{
switch (m_enumRunFlag)
{
case enumRunFlag.Init:
{
m_enumRunFlag = enumRunFlag.Sleep;
Connect();
break;
}
case enumRunFlag.Send:
{
m_enumRunFlag = enumRunFlag.Rece;
sendMsg(m_buffSend);
break;
}
case enumRunFlag.Rece:
{
m_enumRunFlag = enumRunFlag.ReceResult;
ReceMsg();
break;
}
case enumRunFlag.ReceResult:
{
if (m_nReceLen > 0)
{
m_enumRunFlag = enumRunFlag.Sleep;
Console.Out.WriteLine("ReciveLen:{0}", m_nReceLen);
}
else
{
m_enumRunFlag = enumRunFlag.ReceResult;
}
break;
}
case enumRunFlag.Sleep:
{
Thread.Sleep(1000);
break;
}
default:
{
break;
}
}
Thread.Sleep(100);
}
}
/// <summary>
///关闭连接
/// </summary>
private void Close()
{
if (m_tcpClient != null && m_tcpClient.Connected)
{
m_tcpClient.Close();
}
m_tcpClient = null;
m_enumRunFlag = enumRunFlag.Init;
}
public enum enumRunFlag
{
Init = 0,
Send = 1,
Rece = 2,
ReceResult = 4,
Sleep = 5,
End = -1,
}
public enumRunFlag getRunFlag()
{
return m_enumRunFlag;
}
private TcpClient m_tcpClient;
private string m_strIP;
private int m_nPort;
private byte[] m_buffRece;
private byte[] m_buffSend;
private enumRunFlag m_enumRunFlag;
public int m_nReceLen{get; private set;}
}
}
C#简单的TcpClient
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 中国人很喜欢打听别人的收入,收入对于我们这个社会传统来说并不算隐私。对于不是一个专业的人(如亲戚)我还是会说个大概...