欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C#入门编程

程序员文章站 2022-07-16 19:32:17
...

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一.实验目的

1.用C#编写一个命令行/控制台hello world程序,实现如下功能:在屏幕上连续输出50行“hello cqjtu!重交物联2018级”;同时打开一个网络UDP 套接字,向室友电脑或树莓派发送这50行消息。
2.用VS2015/2017 的C#编写一个简单的Form窗口程序,有一个文本框 textEdit和一个发送按钮button,运行程序后,可以在文本框里输入文字,如“hello cqjtu!重交物联2018级”,点击button,将这些文字发送给室友电脑或树莓派,采用UDP套接字;
3.安装wireshark 抓包软件,抓取上述程序发送的网络包,对数据帧结构进行分析。

二.实验环境

Windows10, Virtual Studio2015

三.控制台程序使用 UDP 通信

1.打开vs2015
C#入门编程
2.代码

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPSender

{
  class program
  {
  static void Main(String[] args)
  {
      int flag = 50;
    byte[] data = new byte[1024];
    string input,stringData;
    Console.WriteLine("this is a Client.host name is {0}",Dns.GetHostName());
    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"),8001);
    Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
    data = Encoding.UTF8.GetBytes(welcome);
    server.SendTo(data,data.Length,SocketFlags.None,ipep);
    IPEndPoint sender = new IPEndPoint(IPAddress.Any,0);
    EndPoint Remote = (EndPoint)sender;
    data = new byte[1024];
    int recv = server.ReceiveFrom(data,ref Remote);
    Console.WriteLine("Message received from {0}: ", Remote.ToString());
    Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
    while(flag>0)
    {
           input = "Hello cqjtu!重交物联18级";
      server.SendTo(Encoding.UTF8.GetBytes(input),Remote);
      data = new byte[1024];
      recv = server.ReceiveFrom(data,ref Remote);
      stringData = Encoding.UTF8.GetString(data,0,recv);
      Console.WriteLine(stringData);
      flag--;
    }
  }
}
}

服务端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace UDPReceive

{
  class Program
  {
    static void Main(String[] args)
    {
      int recv;
      byte[] data = new byte[1024];
      IPEndPoint ipep = new IPEndPoint(IPAddress.Any,8001);
      Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
      newsock.Bind(ipep);
      Console.WriteLine("This is a Server, host name is {0}", Dns.GetHostName());
      Console.WriteLine("Waiting for a client");
      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.UTF8.GetString(data,0,recv));
      data = Encoding.UTF8.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.UTF8.GetString(data,0,recv));
        newsock.SendTo(data,recv,SocketFlags.None,Remote);
      }
    }
  }
}

3.运行
C#入门编程

四.Form窗口程序

1.打开
C#入门编程

2.代码

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;
namespace clientForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string stringdate;
            byte[] date = new byte[1024];
            IPEndPoint ippo = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);          
            stringdate = textBox1.ToString();
            date = Encoding.UTF8.GetBytes(stringdate);
            server.SendTo(date,date.Length,SocketFlags.None,ippo);
        }
    }
}

3.运行
C#入门编程
C#入门编程