C#是一种流行的编程语言,被广泛用于网络编程。其中,C# Socket是一种常见的网络编程库,我们可以通过 Socket 实现网络通信。下面是关于如何通过 C# Socket 实现控制台案例的完整攻略。
第一步:引入命名空间
在开始之前,需要引入命名空间 System.Net.Sockets
和 System.Text
,以便我们使用 C# Socket 编程库。
using System.Net.Sockets;
using System.Text;
第二步:创建 Socket 对象
接下来,创建一个 Socket 对象。我们可以使用静态方法 Socket()
创建一个新的 Socket 实例,通过指定 AddressFamily
和 SocketType
参数来设置套接字地址族和套接字类型。
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
第三步:连接服务器
使用 Socket 对象的 Connect()
方法连接服务器。需要提供服务器的 IP 地址和端口号。
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int port = 8888;
IPEndPoint serverEp = new IPEndPoint(serverIP, port);
clientSocket.Connect(serverEp);
第四步:发送数据
通过 Socket 对象的 Send()
方法发送数据。需要指定要发送的数据的字节数组。
string message = "Hello, Socket!";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
clientSocket.Send(messageBytes);
示例1:C# Socket 实现简单的客户端和服务器端通信
下面的示例演示了如何通过 C# Socket 实现简单的客户端和服务器端通信。
客户端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Client {
static void Main(string[] args) {
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int port = 8888;
IPEndPoint serverEp = new IPEndPoint(serverIP, port);
clientSocket.Connect(serverEp);
string message = "Hello, Socket!";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
clientSocket.Send(messageBytes);
byte[] receiveBytes = new byte[1024];
int receiveLength = clientSocket.Receive(receiveBytes);
string receivedMessage = Encoding.ASCII.GetString(receiveBytes, 0, receiveLength);
Console.WriteLine(receivedMessage);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
服务器端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class Server {
static void Main(string[] args) {
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int port = 8888;
IPEndPoint serverEp = new IPEndPoint(serverIP, port);
serverSocket.Bind(serverEp);
serverSocket.Listen(10);
Console.WriteLine("Server started");
Socket clientSocket = serverSocket.Accept();
byte[] receiveBytes = new byte[1024];
int receiveLength = clientSocket.Receive(receiveBytes);
string receivedMessage = Encoding.ASCII.GetString(receiveBytes, 0, receiveLength);
Console.WriteLine(receivedMessage);
string message = "Hello, Client!";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
clientSocket.Send(messageBytes);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
serverSocket.Close();
}
}
示例2:C# Socket 实现多个客户端与服务器端通信
下面的示例演示如何通过 C# Socket 实现多个客户端与服务器端通信。服务器端可以同时接收多个客户端发送的消息。
客户端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Client {
static void Main(string[] args) {
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int port = 8888;
for (int i = 0; i < 5; i++) {
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint serverEp = new IPEndPoint(serverIP, port);
clientSocket.Connect(serverEp);
string message = "Hello, Socket " + i.ToString() + "!";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
clientSocket.Send(messageBytes);
Thread.Sleep(1000);
byte[] receiveBytes = new byte[1024];
int receiveLength = clientSocket.Receive(receiveBytes);
string receivedMessage = Encoding.ASCII.GetString(receiveBytes, 0, receiveLength);
Console.WriteLine(receivedMessage);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
}
服务器端代码:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Server {
static void Main(string[] args) {
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
int port = 8888;
IPEndPoint serverEp = new IPEndPoint(serverIP, port);
serverSocket.Bind(serverEp);
serverSocket.Listen(10);
Console.WriteLine("Server started");
while (true) {
Socket clientSocket = serverSocket.Accept();
Thread clientThread = new Thread(() => {
byte[] receiveBytes = new byte[1024];
int receiveLength = clientSocket.Receive(receiveBytes);
string receivedMessage = Encoding.ASCII.GetString(receiveBytes, 0, receiveLength);
Console.WriteLine(receivedMessage);
string message = "Hello, Client!";
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
clientSocket.Send(messageBytes);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
});
clientThread.Start();
}
serverSocket.Close();
}
}
以上就是通过 C# Socket 实现控制台案例的完整攻略,包含了两个示例说明。使用 Socket 进行网络通信时,需要掌握相关的知识点,如套接字地址族和套接字类型等。希望本文对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# Socket实现简单控制台案例 - Python技术站