下面给大家详细讲解一下“C# Socket网络编程实例”的完整攻略。
引言
在进行网络编程时,我们经常需要使用到Socket
,它是一种协议栈的API,它提供了网络编程在应用层的解决方案。在C#语言中,有着完备的Socket基础类库,我们可以简单的使用C# Socket类来进行网络编程。
Socket 常用方法
1. 创建 Socket
Socket socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
2. 连接 Socket
IPAddress ip = IPAddress.Parse("127.0.0.1");//待连接的IP地址
IPEndPoint port = new IPEndPoint(ip, 5000);//待连接的端口号
socket.Connect(port);
3. 发送数据
string sendData = "你好,这是一个网络编程的实例!";
byte[] data = Encoding.ASCII.GetBytes(sendData);
socket.Send(data);
4. 接收数据
byte[] recvBytes = new byte[1024];
int byteRec = socket.Receive(recvBytes);
string recvData = Encoding.ASCII.GetString(recvBytes, 0, byteRec);
5. 释放 Socket
socket.Shutdown(SocketShutdown.Both);
socket.Close();
C# Socket编程实例
示例一:Socket TCP通信示例
服务端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Socket serverSocket = null;
try
{
serverSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000);
serverSocket.Bind(ipEndPoint);
serverSocket.Listen(20);
Console.WriteLine("服务端已启动,等待客户端连接...");
while (true)
{
Socket clientSocket = serverSocket.Accept();
string str = "欢迎连接服务端!";
byte[] buffer = Encoding.UTF8.GetBytes(str);
int iLength = clientSocket.Send(buffer);
Console.WriteLine("发送消息给客户端成功,消息内容:{0}", str);
}
}
catch (Exception ex)
{
Console.WriteLine("服务器出现异常,异常信息:{0}", ex.Message);
}
finally
{
serverSocket?.Close();
}
}
}
}
客户端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Socket clientSocket = null;
try
{
clientSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8000);
clientSocket.Connect(ipEndPoint);
if (clientSocket.Connected)
{
byte[] buffer = new byte[1024];
int iLength = clientSocket.Receive(buffer);
string str = Encoding.UTF8.GetString(buffer, 0, iLength);
Console.WriteLine("接收到服务端消息:{0}", str);
}
}
catch (Exception ex)
{
Console.WriteLine("客户端出现异常,异常信息:{0}", ex.Message);
}
finally
{
clientSocket?.Close();
}
}
}
}
示例二:Socket UDP通信示例
服务端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Server
{
class Program
{
static void Main(string[] args)
{
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
server.Bind(endPoint);
Console.WriteLine("服务端已启动,等待数据接收...");
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
EndPoint ep = (EndPoint)iep;
while (true)
{
byte[] buffer = new byte[1024];
int iLength = server.ReceiveFrom(buffer, ref ep);
string message = Encoding.Default.GetString(buffer, 0, iLength);
Console.WriteLine("接受到客户端消息:{0}", message);
}
}
}
}
客户端代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace Client
{
class Program
{
static void Main(string[] args)
{
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080);
string message = "Hello server";
byte[] data = Encoding.Default.GetBytes(message);
client.SendTo(data, data.Length, SocketFlags.None, iep);
Console.WriteLine("已经向服务端发送数据:{0}", message);
}
}
}
以上是本次的完整攻略,希望对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# Socket网络编程实例 - Python技术站