C#基于Socket的网络通信类攻略
什么是基于Socket的网络通信?
基于Socket的网络通信是指利用Socket技术实现网络通信的过程。Socket(套接字)是一个通信端点,它包含了IP地址和端口号。在网络通信中,客户端和服务器端都需要创建Socket对象以便建立连接,进行数据传输。
C#实现基于Socket的网络通信的方式
在C#中实现基于Socket的网络通信,可以使用System.Net.Sockets中的TcpClient和TcpListener类。其中,TcpClient用于创建一个客户端Socket对象,TcpListener用于创建一个服务器端Socket对象。
客户端实现网络通信
- 创建TcpClient对象:使用TcpClient类创建一个客户端Socket对象。
TcpClient client = new TcpClient(serverHost, serverPort);
- 获取网络流:使用TcpClient对象获取网络流NetworkStream。
NetworkStream stream = client.GetStream();
- 发送数据:使用网络流NetworkStream将数据发送到服务器端。
byte[] buffer = Encoding.ASCII.GetBytes(data);
stream.Write(buffer, 0, buffer.Length);
- 接收来自服务器端的响应:使用网络流NetworkStream接收服务器端返回的响应。
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
- 关闭连接:使用TcpClient和NetworkStream关闭连接。
stream.Close();
client.Close();
服务器端实现网络通信
- 创建TcpListener对象:使用TcpListener类创建一个服务器端Socket对象。
TcpListener server = new TcpListener(localAddr, port);
- 开始监听:使用TcpListener对象开始监听来自客户端的连接请求。
server.Start();
- 接受连接请求:使用TcpListener对象Accept方法接受来自客户端的连接请求,并返回一个TcpClient对象。
TcpClient client = server.AcceptTcpClient();
- 获取网络流:使用TcpClient对象获取网络流NetworkStream。
NetworkStream stream = client.GetStream();
- 接收来自客户端的数据:使用网络流NetworkStream接收客户端发送的数据。
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
- 发送响应:使用网络流NetworkStream将响应发送到客户端。
byte[] responseBuffer = Encoding.ASCII.GetBytes(response);
stream.Write(responseBuffer, 0, responseBuffer.Length);
- 关闭连接:使用TcpClient和NetworkStream关闭连接。
stream.Close();
client.Close();
server.Stop();
示例
客户端发送请求,服务器端响应
客户端代码:
using System;
using System.Net.Sockets;
using System.Text;
class Program {
static void Main(string[] args) {
string data = "Hello from client";
string response = "";
string serverHost = "localhost";
int serverPort = 12345;
TcpClient client = new TcpClient(serverHost, serverPort);
NetworkStream stream = client.GetStream();
byte[] buffer = Encoding.ASCII.GetBytes(data);
stream.Write(buffer, 0, buffer.Length);
buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Response received from server: {0}", response);
stream.Close();
client.Close();
Console.ReadKey();
}
}
服务器端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Program {
static void Main(string[] args) {
string response = "Hello from server";
int port = 12345;
TcpListener server = new TcpListener(IPAddress.Any, port);
server.Start();
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
byte[] buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Data received from client: {0}", data);
byte[] responseBuffer = Encoding.ASCII.GetBytes(response);
stream.Write(responseBuffer, 0, responseBuffer.Length);
stream.Close();
client.Close();
server.Stop();
Console.ReadKey();
}
}
服务器端广播消息
客户端代码:
using System;
using System.Net.Sockets;
using System.Text;
class Program {
static void Main(string[] args) {
string data = "Hello from client";
string response = "";
string serverHost = "localhost";
int serverPort = 12345;
TcpClient client = new TcpClient(serverHost, serverPort);
NetworkStream stream = client.GetStream();
byte[] buffer = Encoding.ASCII.GetBytes(data);
stream.Write(buffer, 0, buffer.Length);
buffer = new byte[1024];
int bytesRead = stream.Read(buffer, 0, buffer.Length);
response = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Response received from server: {0}", response);
stream.Close();
client.Close();
Console.ReadKey();
}
}
服务器端代码:
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Server {
static void Main(string[] args) {
int port = 12345;
List<TcpClient> clients = new List<TcpClient>();
TcpListener server = new TcpListener(IPAddress.Any, port);
server.Start();
Console.WriteLine("Server started on port {0}", port);
while (true) {
TcpClient client = server.AcceptTcpClient();
clients.Add(client);
NetworkStream stream = client.GetStream();
byte[] welcomeBuffer = Encoding.ASCII.GetBytes("Welcome to the server!");
stream.Write(welcomeBuffer, 0, welcomeBuffer.Length);
Console.WriteLine("Client connected");
byte[] buffer = new byte[1024];
int bytesRead;
while (true) {
try {
bytesRead = stream.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0, bytesRead);
Console.WriteLine("Data received from client: {0}", data);
byte[] responseBuffer = Encoding.ASCII.GetBytes(data);
foreach(TcpClient c in clients) {
NetworkStream ns = c.GetStream();
ns.Write(responseBuffer, 0, responseBuffer.Length);
}
} catch(IOException) {
Console.WriteLine("Client disconnected");
clients.Remove(client);
stream.Close();
client.Close();
break;
}
}
}
}
}
总结
基于Socket的网络通信是实现网络通信的一种方法,在C#中可以使用TcpClient和TcpListener类来实现。这种方法可以实现客户端与服务器端之间的数据传输。通过本文的讲解,我们学习了如何使用TcpClient和TcpListener类来实现基于Socket的网络通信,以及一些实用的示例,使我们了解了该技术的实际应用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#基于Socket的网络通信类你了解吗 - Python技术站