C# 运用Socket,创建服务端和客户端,一个服务端可和多个客户端发送接收消息

我是一个有强迫症的人。优化了下。
效果图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
先来看界面控件名
在这里插入图片描述

在这里插入图片描述
上码

服务端:

public partial class Form5 : 
  • 5
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,这是一个比较复杂的问题,需要分步骤来实现。 1. 创建服务端 首先,你需要创建一个服务端程序,用于接收客户端发送的图片。可以使用C#语言中提供的Socket类来实现。具体步骤如下: - 创建Socket对象,指定通信协议和端口号。 - 使用Socket对象的Bind方法将Socket绑定到指定的端口。 - 使用Socket对象的Listen方法开始监听客户端连接请求。 - 使用Socket对象的Accept方法接受客户端连接请求,并返回一个新的Socket对象来处理客户端的请求。 - 使用新的Socket对象来接收客户端发送的图片数据。 下面是一个简单的示例代码: ```csharp using System; using System.Net; using System.Net.Sockets; using System.Threading; namespace Server { class Program { static void Main(string[] args) { // 创建Socket对象 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 绑定端口 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8888); serverSocket.Bind(endPoint); // 开始监听 serverSocket.Listen(100); Console.WriteLine("Server started."); while (true) { // 接受客户端连接请求 Socket clientSocket = serverSocket.Accept(); // 开启新线程处理客户端请求 Thread thread = new Thread(() => { Console.WriteLine("Client connected: " + clientSocket.RemoteEndPoint.ToString()); // 接收图片数据 byte[] buffer = new byte[1024]; int count = clientSocket.Receive(buffer); byte[] imageData = new byte[count]; Array.Copy(buffer, imageData, count); // TODO: 处理图片数据 // 关闭客户端连接 clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); Console.WriteLine("Client disconnected: " + clientSocket.RemoteEndPoint.ToString()); }); thread.Start(); } } } } ``` 2. 创建客户端 接下来,你需要创建一个客户端程序,用于选择并发送图片给服务端。同样可以使用C#语言中提供的Socket类来实现。具体步骤如下: - 创建Socket对象,指定通信协议和服务端IP地址及端口号。 - 使用Socket对象的Connect方法连接服务端。 - 使用Socket对象的Send方法发送图片数据给服务端。 下面是一个简单的示例代码: ```csharp using System; using System.IO; using System.Net; using System.Net.Sockets; namespace Client { class Program { static void Main(string[] args) { // 输入图片路径 Console.Write("Enter image path: "); string imagePath = Console.ReadLine(); // 读取图片数据 byte[] imageData = File.ReadAllBytes(imagePath); // 创建Socket对象 Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 连接服务端 IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888); clientSocket.Connect(endPoint); Console.WriteLine("Connected to server."); // 发送图片数据 clientSocket.Send(imageData); // 关闭连接 clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); Console.WriteLine("Disconnected from server."); } } } ``` 3. 实现图形化界面 最后,你需要将服务端客户端程序集成到一个图形化界面中,用于选择和发送图片。可以使用C#语言中提供的Windows窗口应用程序来实现。具体步骤如下: - 创建Windows窗口应用程序项目。 - 在界面中添加图形化控件,如按钮、文本框、图片框等。 - 在按钮的Click事件中实现服务端客户端的功能代码。 下面是一个简单的示例代码: ```csharp using System; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading.Tasks; using System.Windows.Forms; namespace ChatSample { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnServer_Click(object sender, EventArgs e) { Task.Run(() => { // 创建Socket对象 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 绑定端口 IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8888); serverSocket.Bind(endPoint); // 开始监听 serverSocket.Listen(100); Invoke(new Action(() => { txtLog.AppendText("Server started.\r\n"); })); while (true) { // 接受客户端连接请求 Socket clientSocket = serverSocket.Accept(); // 开启新线程处理客户端请求 Task.Run(() => { Invoke(new Action(() => { txtLog.AppendText("Client connected: " + clientSocket.RemoteEndPoint.ToString() + "\r\n"); })); // 接收图片数据 byte[] buffer = new byte[1024]; int count = clientSocket.Receive(buffer); byte[] imageData = new byte[count]; Array.Copy(buffer, imageData, count); // 显示图片 Invoke(new Action(() => { MemoryStream stream = new MemoryStream(imageData); Image image = Image.FromStream(stream); picImage.Image = image; })); // 关闭客户端连接 clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); Invoke(new Action(() => { txtLog.AppendText("Client disconnected: " + clientSocket.RemoteEndPoint.ToString() + "\r\n"); })); }); } }); } private void btnClient_Click(object sender, EventArgs e) { // 选择图片文件 OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Image Files (*.jpg, *.png)|*.jpg;*.png"; if (dialog.ShowDialog() != DialogResult.OK) return; // 读取图片数据 byte[] imageData = File.ReadAllBytes(dialog.FileName); Task.Run(() => { // 创建Socket对象 Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 连接服务端 IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888); clientSocket.Connect(endPoint); Invoke(new Action(() => { txtLog.AppendText("Connected to server.\r\n"); })); // 发送图片数据 clientSocket.Send(imageData); // 关闭连接 clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); Invoke(new Action(() => { txtLog.AppendText("Disconnected from server.\r\n"); })); }); } } } ``` 这样,你就可以实现一个能够互相选择和发送图片的服务端客户端程序了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzm996

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值