C#利用服务器实现客户端之间通信
在C#中,我们可以使用 TCP、UDP 等协议,以及 socket 编程来实现客户端之间的通信。下面将详细介绍基于 TCP 协议的服务器和客户端之间的通信实现。
环境准备
在进行操作之前,需要准备以下环境:
- 安装 Visual Studio 开发工具
- 使用 C# 语言进行开发
服务器端代码
1. 引入命名空间
要使用 socket 进行编程,需要使用 System.Net 命名空间。
using System.Net;
using System.Net.Sockets;
2. 定义服务器
服务器可以使用 Socket 类实现。
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpListener server = new TcpListener(ip, port);
3. 开始监听
将服务器绑定到本地 IP 地址和端口,并开始监听来自客户端的连接请求。
server.Start();
4. 接受客户端连接请求
等待客户端的连接请求,并接受连接请求。
TcpClient client = server.AcceptTcpClient();
5. 接受客户端发送的数据
接受客户端发送的数据,并将数据存储在一个 byte 数组中。
NetworkStream stream = client.GetStream();
byte[] data = new byte[1024];
stream.Read(data, 0, data.Length);
6. 发送数据到客户端
向客户端发送数据,可以使用 NetworkStream.Write 方法。
string message = "Hello Client!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
7. 关闭连接
关闭客户端连接。
client.Close();
完整代码示例
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpListener server = new TcpListener(ip, port);
server.Start();
Console.WriteLine("Server Started.");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client Connected.");
NetworkStream stream = client.GetStream();
byte[] data = new byte[1024];
stream.Read(data, 0, data.Length);
string message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
message = "Hello Client!";
data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
Console.WriteLine($"Sent {message}");
client.Close();
Console.WriteLine("Client Disconnected.");
server.Stop();
Console.WriteLine("Server Stopped.");
}
}
}
客户端代码
1. 引入命名空间
同样需要使用 System.Net 命名空间。
using System.Net;
using System.Net.Sockets;
2. 客户端连接服务器
向服务器发送请求,并连接到服务器。
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpClient client = new TcpClient();
client.Connect(ip, port);
3. 向服务器发送数据
客户端发送数据到服务器,并接收服务器返回的数据。
NetworkStream stream = client.GetStream();
string message = "Hello Server!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
data = new byte[1024];
stream.Read(data, 0, data.Length);
message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
4. 关闭连接
关闭客户端连接。
client.Close();
完整代码示例
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("Connected to Server.");
NetworkStream stream = client.GetStream();
string message = "Hello Server!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
data = new byte[1024];
stream.Read(data, 0, data.Length);
message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
client.Close();
Console.WriteLine("Disconnected.");
}
}
}
示例说明
示例1
假设有两个客户端A和B,客户端A向服务端发送消息“Hello Server!”,服务端接收到消息后再发送消息“Hello Client!”,客户端A接收到消息后打印出来。
服务端代码:
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpListener server = new TcpListener(ip, port);
server.Start();
Console.WriteLine("Server Started.");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Client Connected.");
NetworkStream stream = client.GetStream();
byte[] data = new byte[1024];
stream.Read(data, 0, data.Length);
string message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
message = "Hello Client!";
data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
Console.WriteLine($"Sent {message}");
client.Close();
Console.WriteLine("Client Disconnected.");
server.Stop();
Console.WriteLine("Server Stopped.");
客户端A代码:
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("Connected to Server.");
NetworkStream stream = client.GetStream();
string message = "Hello Server!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
data = new byte[1024];
stream.Read(data, 0, data.Length);
message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
client.Close();
Console.WriteLine("Disconnected.");
示例2
在示例1的基础上,增加客户端B,表示客户端B向服务端发送消息“Hello Server too!”,服务端接收到消息后再发送消息“Hello Client too!”给客户端B,并且客户端B接收到消息后打印出来。
服务端代码:
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpListener server = new TcpListener(ip, port);
server.Start();
Console.WriteLine("Server Started.");
TcpClient clientA = server.AcceptTcpClient();
Console.WriteLine("Client A Connected.");
NetworkStream streamA = clientA.GetStream();
byte[] dataA = new byte[1024];
streamA.Read(dataA, 0, dataA.Length);
string messageA = Encoding.ASCII.GetString(dataA);
Console.WriteLine($"Received {messageA}");
messageA = "Hello Client A!";
dataA = Encoding.ASCII.GetBytes(messageA);
streamA.Write(dataA, 0, dataA.Length);
Console.WriteLine($"Sent {messageA}");
TcpClient clientB = server.AcceptTcpClient();
Console.WriteLine("Client B Connected.");
NetworkStream streamB = clientB.GetStream();
byte[] dataB = new byte[1024];
streamB.Read(dataB, 0, dataB.Length);
string messageB = Encoding.ASCII.GetString(dataB);
Console.WriteLine($"Received {messageB}");
messageB = "Hello Client B!";
dataB = Encoding.ASCII.GetBytes(messageB);
streamB.Write(dataB, 0, dataB.Length);
Console.WriteLine($"Sent {messageB}");
clientA.Close();
Console.WriteLine("Client A Disconnected.");
clientB.Close();
Console.WriteLine("Client B Disconnected.");
server.Stop();
Console.WriteLine("Server Stopped.");
客户端A代码:
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("Connected to Server.");
NetworkStream stream = client.GetStream();
string message = "Hello Server!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
data = new byte[1024];
stream.Read(data, 0, data.Length);
message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
client.Close();
Console.WriteLine("Disconnected.");
客户端B代码:
IPAddress ip = IPAddress.Parse("127.0.0.1");
int port = 8888;
TcpClient client = new TcpClient();
client.Connect(ip, port);
Console.WriteLine("Connected to Server.");
NetworkStream stream = client.GetStream();
string message = "Hello Server too!";
byte[] data = Encoding.ASCII.GetBytes(message);
stream.Write(data, 0, data.Length);
data = new byte[1024];
stream.Read(data, 0, data.Length);
message = Encoding.ASCII.GetString(data);
Console.WriteLine($"Received {message}");
client.Close();
Console.WriteLine("Disconnected.");
以上就是基于 TCP 协议的服务器和客户端之间通信的完整攻略,希望可以帮助您进行相关的开发。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#利用服务器实现客户端之间通信 - Python技术站