嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元微信扫码支付:1 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
Tcp网络通信协议类源码可生成类库 直接调用可用
客户端核心代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace Xpwy.Backup.PTcpHelper
{
public class TcpClientHelper:IDisposable
{
TcpClient client;
NetworkStream netstream;
string _serverip = "127.0.0.1";
int _port = 8080;
TcpCommon tcpCommon = new TcpCommon();
#region TcpClientHelper constructor
public TcpClientHelper(string strServerIP, int serverPort)
{
_serverip = strServerIP;
_port = serverPort;
}
#endregion
public void Start()
{
client = new TcpClient(_serverip, _port);
netstream = client.GetStream();
}
public void Stop()
{
if (netstream != null)
{
netstream.Close();
}
if (client != null)
{
client.Close();
}
}
#region TcpCommon所有方法
public string CalcFileHash(string FilePath)
{
return tcpCommon.CalcFileHash(FilePath);
}
public bool SendFile(string filePath)
{
return tcpCommon.SendFile(filePath, netstream);
}
public bool ReceiveFile(string filePath)
{
return tcpCommon.ReceiveFile(filePath, netstream);
}
public bool SendMessage(string message)
{
return tcpCommon.SendMessage(message, netstream);
}
public string ReadMessage()
{
return tcpCommon.ReadMessage(netstream);
}
#endregion
#region IDisposable 成员
public void Dispose()
{
if (netstream != null)
{
netstream.Close();
}
if (client != null)
{
client.Close();
}
}
#endregion
}
}
服务端代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace Xpwy.Backup.PTcpHelper
{
public class TcpListenerHelper
{
private string _strServerIP = "";
private int _serverPort = 0;
TcpListener server;
TcpClient client;
NetworkStream netstream;
IAsyncResult asyncResult;
TcpCommon tcpCommon = new TcpCommon();
ManualResetEvent listenConnected = new ManualResetEvent(false);
bool _active = false;
public TcpListenerHelper(string strServerIP, int serverPort)
{
_strServerIP = strServerIP;
_serverPort = serverPort;
server = new TcpListener(IPAddress.Parse(strServerIP), serverPort);
server.Server.ReceiveTimeout = 6000;
server.Server.SendTimeout = 6000;
}
/// <summary>
/// 启动
/// </summary>
public void Start()
{
try
{
_active = true;
server.Start();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 停止
/// </summary>
public void Stop()
{
try
{
_active = false;
if (client != null)
{
client.Close();
}
if (netstream != null)
{
netstream.Close();
}
server.Stop();
}
catch (Exception ex)
{
throw ex;
}
}
public void Listen()
{
listenConnected.Reset();
asyncResult = server.BeginAcceptTcpClient(new AsyncCallback(AsyncCall), server);
}
public void AsyncCall(IAsyncResult ar)
{
try
{
TcpListener tlistener = (TcpListener)ar.AsyncState;
if (_active)
{
client = tlistener.EndAcceptTcpClient(ar);
netstream = client.GetStream();
}
else
{
client = null;
netstream = null;
}
listenConnected.Set();
}
catch (Exception ex)
{
throw ex;
}
}
public bool WaitForConnect()
{
listenConnected.WaitOne();
if (client != null && netstream != null)
{
return true;
}
else
{
return false;
}
}
#region TcpCommon所有方法
/// <summary>
/// 计算文件的hash值
/// </summary>
public string CalcFileHash(string FilePath)
{
return tcpCommon.CalcFileHash(FilePath);
}
/// <summary>
/// 发送文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool SendFile(string filePath)
{
return tcpCommon.SendFile(filePath, netstream);
}
/// <summary>
/// 接收文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public bool ReceiveFile(string filePath)
{
return tcpCommon.ReceiveFile(filePath, netstream);
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
public bool SendMessage(string message)
{
return tcpCommon.SendMessage(message, netstream);
}
/// <summary>
/// 接收消息
/// </summary>
/// <returns></returns>
public string ReadMessage()
{
return tcpCommon.ReadMessage(netstream);
}
#endregion
#region IDisposable 成员
public void Dispose()
{
Stop();
}
#endregion
}
}