C#实现点对点通信,客户端既是服务端又是服务端
时间: 2023-11-08 18:04:15 浏览: 70
在C#中实现点对点通信,一种常用的方式是使用Socket编程。通过Socket编程,可以实现客户端和服务端之间的通信。
如果需要实现客户端既是服务端又是服务端的功能,可以使用双向Socket通信。在这种情况下,客户端同时具有发送和接收数据的能力,而服务端则负责接收客户端发送的数据并向客户端发送回复。
下面是一个简单的C#代码示例,实现了一个双向Socket通信的客户端和服务端:
```csharp
// 服务端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
// 创建一个监听Socket
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 绑定监听端口
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, 12345);
listener.Bind(localEP);
// 开始监听
listener.Listen(10);
Console.WriteLine("Waiting for a connection...");
// 接收客户端连接请求
Socket handler = listener.Accept();
Console.WriteLine("Connected to client");
while (true)
{
// 接收客户端发送的数据
byte[] bytes = new byte[1024];
int bytesRec = handler.Receive(bytes);
string data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
Console.WriteLine($"Received from client: {data}");
// 向客户端发送数据
byte[] msg = Encoding.ASCII.GetBytes("Hello from server");
handler.Send(msg);
}
// 关闭连接
handler.Shutdown(SocketShutdown.Both);
handler.Close();
}
}
}
// 客户端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
// 创建一个Socket连接到服务器
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12345);
client.Connect(remoteEP);
Console.WriteLine("Connected to server");
while (true)
{
// 向服务器发送数据
string msg = "Hello from client";
byte[] bytes = Encoding.ASCII.GetBytes(msg);
client.Send(bytes);
// 接收服务器发送的数据
bytes = new byte[1024];
int bytesRec = client.Receive(bytes);
string data = Encoding.ASCII.GetString(bytes, 0, bytesRec);
Console.WriteLine($"Received from server: {data}");
}
// 关闭连接
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}
}
```
在这个例子中,客户端和服务端分别运行在两个不同的进程中。客户端和服务端都创建了一个Socket连接,并通过Connect和Accept方法建立连接。客户端和服务端通过Send和Receive方法互相发送数据。
需要注意的是,在实际使用中,需要添加异常处理以及其他相关的代码来确保程序的稳定性和安全性。
相关推荐
![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)