用最快的代码实现TCP,UDP的服务端客户端,帮助新手入门,写出第一份Socket通信;
代码只为用最少的记忆量写出,求快,不求完整.代码有大部分细节需要后续补齐.
1:快速写TCP服务端
同步:
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace FastSocket
{
class FastSocketTCPServer
{
//Socket初始化参数速记:地址因特网,套接字流,协议Tcp
Socket socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
List<Socket> Clients = new List<Socket>();
public FastSocketTCPServer()
{
socket.Bind(new IPEndPoint(IPAddress.Any, 1000));//绑定本地监听端口
socket.Listen(5);
new Thread(AcceptThread).Start();//开始等待客户端接入
}
private void AcceptThread()
{
while (true)
{
Socket proxy = socket.Accept();//阻塞,直到收到用户接入,得到连接
Clients.Add(proxy);
new Thread(RecieveThread).Start(proxy);//开始读取客户端发来的信息
}
}
private void RecieveThread(object proxySocket)
{
while (true)
{
byte[] buffer = new byte[1024];
int len = ((Socket)proxySocket).Receive(buffer);//阻塞,直到读到用户发来数据
DoEventHandle(buffer,len);//处理收到的数据
}
}
private void DoEventHandle(byte[] buffer, int len)
{
//接收到数据,在此处理数据接收事件
//数据在buffer,长度为len字节,若len<0为异常,等于0为结束会话
}
public void Send(IPEndPoint end,byte[] data)
{
foreach (Socket proxy in Clients)
{
if (proxy.RemoteEndPoint != end)
{
continue;
}
proxy.Send(data);//向指定客户端发送数据
}
}
}
}
以上代码,58行实现了一个完整而简单的TCP服务端;
开启了1+N个线程,接收N个客户端接入并读取客户端发来的数据.
其过程可简述为8个步骤:Bind端口,Listen数量,Start接受线程,Accept获取会话,客户端加入列表,Start读取线程,Recieve获取数据,客户端Send数据;
为了压缩代码量,做了简略,有较多地方需要读者补充异常捕获.
2:快速TCP客户端:
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace FastSocket
{
class FastSocketTCPClient
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public FastSocketTCPClient()
{
socket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1000));//接入服务端
new Thread(RecieveThread).Start();//开始接收数据
}
private void RecieveThread()
{
while (true)
{
byte[] buffer = new byte[1024];
int len = socket.Receive(buffer);//阻塞,直到收到服务端发来数据
DoEventHandle(buffer, len);//处理收到的数据
}
}
private void DoEventHandle(byte[] buffer, int len)
{
//收到服务端发来数据
}
public void Send(byte[] data)
{
socket.Send(data);
}
}
}
以上代码,40行实现了一个完整而简单的TCP客户端;
开启了1个子线程,接收1个服务端发来的数据.
其过程可简述为4个步骤:Connect服务端,Start读取线程,Recieve获取数据,Send数据;
为了压缩代码量,做了简略,有较多地方需要读者补充异常捕获.
3快速UDP:
UDP没有服务端,都是平级的客户端:
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace FastSocket
{
class FastSocketUDPSendOnly
{
//注意这里初始化和TCP不同,参数速记:地址因特网,套接字数据报,协议Udp
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
public FastSocketUDPSendOnly()
{
//不需要Bind也能推送Udp数据报
//socket.Bind(new IPEndPoint(IPAddress.Any, 1001));
}
public void Send(byte[] data,string ip,int port)
{
socket.SendTo(data,new IPEndPoint(IPAddress.Parse(ip),port));
}
}
class FastSocketUDPReceiveSend
{
//注意这里初始化和TCP不同,参数速记:地址因特网,套接字数据报,协议Udp
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
public FastSocketUDPReceiveSend()
{
socket.Bind(new IPEndPoint(IPAddress.Any, 1001));
new Thread(ReceiveFromThread).Start();
}
private void ReceiveFromThread()
{
while (true)
{
EndPoint remote = new IPEndPoint(0, 0);
byte[] buffer = new byte[1024];
int len = socket.ReceiveFrom(buffer, ref remote);
DoEventHandler(buffer, len, remote);
}
}
private void DoEventHandler(byte[] buffer, int len, EndPoint remote)
{
//收到来自另一个UDP客户端remote的数据,长度为len字节
}
public void Send(byte[] data, string ip, int port)
{
socket.SendTo(data, new IPEndPoint(IPAddress.Parse(ip), port));
}
}
}
以上代码,53行实现了2个简单的UDP客户端;
需要接收时,开启1个子线程,接收其他UDP客户端发来的数据.
其过程可简述为1或个步骤:[Bind本地端口,Start读取线程],SendTo发送数据;
为了压缩代码量,做了简略,有较多地方需要读者补充异常捕获.