目录
一、C#编写控制台hello world程序
1.创建项目
2.编写代码及效果
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int i;
for (i = 1; i <= 50; i++)
{
Console.WriteLine("Hello cqitu!重交物联2018 "+i);
}
Console.ReadLine(); //Console.ReadLine()方法从控制台读取任何行。通过输入此行代码,程序将等待并且不会立即退出
}
}
}
二、使用UDP套接字向另一台计算机发送消息
1.创建一个控制台程序
- 创建步骤和上面一样,客户端(发送方)代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDPClient
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
string input, stringData;
//构建UDP 服务器
Console.WriteLine("This is a Client, host name is LZZ");
//设置服务IP,设置UDP端口号
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.100"), 1316);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "hello! ";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ip);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sender;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
while (true)
{
string msg = "hello cqjtu! 重交物联2018级";
int i = 1;
while(i <= 50)
{
server.SendTo(Encoding.ASCII.GetBytes(msg),Remote);
i++;
}
input = Console.ReadLine();
if (input == "exit")
break;
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
data = new byte[1024];
recv = server.ReceiveFrom(data, ref Remote);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine(stringData);
}
Console.WriteLine("Stopping Client.");
server.Close();
}
}
}
注意:将
(IPAddress.Parse("192.168.43.100"), 1316);
中的IP地址换成服务端(接受消息方)的IP地址
- 服务端也要创建一个控制台程序,服务端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace UDP
{
class Program
{
static void Main(string[] args)
{
int recv;
byte[] data = new byte[1024];
//得到本机IP,设置TCP端口号
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 1316);
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定网络地址
newsock.Bind(ip);
Console.WriteLine("This is a Server, host name is TBM");
//等待客户机连接
Console.WriteLine("Waiting for a client");
//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
//客户机连接成功后,发送信息
string welcome = "hello ! ";
//字符串与字节数组相互转换
data = Encoding.ASCII.GetBytes(welcome);
//发送信息
newsock.SendTo(data, data.Length, SocketFlags.None, Remote);
while (true)
{
data = new byte[1024];
//发送接收信息
recv = newsock.ReceiveFrom(data, ref Remote);
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
newsock.SendTo(data, recv, SocketFlags.None, Remote);
}
}
}
}
注意:运行程序时要先运行服务端代码,再运行客户端代码,否则服务端无法接收成功。
2.运行及结果
- 客户端:
- 服务端:
三、创建Form窗口来传递信息
1.创建一个Windows窗体应用
- 在设计界面添加一个按钮Button,和一个输入框Box
- Button1_Click代码:
byte[] data = new byte[1024];
string input;
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.82"), 1316);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.SendTo(data, data.Length, SocketFlags.None, ip);
IPEndPoint sende = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sende;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
int recv = server.ReceiveFrom(data, ref Remote);
input = textBox1.Text;
if (input == "exit")
server.Close();
server.SendTo(Encoding.ASCII.GetBytes(input), Remote);
server.Close();
- CS的main函数代码:
byte[] data = new byte[1024];
//构建TCP 服务器
Console.WriteLine("This is a Client, host name is TBM");
//设置服务IP,设置TCP端口号
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("192.168.43.82"), 1316);
//定义网络类型,数据连接类型和网络协议UDP
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
string welcome = "success connect! ";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, ip);
IPEndPoint sende = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)sende;
data = new byte[1024];
//对于不存在的IP地址,加入此行代码后,可以在指定时间内解除阻塞模式限制
int recv = server.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}: ", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
四、通过 wireshark抓包,对数据帧进行分析
- 抓包:
筛选UPD包,抓住后,由此可以看见,从我的电脑IP192.168.43.91的端口1316,向192.168.43.100的端口1316,发送了包。
在这里插入图片描述
五、参考链接
六、总结
初步学习了创建控制台程序、Form窗口程序以及抓包,并使用UDP套接字实现与其他电脑发送消息。