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

资源简介

使用C#开发的 TCP SERVER 服务器,使用了多线程技术,从工作线程更新主线程的显示界面技术(委托)

资源截图

代码片段和文件信息

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

namespace WindowsFormsApplicationTcpServer
{
    public partial class Form1 : Form
    { 
      // Socket array = new  
        int i=0;
        Socket[] array1 = new Socket[50];
        #region//定义变量
        IPAddress HostIP = IPAddress.Parse(“127.0.0.1“);
        IPEndPoint point;
        Socket socket;
        bool flag = true;
        
        #endregion


        #region//声明委托
        
        delegate void SetTextCallback(string text);
        public void SetText(string text)
        {
            textBox2.AppendText(text + “\r\n“);
        }


        delegate void SetTreeViewCallback(string text);
        private void SetTreeView(string text)
        {
           // treeView1.BeginUpdate();

            treeView1.Nodes[0].Nodes[0].Nodes.Add(text);
            treeView1.Nodes[0].Nodes[0].Nodes[0].ImageIndex = 3;
            treeView1.SelectedImageIndex = 3;
            treeView1.ExpandAll();
           // treeView1.EndUpdate();
        }


        #endregion
        #region//进程方法

        public void UpdateTextBox(string text)
        {
            this.Invoke(new SetTextCallback(SetText) new object[] { text });
        }

        class Work_Process 
        {
            public Socket acceptedSocket;
            public Form1 f;
            public void Proccess()
            {
                if (acceptedSocket.Connected)
                {
                    while (true)
                    {
                        byte[] receiveByte = new byte[64];
                        acceptedSocket.Receive(receiveByte receiveByte.Length 0);
                        string strInfo = Encoding.GetEncoding(51936).GetString(receiveByte);
                        f.UpdateTextBox(strInfo); 
                    }
                }
            }
        }

        private void ProcessAccept()
        {
           
          
            
            while(flag)
            {
                try
                {
                    Socket sk = socket.Accept();
                    Work_Process wkp = new Work_Process();
                    wkp.f  = this;
                    wkp.acceptedSocket = sk;
                    array1[i] = sk;
                    i++;
                    this.Invoke(new SetTreeViewCallback(SetTreeView) new object[] { sk.Handle.ToString() });
                    Thread thread = new Thread(new ThreadStart(wkp.Proccess ));
                    thread.Start();
                }
            catch (Exception ey)
            {
                MessageBox.Show(ey.Message);
            }
           }
        }
        #endregion
        public Form1()
        {
     

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\
     目录           0  2013-03-09 12:08  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\
     文件         187  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\App.config
     文件        9317  2013-03-14 15:51  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Form1.Designer.cs
     文件        7110  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Form1.cs
     文件       11275  2013-03-14 15:51  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Form1.resx
     文件         544  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Program.cs
     目录           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\
     文件        1402  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\AssemblyInfo.cs
     文件        2916  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\Resources.Designer.cs
     文件        5612  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\Resources.resx
     文件        1119  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\Settings.Designer.cs
     文件         249  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\Properties\Settings.settings
     文件        3843  2013-02-27 20:36  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer.csproj
     文件         953  2013-03-09 12:08  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer.sln
     文件       39424  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer.v11.suo
     目录           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\
     目录           0  2013-02-27 20:36  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\
     文件       19456  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.exe
     文件         187  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.exe.config
     文件       34304  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.pdb
     文件       22984  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.vshost.exe
     文件         187  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.vshost.exe.config
     文件         490  2012-06-06 02:06  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\bin\Debug\WindowsFormsApplicationTcpServer.vshost.exe.manifest
     目录           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\
     目录           0  2013-03-14 16:05  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\
     文件        1308  2013-03-02 09:11  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7099  2013-03-09 10:47  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2013-03-23 17:01  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\TempPE\
     文件           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2013-02-27 20:29  WindowsFormsApplicationTcpServer\WindowsFormsApplicationTcpServer\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
............此处省略10个文件信息

评论

共有 条评论