C#实现局域网之间互相传递消息 您所在的位置:网站首页 握手网最新消息 C#实现局域网之间互相传递消息

C#实现局域网之间互相传递消息

2024-07-09 17:54| 来源: 网络整理| 查看: 265

文章目录 前言输入IP获取局域网IP 创建UDP客户端完整代码运行结果不同的字符编码其他文章

前言

本文章做的程序使用UDP协议进行消息传输,使用简单的C#控制台应用程序。包含了两个设备之间的通信功能。

输入IP

首先,用户需要在运行程序时输入设备1和设备2的IP地址,分别用两个变量device1IP和device2IP存下来。

Console.WriteLine("请输入设备1的IP地址:"); string device1IP = Console.ReadLine(); Console.WriteLine("请输入设备2的IP地址:"); string device2IP = Console.ReadLine(); 获取局域网IP

参考C#如何获取当前主机的IP地址这篇文章,也可以进入任务管理器查看。

创建UDP客户端

在主函数中,使用UdpClient类创建一个UDP客户端,监听指定的端口号(这里使用8888)。然后再创建两个IPEndPoint实例,分别对应设备1和设备2的IP地址和端口号。

using (UdpClient client = new UdpClient(Port)) { IPEndPoint device1EndPoint = new IPEndPoint(IPAddress.Parse(device1IP), Port); IPEndPoint device2EndPoint = new IPEndPoint(IPAddress.Parse(device2IP), Port); }

接下来,我们启动一个接收消息的线程,不断监听设备2发送到指定端口的消息。如果收到消息,我们将字节数组转换为字符串,并在控制台上显示出来。

// 开启接收消息的线程 var receiveThread = new System.Threading.Thread(() => { while (true) { Console.WriteLine("等待接收消息..."); byte[] bytes = client.Receive(ref device2EndPoint); string receivedMessage = Encoding.Unicode.GetString(bytes); Console.WriteLine("收到的消息:"); Console.WriteLine(receivedMessage); } }); receiveThread.Start();

注意,在接收消息的线程中,Receive方法的第一个参数是ref device2EndPoint,这是为了确保每次接收消息时都从设备2的IP地址和端口接收,而不是设备1的。

接着,在主循环中,我们显示一个菜单供用户选择操作:发送消息或退出程序。用户可以根据提示进行选择。

while (true) { Console.WriteLine("请选择操作:"); Console.WriteLine("1. 发送消息"); Console.WriteLine("2. 退出"); string input = Console.ReadLine(); switch (input) { case "1": Console.WriteLine("请输入一条消息:"); string message = Console.ReadLine(); byte[] bytes = Encoding.Unicode.GetBytes(message); client.Send(bytes, bytes.Length, device1EndPoint); Console.WriteLine("消息已发送。"); break; case "2": Console.WriteLine("程序已退出。"); receiveThread.Abort(); return; default: Console.WriteLine("无效的选择,请重新输入。"); break; } } 如果用户选择发送消息(输入1),则程序会要求用户输入要发送的消息,并将消息转换为字节数组,使用Send方法将消息发送到设备1的IP地址和端口。如果用户选择退出程序(输入2),则程序会中止接收消息的线程并退出程序。 完整代码 using System; using System.Net; using System.Net.Sockets; using System.Text; class Program { private const int Port = 8888; public static void Main(string[] args) { Console.WriteLine("请输入设备1的IP地址:"); string device1IP = Console.ReadLine(); Console.WriteLine("请输入设备2的IP地址:"); string device2IP = Console.ReadLine(); using (UdpClient client = new UdpClient(Port)) { IPEndPoint device1EndPoint = new IPEndPoint(IPAddress.Parse(device1IP), Port); IPEndPoint device2EndPoint = new IPEndPoint(IPAddress.Parse(device2IP), Port); Console.WriteLine("已启动程序。"); // 开启接收消息的线程 var receiveThread = new System.Threading.Thread(() => { while (true) { Console.WriteLine("等待接收消息..."); byte[] bytes = client.Receive(ref device2EndPoint); string receivedMessage = Encoding.Unicode.GetString(bytes); Console.WriteLine("收到的消息:"); Console.WriteLine(receivedMessage); } }); receiveThread.Start(); while (true) { Console.WriteLine("请选择操作:"); Console.WriteLine("1. 发送消息"); Console.WriteLine("2. 退出"); string input = Console.ReadLine(); switch (input) { case "1": Console.WriteLine("请输入一条消息:"); string message = Console.ReadLine(); byte[] bytes = Encoding.Unicode.GetBytes(message); client.Send(bytes, bytes.Length, device1EndPoint); Console.WriteLine("消息已发送。"); break; case "2": Console.WriteLine("程序已退出。"); receiveThread.Abort(); return; default: Console.WriteLine("无效的选择,请重新输入。"); break; } } } } } 运行结果

在这里插入图片描述

不同的字符编码

使用不同的编码,直接改Encoding的属性即可。

例如,下面的代码是把将字符串 message 使用 UTF-8 编码方式转换为字节数组 bytes。

byte[] bytes = Encoding.UTF8.GetBytes(message); 其他文章

安卓获取当前设备的局域网IP地址 安卓监听端口接收消息



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有