c# socket封装
时间: 2023-08-07 21:06:53 浏览: 51
C#中可以使用Socket类来实现网络通信,以下是一个简单的Socket封装示例:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketClient {
private Socket clientSocket;
public void Connect(string ip, int port) {
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.Connect(new IPEndPoint(IPAddress.Parse(ip), port));
}
public void Send(string message) {
byte[] data = Encoding.UTF8.GetBytes(message);
clientSocket.Send(data);
}
public string Receive() {
byte[] data = new byte[1024];
int count = clientSocket.Receive(data);
return Encoding.UTF8.GetString(data, 0, count);
}
public void Close() {
clientSocket.Close();
}
}
public class SocketServer {
private Socket serverSocket;
private byte[] buffer = new byte[1024];
public void Start(int port) {
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
serverSocket.Listen(10);
Console.WriteLine("服务器启动,等待客户端连接...");
serverSocket.BeginAccept(AcceptCallback, null);
}
private void AcceptCallback(IAsyncResult ar) {
Socket clientSocket = serverSocket.EndAccept(ar);
Console.WriteLine("客户端连接成功,客户端地址:" + clientSocket.RemoteEndPoint.ToString());
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
serverSocket.BeginAccept(AcceptCallback, null);
}
private void ReceiveCallback(IAsyncResult ar) {
Socket clientSocket = (Socket)ar.AsyncState;
int count = clientSocket.EndReceive(ar);
if (count == 0) {
clientSocket.Close();
Console.WriteLine("客户端关闭连接:" + clientSocket.RemoteEndPoint.ToString());
return;
}
string message = Encoding.UTF8.GetString(buffer, 0, count);
Console.WriteLine("接收到客户端消息:" + message);
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, clientSocket);
}
}
```
以上是一个简单的Socket封装示例,包含了客户端和服务器端的实现。其中,客户端使用Connect方法连接服务器,使用Send方法发送消息,使用Receive方法接收消息,使用Close方法关闭连接。服务器端使用Start方法启动服务器,使用AcceptCallback方法接受客户端连接,使用ReceiveCallback方法接收客户端消息。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)