C# TCP 服务器客户端类代码解析
这段代码是一个 C# 语言编写的 TCP 服务器的客户端类。它维护一个 Socket 对象,用于连接到服务器,并提供方法来启动接收和发送数据。它还包括一些事件,用于通知客户端已经收到数据、发送数据已经完成或连接已经关闭。此外,它还包括一些辅助方法,如关闭 Socket 和转换 IP 地址。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
namespace TcpServerSocket
{
public class SocketClient
{
private Guid mClientGuid;
private ClosedEventHandler mCloseEventHandler;
private byte[] mReceiveBuffer;
private ReceiveDataEventHandler mReceiveDataEventHandler;
private SendDataEventHandler mSendDataEventHandler;
private StringBuilder mStringBuilder;
private Socket mWorkSocket;
public event ReceiveDataEventHandler ReceiveDataFinish;
public event SendDataEventHandler SendDataFinish;
public event ClosedEventHandler SocketClosed;
public SocketClient(Socket iSocket)
{
this.mWorkSocket = iSocket;
this.mClientGuid = Guid.NewGuid();
}
public void CloseSocket()
{
this.CloseSocket(new Exception('服务器关闭客户端连接'));
}
private void CloseSocket(Exception iRException)
{
try
{
if (this.mWorkSocket.Connected)
{
this.mWorkSocket.Disconnect(true);
}
if (this.mCloseEventHandler != null)
{
this.mCloseEventHandler(this, iRException);
}
}
catch (Exception exception)
{
if (this.mCloseEventHandler != null)
{
this.mCloseEventHandler(this, exception);
}
}
}
private void ReceiveCallback(IAsyncResult iIAsyncResult)
{
try
{
int length = this.mWorkSocket.EndReceive(iIAsyncResult);
if (length == 0)
{
this.CloseSocket(new Exception('客户端断开连接'));
}
else
{
if (this.mReceiveDataEventHandler != null)
{
byte[] destinationArray = new byte[length];
Array.Copy(this.mReceiveBuffer, 0, destinationArray, 0, length);
this.mReceiveDataEventHandler(this, destinationArray);
}
this.mWorkSocket.BeginReceive(this.mReceiveBuffer, 0, this.mReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), this);
}
}
catch (Exception exception)
{
this.CloseSocket(exception);
}
}
private void SendDataCallBack(IAsyncResult iIAsyncResult)
{
try
{
int lenth = this.mWorkSocket.EndSend(iIAsyncResult);
if (this.mSendDataEventHandler != null)
{
this.mSendDataEventHandler(this, lenth);
}
}
catch (Exception exception)
{
this.CloseSocket(exception);
}
}
public void StartReceive()
{
try
{
if (this.mWorkSocket == null)
{
throw new ArgumentNullException('客户端不存在');
}
this.mWorkSocket.BeginReceive(this.mReceiveBuffer, 0, this.mReceiveBuffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), this);
}
catch (Exception exception)
{
this.CloseSocket(exception);
}
}
public void StartSend(string value)
{
byte[] bytes = Encoding.ASCII.GetBytes(value);
this.StartSend(bytes);
}
public void StartSend(byte[] buffer)
{
try
{
if (this.mWorkSocket == null)
{
throw new ArgumentNullException('客户端不存在');
}
if ((buffer != null) && (buffer.Length != 0))
{
this.mWorkSocket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(this.SendDataCallBack), this);
}
}
catch (Exception exception)
{
this.CloseSocket(exception);
}
}
public override string ToString()
{
if (this.mWorkSocket.RemoteEndPoint == null)
{
return string.Empty;
}
return this.mWorkSocket.RemoteEndPoint.ToString();
}
public Guid ClientGuid
{
get
{
return this.mClientGuid;
}
}
public Socket WorkSocket
{
get
{
return this.mWorkSocket;
}
set
{
this.mWorkSocket = value;
}
}
}
}
该代码展示了如何使用 Socket 类构建 TCP 服务器的客户端,包含连接、数据收发、事件处理等核心功能。
连接服务器
SocketClient 类通过构造函数接受一个 Socket 对象作为参数,这个 Socket 对象代表了与服务器的连接。
数据收发
StartReceive() 方法启动异步接收数据,当有数据到达时,ReceiveCallback() 方法会被调用。StartSend() 方法启动异步发送数据,当发送完成时,SendDataCallBack() 方法会被调用。
事件处理
SocketClient 类定义了三个事件:
ReceiveDataFinish:当接收到数据时触发。SendDataFinish:当发送数据完成时触发。SocketClosed:当连接关闭时触发。
辅助方法
CloseSocket() 方法关闭与服务器的连接,并触发 SocketClosed 事件。ToString() 方法返回客户端的 IP 地址和端口号。
总结
这段代码是一个基本的 TCP 服务器客户端的实现,它可以帮助您理解如何使用 Socket 类实现 TCP 协议的客户端应用。您可以参考这段代码,并根据自己的需求进行修改和扩展。
原文地址: https://www.cveoy.top/t/topic/nXiq 著作权归作者所有。请勿转载和采集!