C#编程和网络编程
一、C#简单helloworld程序
1.用C#编写一个命令行/控制台hello world程序,实现如下功能:在屏幕上连续输出50行“hello cqjtu!重交物联2018级”;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace helloworld控制台
{
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 50; i++)
{
Console.WriteLine("hello cqjtu!重交物联2018级");
}
Console.ReadKey();
}
}
}
2.创建一个server服务器和client进行通信编程
服务器代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace server
{
class Program
{
static void Main(string[] args)
{
UdpClient udpRec = new UdpClient(8888);
Console.WriteLine("服务器已开启!");
try
{
while (true)
{
IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] recBytes = udpRec.Receive(ref remoteIpEndPoint);
string returnData = Encoding.Default.GetString(recBytes);
Console.WriteLine("接收到的数据是:" + returnData);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
udpRec.Close();
}
}
}
}
客户端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
namespace client
{
class Program
{
static void Main(string[] args)
{
int j = 0;
while (j < 50)
{
Console.WriteLine("hello cqjtu!重交物联2018级");
j++;
}
UdpClient client = new UdpClient("127.0.0.1", 8888);
Console.WriteLine("正在准备发送数据!");
try
{
while (j>0)
{
string str = "hello cqjtu!重交物联2018级";
byte[] sendBytes = Encoding.Default.GetBytes(str);
client.Send(sendBytes, sendBytes.Length);
j--;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
client.Close();
}
Console.WriteLine("数据发送成功!");
Console.Read();
}
}
}
二、写一个Form窗口程序
用VS2015/2017 的C#编写一个简单的Form窗口程序,有一个文本框 textEdit和一个发送按钮button,运行程序后,可以在文本框里输入文字,如“hello cqjtu物联18!”,点击button,将这些文字发送给室友电脑或树莓派,采用UDP套接字;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Server
{
public partial class Form1 : Form
{
private UdpClient receiveUdpClient;//接收用
private UdpClient sendUdpClient;//发送用
private const int port = 8888;//和本机绑定的端口号
IPAddress ip;//本机ip
IPAddress remoteip;//=IPAddress.Parse("");远程主机ip
public Form1()
{
InitializeComponent();
CheckForIllegalCrossThreadCalls = false;
//获取本机可用IP地址
IPAddress[] ips = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress ipa in ips)
{
if (ipa.AddressFamily == AddressFamily.InterNetwork)
{
ip = ipa;
break;
}
}
//为了在同一台机器调试,此IP也作为默认远程IP
remoteip = ip;
}
private void Form1_Load(object sender, EventArgs e)
{
//创建一个线程接收远程主机发来的信息
Thread myThread = new Thread(ReceiveData);
myThread.IsBackground = true;
myThread.Start();
}
//接收数据
private void ReceiveData()
{
IPEndPoint local = new IPEndPoint(ip, port);
receiveUdpClient = new UdpClient(local);
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
try
{
//关闭udpClient 时此句会产生异常
byte[] receiveBytes = receiveUdpClient.Receive(ref remote);
string receiveMessage = Encoding.Unicode.GetString(
receiveBytes, 0, receiveBytes.Length);
listBox1.Items.Add("收到的消息:" + receiveMessage);
}
catch
{
break;
}
}
}
//点击发送按钮发送数据
private void button1_Click(object sender, EventArgs e)
{
//remoteip = IPAddress.Parse(txt_IPAddress.Text);
Thread myThread = new Thread(SendMessage);
myThread.IsBackground = true;
myThread.Start(textBox2.Text);
}
//发送消息
private void SendMessage(object obj)
{
string message = (string)obj;
sendUdpClient = new UdpClient(0);
byte[] bytes = Encoding.Unicode.GetBytes(message);
IPEndPoint iep = new IPEndPoint(remoteip, port);
try
{
sendUdpClient.Send(bytes, bytes.Length, iep);
listBox1.Items.Add("发送的消息:" + message);
textBox2.Clear();
}
catch (Exception ex)
{
listBox1.Items.Add("发送出错:" + ex.Message);
}
}
delegate void AddItemDelegate(ListBox listbox, string text);
private void AddItem(ListBox listbox, string text)
{
if (listbox.InvokeRequired)
{
AddItemDelegate d = AddItem;
//Control.Invoke 方法 (Delegate, Object[]):
//在拥有控件的基础窗口句柄的线程上,用指定的参数列表执行指定委托。
listbox.Invoke(d, new object[] { listbox, text });
}
else
{
//Add:动态的添加列表框中的项
listbox.Items.Add(text);
//SelectedIndex属性获取单项选择ListBox中当前选定项的位置
//Count:列表框中条目的总数
listbox.SelectedIndex = listbox.Items.Count - 1;
//调用此方法等效于将 SelectedIndex 属性设置为-1。
//可以使用此方法快速取消选择列表中的所有项。
listbox.ClearSelected();
}
}
delegate void ClearTextBoxDelegate();
private void ClearTextBox()
{
if (textBox2.InvokeRequired)
{
ClearTextBoxDelegate d = ClearTextBox;
textBox2.Invoke(d);
}
else
{
textBox2.Clear();
textBox2.Focus();
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void label2_Click_1(object sender, EventArgs e)
{
}
}
}
三、wirshark
安装wireshark 抓包软件,抓取上述程序发送的网络包,对数据帧结构进行分析
前四行分别为源端口,目的端口,长度,校验和。
每一个各占2byte,一共8byte。
发送的消息为:“你好,重庆交通大学”,一共9个字符,18byte,18+8=26,即长度。