• 大小: 62KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-14
  • 语言: C#
  • 标签:

资源简介

本代码包括了TCP和UDP的客户端和服务端,适合C#初学者学习、参考

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NetWork
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IPAddress ip;
        IPEndPoint point = new IPEndPoint(IPAddress.Any0);
        string protocol;
        Socket aimSocket;
        Socket mySocket;
        UdpClient RecUdpClient;
        //IPEndPoint remote;
        private void Form1_Load(object sender EventArgs e)//初始化为UDP Server模式
        {
            cobProtocol.SelectedIndex = 0;
            txtIP.Text = GetAddressIP();
            Control.CheckForIllegalCrossThreadCalls = false;
        }
        string GetAddressIP()
        {
            string AddressIP = ““;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == “InterNetwork“)
                {
                    AddressIP = _IPAddress.ToString();
                    ip = _IPAddress;//设定全局的IP
                }
            }
            return AddressIP;
        }

        private void btnStart_Click(object sender EventArgs e)//设置目标IP(Client),本地IP(Server)
        {
            try
            {
                if (protocol == “UDP Server“)
                {
                    aimSocket = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);
                    RecUdpClient = new UdpClient(new IPEndPoint(ip Convert.ToInt32(txtPort.Text)));
                    MessageBox.Show(“UDP Server创建成功“);
                    Thread thReceive = new Thread(USReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }
                else if (protocol == “UDP Client“)//UPD客户端,目标IP和端口  调用Socket.SendTo
                {
                    aimSocket = new Socket(AddressFamily.InterNetwork SocketType.Dgram ProtocolType.Udp);//建立通信的Socket
                    ip = IPAddress.Parse(txtIP.Text);
                    point = new IPEndPoint(ip Convert.ToInt32(txtPort.Text));
                    MessageBox.Show(“UDP Client设置成功“);
                    Thread thReceive = new Thread(UCReceive);
                    thReceive.IsBackground = true;
                    thReceive.Start();
                }
                else if (protocol == “TCP Server“)
                {
                    mySocket = new Socket(AddressFamily.InterNetwork SocketType.Stream ProtocolType.Tcp);
                    point = new IPEndPoint(ip Convert.ToInt32(txtPort.Text));//绑定端口号
                    mySocket.Bind(point);//绑定监听端口
                    MessageBox.Show(“TCP Server绑定成功“);
      

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-09-18 10:57  NetWork\
     目录           0  2015-09-18 17:36  NetWork\NetWork\
     文件         990  2015-09-18 10:57  NetWork\NetWork.sln
     文件       27648  2015-09-18 17:36  NetWork\NetWork.v12.suo
     文件         187  2015-09-18 10:57  NetWork\NetWork\App.config
     目录           0  2015-09-18 10:57  NetWork\NetWork\bin\
     目录           0  2015-09-18 11:31  NetWork\NetWork\bin\Debug\
     文件       13824  2015-09-18 17:36  NetWork\NetWork\bin\Debug\NetWork.exe
     文件         187  2015-09-18 10:57  NetWork\NetWork\bin\Debug\NetWork.exe.config
     文件       30208  2015-09-18 17:36  NetWork\NetWork\bin\Debug\NetWork.pdb
     文件       24224  2015-09-18 17:36  NetWork\NetWork\bin\Debug\NetWork.vshost.exe
     文件         187  2015-09-18 10:57  NetWork\NetWork\bin\Debug\NetWork.vshost.exe.config
     文件         490  2014-01-13 21:31  NetWork\NetWork\bin\Debug\NetWork.vshost.exe.manifest
     文件        8151  2015-09-18 17:36  NetWork\NetWork\Form1.cs
     文件        8055  2015-09-18 11:30  NetWork\NetWork\Form1.Designer.cs
     文件        5817  2015-09-18 11:31  NetWork\NetWork\Form1.resx
     文件        3794  2015-09-18 11:31  NetWork\NetWork\NetWork.csproj
     目录           0  2015-09-18 10:57  NetWork\NetWork\obj\
     目录           0  2015-09-18 17:36  NetWork\NetWork\obj\Debug\
     文件         865  2015-09-18 10:57  NetWork\NetWork\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6987  2015-09-18 10:57  NetWork\NetWork\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         726  2015-09-18 17:36  NetWork\NetWork\obj\Debug\NetWork.csproj.FileListAbsolute.txt
     文件         977  2015-09-18 11:31  NetWork\NetWork\obj\Debug\NetWork.csproj.GenerateResource.Cache
     文件        2211  2015-09-18 11:31  NetWork\NetWork\obj\Debug\NetWork.csprojResolveAssemblyReference.cache
     文件       13824  2015-09-18 17:36  NetWork\NetWork\obj\Debug\NetWork.exe
     文件         180  2015-09-18 11:31  NetWork\NetWork\obj\Debug\NetWork.Form1.resources
     文件       30208  2015-09-18 17:36  NetWork\NetWork\obj\Debug\NetWork.pdb
     文件         180  2015-09-18 11:31  NetWork\NetWork\obj\Debug\NetWork.Properties.Resources.resources
     文件           0  2015-09-18 10:57  NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2015-09-18 10:57  NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2015-09-18 10:57  NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略8个文件信息

评论

共有 条评论

相关资源