C#使用TCP/UDP协议通信
一.控制台程序使用 UDP 通信
1.创建项目
2.代码
循环输出"hello cqjtu!重交物联2019级"
for (int i = 0; i < 50; i++)
{
Console.WriteLine("hello cqjtu!重交物联2019级");
}
System.Console.ReadKey();
3.运行结果
二.使用 UDP 通信
目前最普遍的服务模式是 C/S 模式,所以需要编写一个客户端 client 和一个服务端 Server ,来实现通信。
1.发送端
代码流程:
首先显示提示信息,等待使用人员操作;做好连接准备,
如:设置IP、端口号等;
for 循环发送数据;
关闭端口;显示提示信息,等待用户确认退出。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
//注意头文件!!!使用网络协议需要引入头文件 .Net 和 .Net.Sockets)
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//提示信息
Console.WriteLine("按下任意按键开始发送...");
Console.ReadKey();
//做好链接准备
UdpClient client = new UdpClient(); //实例一个端口
IPAddress remoteIP = IPAddress.Parse("10.60.4.53"); //假设发送给这个IP
int remotePort = 11000; //设置端口号
IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort); //实例化一个远程端点
for (int i = 0; i < 50; i++)
{
//要发送的数据:第n行:hello cqjtu!重交物联2018级
string sendString = null;
sendString += "hello cqjtu!重交物联2019级";
//定义发送的字节数组
//将字符串转化为字节并存储到字节数组中
byte[] sendData = null;
sendData = Encoding.Default.GetBytes(sendString);
client.Send(sendData, sendData.Length, remotePoint);//将数据发送到远程端点
}
client.Close();//关闭连接
//提示信息
Console.WriteLine("");
Console.WriteLine("数据发送成功,按任意键退出...");
System.Console.ReadKey();
}
}
}
2.接收端
代码流程:
做好连接准备,并设置结束标志;
循环接收数据;
关闭连接;显示提示信息,等待用户确定退出。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.