写一个c#socket通讯解决粘包示例
时间: 2023-07-25 11:09:04 浏览: 125
下面是一个简单的 C# Socket 通讯解决粘包的示例:
```csharp
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class SocketServer
{
private Socket listener;
private const int BUFFER_SIZE = 1024;
private byte[] buffer = new byte[BUFFER_SIZE];
public void Start()
{
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 8080));
listener.Listen(10);
Console.WriteLine("Server started on port 8080.");
while (true)
{
Socket client = listener.Accept();
Console.WriteLine("Client connected: " + client.RemoteEndPoint.ToString());
// Start receiving data from client
client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client);
}
}
private void OnReceive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// Convert received bytes to string
string receivedData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// Process received data
Console.WriteLine("Received data: " + receivedData);
// Continue receiving data from client
client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client);
}
else
{
// Client has disconnected
Console.WriteLine("Client disconnected: " + client.RemoteEndPoint.ToString());
client.Close();
}
}
}
public class SocketClient
{
private Socket client;
private const int BUFFER_SIZE = 1024;
private byte[] buffer = new byte[BUFFER_SIZE];
public void Connect()
{
client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(new IPEndPoint(IPAddress.Loopback, 8080), new AsyncCallback(OnConnect), null);
}
private void OnConnect(IAsyncResult ar)
{
Console.WriteLine("Connected to server.");
client.EndConnect(ar);
// Start receiving data from server
client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client);
}
private void OnReceive(IAsyncResult ar)
{
Socket client = (Socket)ar.AsyncState;
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
// Convert received bytes to string
string receivedData = Encoding.UTF8.GetString(buffer, 0, bytesRead);
// Process received data
Console.WriteLine("Received data: " + receivedData);
// Continue receiving data from server
client.BeginReceive(buffer, 0, BUFFER_SIZE, 0, new AsyncCallback(OnReceive), client);
}
else
{
// Server has disconnected
Console.WriteLine("Server disconnected.");
client.Close();
}
}
public void Send(string data)
{
byte[] sendData = Encoding.UTF8.GetBytes(data);
client.BeginSend(sendData, 0, sendData.Length, 0, new AsyncCallback(OnSend), null);
}
private void OnSend(IAsyncResult ar)
{
client.EndSend(ar);
}
}
class Program
{
static void Main(string[] args)
{
// Start server
SocketServer server = new SocketServer();
server.Start();
// Connect client to server
SocketClient client = new SocketClient();
client.Connect();
// Send data to server
client.Send("Hello, server!");
Console.ReadLine();
}
}
```
这个示例中,我们使用了异步的 BeginReceive 和 BeginSend 方法来在接收和发送数据时不阻塞主线程。在接收数据时,我们先接收到一部分数据,然后判断是否收到了完整的数据,如果没有就继续接收。在发送数据时,我们将要发送的数据转换成字节数组后,使用 BeginSend 方法发送,然后在回调函数中调用 EndSend 方法结束发送。
阅读全文