• 大小: 54KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-11
  • 语言: C#
  • 标签: C#  Sockets  TCP/IP  

资源简介

C# Sockets 通讯应用 小小的贡献啦,能给到你帮助

资源截图

代码片段和文件信息

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.Net.Sockets;
using System.IO;
using System.Threading;

namespace AsyncTcpServer
{
    public partial class FormServer : Form
    {
        private bool isExit = false;
        //保存连接的所有客户端 
        System.Collections.ArrayList clientList = new System.Collections.ArrayList();
        TcpListener listener;
        //用于线程间互操作 
        private delegate void SetListBoxCallback(string str);
        private SetListBoxCallback setListBoxCallback;
        private delegate void SetRichTextBoxCallback(string str);
        private SetRichTextBoxCallback setRichTextBoxCallback;
        private delegate void SetComboBoxCallback(string str);
        private SetComboBoxCallback setComboBoxCallback;
        private delegate void RemoveComboBoxItemsCallback(ReadWriteobject readWriteobject);
        private RemoveComboBoxItemsCallback removeComboBoxItemsCallback;
        //用于线程同步,初始状态设为非终止状态,使用手动重置方式 
        private EventWaitHandle allDone =
               new EventWaitHandle(false EventResetMode.ManualReset); 

        public FormServer()
        {
            InitializeComponent();
            listBoxStatus.HorizontalScrollbar = true;
            setListBoxCallback = new SetListBoxCallback(SetListBox);
            setRichTextBoxCallback = new SetRichTextBoxCallback(SetReceiveText);
            setComboBoxCallback = new SetComboBoxCallback(SetComboBox);
            removeComboBoxItemsCallback =
                   new RemoveComboBoxItemsCallback(RemoveComboBoxItems);
        }

        private void buttonstart_Click(object sender EventArgs e)
        {
            //由于服务器要为多个客户服务,所以需要创建一个线程监听客户端连接请求 
            ThreadStart ts = new ThreadStart(AcceptConnect);
            Thread myThread = new Thread(ts);
            myThread.Start();
            buttonstart.Enabled = false;
            buttonstop.Enabled = true; 
        }

        private void AcceptConnect()
        {
            //获取本机所有IP地址 
            IPAddress[] ip = Dns.GetHostAddresses(Dns.GetHostName());
            if (ip.Length == 0)//判斷是否是ipv6還是ipv4
            {
                listener = new TcpListener(ip[0] 51888);
            }
            else
            {
                listener = new TcpListener(ip[1] 51888);
            }
            listener.Start();
            while (isExit == false)
            {
                try
                {
                    //将事件的状态设为非终止 
                    allDone.Reset();
                    //引用在异步操作完成时调用的回调方法 
                    AsyncCallback callback = new AsyncCallback(AcceptTcpClientCallback);
                    listBoxStatus.Invoke(setListBoxCallback “开始等待客户连接“);
                    //开始一个异步操作接受传入的连接尝试 
                    listener.BeginAcceptTcpClient(

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-03-23 15:31  AsyncTcpServer\
     目录           0  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\
     文件         932  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer.sln
     文件       17408  2013-10-22 16:29  AsyncTcpServer\AsyncTcpServer.suo
     文件        3810  2012-03-23 16:11  AsyncTcpServer\AsyncTcpServer\AsyncTcpServer.csproj
     目录           0  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\bin\
     目录           0  2013-10-22 15:41  AsyncTcpServer\AsyncTcpServer\bin\Debug\
     文件       15360  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\bin\Debug\AsyncTcpServer.exe
     文件       34304  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\bin\Debug\AsyncTcpServer.pdb
     文件       14328  2013-10-22 16:29  AsyncTcpServer\AsyncTcpServer\bin\Debug\AsyncTcpServer.vshost.exe
     文件         490  2009-06-11 05:14  AsyncTcpServer\AsyncTcpServer\bin\Debug\AsyncTcpServer.vshost.exe.manifest
     文件        9033  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\FormServer.cs
     文件        8491  2012-03-23 16:11  AsyncTcpServer\AsyncTcpServer\FormServer.Designer.cs
     文件        5814  2012-03-23 16:11  AsyncTcpServer\AsyncTcpServer\FormServer.resx
     目录           0  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\obj\
     目录           0  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\obj\Debug\
     文件         658  2013-10-22 16:29  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.csproj.FileListAbsolute.txt
     文件         852  2013-10-22 15:41  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.csproj.GenerateResource.Cache
     文件       15360  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.exe
     文件         180  2013-10-22 15:41  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.FormServer.resources
     文件       34304  2013-10-22 16:03  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.pdb
     文件         180  2013-10-22 15:41  AsyncTcpServer\AsyncTcpServer\obj\Debug\AsyncTcpServer.Properties.Resources.resources
     目录           0  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\obj\Debug\Refactor\
     目录           0  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\obj\Debug\TempPE\
     文件         500  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Program.cs
     目录           0  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\
     文件        1384  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\AssemblyInfo.cs
     文件        2878  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\Resources.Designer.cs
     文件        5612  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\Resources.resx
     文件        1099  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\Settings.Designer.cs
     文件         249  2012-03-23 15:31  AsyncTcpServer\AsyncTcpServer\Properties\Settings.settings
............此处省略1个文件信息

评论

共有 条评论