• 大小: 33KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-05
  • 语言: 其他
  • 标签: TCP?ip  

资源简介

不错的TCP协议开发 实现上位机网络通信,很好用,直接运行可用

资源截图

代码片段和文件信息

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

namespace Host
{
    public partial class Form1 : Form
    {

        TcpListener myListener;

        private Thread Communi = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender EventArgs e)
        {
            String Result1 = ““;
            String str1;

            String hostName1 = Dns.GetHostName();
            IPAddress[] myIP1 = Dns.GetHostAddresses(hostName1);
            foreach (IPAddress address1 in myIP1)
            {
                str1 = address1.ToString();
                for (int i = 0; i < str1.Length; i++)
                {
                    if ((str1[i] >= ‘0‘) && (str1[i] <= ‘9‘ || str1[i] == ‘.‘))
                        Result1 = str1;
                }
            }

            IPAddress ipa1 = IPAddress.Parse(Result1);

          
           
            myListener = new TcpListener(ipa1 8001);
            myListener.Start();

            Communi = new Thread(new ThreadStart(this.Communicate));
            Communi.IsBackground = true;
            Recive_TextBox.Text = string.Empty;
            Communi.Start();
         
        }

        public void Communicate()
        {
            try
            {

                while (true)
                {
                    if (myListener.Pending() == true)
                    {
                        Socket mySocket = myListener.AcceptSocket();
                    //    (mySocket.RemoteEndPoint as IPEndPoint).IPAddress;
                        IPAddress IP = (mySocket.RemoteEndPoint as IPEndPoint).Address;

                        textBox1.Text = IP.ToString();
                        byte[] receive_databyte=new byte[50];


                    
                        mySocket.Receive(receive_databyte);
                        String content = Encoding.Unicode.GetString(receive_databyte);
                        
                        Recive_TextBox.Text += System.DateTime.Now.ToString() + “  “ + IP.ToString() + “\n说:“ + content;

                        
                        
                        byte[] send_databyte=Encoding.Unicode.GetBytes(“我爱你“);
                        mySocket.Send(send_databyte);
                     //   mySocket.Send(Encoding.Unicode.GetBytes(send_message));
                        // MessageBox.Show(“I love you“);
                      
                        
                    }
                    Thread.Sleep(10);




                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        901  2011-04-08 14:39  TCP\tcp\Host.sln

    ..A..H.     17408  2011-04-13 17:34  TCP\tcp\Host.suo

     文件      20480  2011-04-08 17:10  TCP\tcp\tcp\bin\Debug\Host.exe

     文件      22016  2011-04-08 17:10  TCP\tcp\tcp\bin\Debug\Host.pdb

     文件       5632  2005-12-08 14:51  TCP\tcp\tcp\bin\Debug\Host.vshost.exe

     文件       2981  2011-04-11 17:33  TCP\tcp\tcp\Form1.cs

     文件       4248  2011-04-08 15:31  TCP\tcp\tcp\Form1.Designer.cs

     文件       5814  2011-04-08 15:31  TCP\tcp\tcp\Form1.resx

     文件       3215  2011-04-08 15:31  TCP\tcp\tcp\Host.csproj

     文件        842  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug\Host.csproj.GenerateResource.Cache

     文件      20480  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug\Host.exe

     文件        180  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug\Host.Form1.resources

     文件      22016  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug\Host.pdb

     文件        180  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug\Host.Properties.Resources.resources

     文件        687  2011-04-13 13:33  TCP\tcp\tcp\obj\Host.csproj.FileListAbsolute.txt

     文件        463  2011-04-08 14:39  TCP\tcp\tcp\Program.cs

     文件       1166  2011-04-08 14:39  TCP\tcp\tcp\Properties\AssemblyInfo.cs

     文件       2864  2011-04-08 14:39  TCP\tcp\tcp\Properties\Resources.Designer.cs

     文件       5612  2011-04-08 14:39  TCP\tcp\tcp\Properties\Resources.resx

     文件       1089  2011-04-08 14:39  TCP\tcp\tcp\Properties\Settings.Designer.cs

     文件        249  2011-04-08 14:39  TCP\tcp\tcp\Properties\Settings.settings

     目录          0  2011-04-08 14:43  TCP\tcp\tcp\obj\Debug\Refactor

     目录          0  2011-04-08 14:39  TCP\tcp\tcp\obj\Debug\TempPE

     目录          0  2011-04-08 17:10  TCP\tcp\tcp\bin\Debug

     目录          0  2011-04-08 17:10  TCP\tcp\tcp\obj\Debug

     目录          0  2011-04-08 14:39  TCP\tcp\tcp\bin

     目录          0  2011-04-08 17:10  TCP\tcp\tcp\obj

     目录          0  2011-04-08 14:39  TCP\tcp\tcp\Properties

     目录          0  2011-04-11 17:33  TCP\tcp\tcp

     目录          0  2018-09-07 11:13  TCP\tcp

............此处省略4个文件信息

评论

共有 条评论

相关资源