C#使用Socket实现TCP服务器端
1、TCP服务器实现代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PtLib.TcpServer
{
public delegate void TcpReceivedEventHandler(TcpStateEventArgs args);
public class TcpServer
{
#region 变量
//接收委托
public event TcpReceivedEventHandler TcpReceived;
//服务器IP
private string _ip = "127.0.0.1";
//服务器端口号
private int _port = 8080;
//当前Socket
Socket currentSocket = null;
//当前线程
Thread currentThread = null;
//Socket字典
public Dictionary<string,Socket> dictSocket = new Dictionary<string,Socket>();
//线程字典
public Dictionary<string,Thread> dictThread = new Dictionary<string,Thread>();
//运行标志
public bool isRun =false;
//
public bool isClientChanged = false;
#endregion
/// <summary>
/// 构造函数
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
public TcpServer(string ip,int port)
{
_ip = ip;
_port = port;
}
/// <summary>
/// 打开
/// </summary>
public void Open()
{
currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(_ip), _port);
currentSocket.
原文地址:https://blog.csdn.net/qq_27474555/article/details/140851066
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!