• 大小: 409KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C#
  • 标签: C#Socket  Socket通信  

资源简介

一款C#开发的稳定完整版的Socket通信应用实例

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Wodsoft.Net.Sockets;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace Wodsoft.Net.Communication
{
    public class Client : Communicationbase
    {
        // Fields
        private byte[] _ConnectHead;
        private bool _Connecting;

        // Events
        public event EventHandler ConnectCompleted;

        // Methods
        public Client()
            : base(new TCPClient())
        {
            TCPClient client = (TCPClient)base.Socket;
            client.ConnectCompleted += client_ConnectCompleted;
            client.ReceiveCompleted += client_ReceiveCompleted;
            client.DisconnectCompleted += client_DisconnectCompleted;
        }

        private void client_ConnectCompleted(object sender SocketEventArgs e)
        {
            if (e.Socket.IsConnected)
            {
                List data = new List { 0 };
                if (_ConnectHead == null)
                {
                    data.Add(0);
                    data.Add(0);
                }
                else
                {
                    data.AddRange(BitConverter.GetBytes((ushort)_ConnectHead.Length));
                    data.AddRange(_ConnectHead);
                }
                if (Credential != null)
                {
                    if (Credential.Username == null)
                    {
                        data.Add(0);
                        data.Add(0);
                    }
                    else
                    {
                        data.AddRange(BitConverter.GetBytes((ushort)Credential.Username.Length));
                        data.AddRange(Credential.Username);
                    }
                    if (Credential.Password != null)
                    {
                        data.AddRange(BitConverter.GetBytes((ushort)Credential.Password.Length));
                        data.AddRange(Credential.Password);
                    }
                }
                e.Socket.SendAsync(data.ToArray());
            }
            else
            {
                _Connecting = false;
                CommunicationConnectEventArgs eventArgs = new CommunicationConnectEventArgs(this false null);
                if (ConnectCompleted != null)
                {
                    ConnectCompleted(this eventArgs);
                }
            }
        }

        private void client_DisconnectCompleted(object sender SocketEventArgs e)
        {
            if (_Connecting)
            {
                _Connecting = false;
            }
        }

        private void client_ReceiveCompleted(object sender SocketEventArgs e)
        {
            if (((e.Data[0] == 0) && (e.DataLength >= 2)) && _Connecting)
            {
       

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

     文件      76288  2013-05-14 19:35  Wodsoft.Net\bin\Debug\Wodsoft.Net.dll

     文件     202240  2013-05-14 19:35  Wodsoft.Net\bin\Debug\Wodsoft.Net.pdb

     文件      69632  2013-05-28 10:22  Wodsoft.Net\bin\Release\Wodsoft.Net.dll

     文件     212480  2013-05-28 10:22  Wodsoft.Net\bin\Release\Wodsoft.Net.pdb

     文件       4389  2013-04-21 15:39  Wodsoft.Net\Communication\Client.cs

     文件      10335  2013-04-25 16:52  Wodsoft.Net\Communication\Communicationbase.cs

     文件       3258  2013-04-21 15:39  Wodsoft.Net\Communication\CommunicationEventArgs.cs

     文件       1198  2013-04-21 15:39  Wodsoft.Net\Communication\CoreServer.txt

     文件        444  2013-04-21 15:39  Wodsoft.Net\Communication\Credential.cs

     文件        714  2013-04-21 15:39  Wodsoft.Net\Communication\ICommunication.cs

     文件       5855  2013-04-21 15:39  Wodsoft.Net\Communication\Server.cs

     文件        702  2013-04-21 15:39  Wodsoft.Net\Communication\ServerAuthentication.cs

     文件        594  2013-04-21 15:39  Wodsoft.Net\Communication\ServerClient.cs

     文件     157184  2013-04-21 15:39  Wodsoft.Net\Communication\流程图.vsd

     文件       6556  2013-04-26 09:37  Wodsoft.Net\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        418  2013-05-14 19:35  Wodsoft.Net\obj\Debug\Wodsoft.Net.csproj.FileListAbsolute.txt

     文件       1602  2013-05-14 19:35  Wodsoft.Net\obj\Debug\Wodsoft.Net.csprojResolveAssemblyReference.cache

     文件      76288  2013-05-14 19:35  Wodsoft.Net\obj\Debug\Wodsoft.Net.dll

     文件     202240  2013-05-14 19:35  Wodsoft.Net\obj\Debug\Wodsoft.Net.pdb

     文件       6560  2013-05-28 09:41  Wodsoft.Net\obj\Release\DesignTimeResolveAssemblyReferencesInput.cache

     文件        428  2013-04-25 16:53  Wodsoft.Net\obj\Release\Wodsoft.Net.csproj.FileListAbsolute.txt

     文件      11752  2013-04-25 11:07  Wodsoft.Net\obj\Release\Wodsoft.Net.csprojResolveAssemblyReference.cache

     文件      69632  2013-05-28 10:22  Wodsoft.Net\obj\Release\Wodsoft.Net.dll

     文件     212480  2013-05-28 10:22  Wodsoft.Net\obj\Release\Wodsoft.Net.pdb

     文件       1366  2013-04-21 15:39  Wodsoft.Net\Properties\AssemblyInfo.cs

     文件       4129  2013-04-21 15:39  Wodsoft.Net\Service\BinaryDataReader.cs

     文件       2472  2013-04-21 15:39  Wodsoft.Net\Service\BinaryDataWriter.cs

     文件       1663  2013-04-21 15:39  Wodsoft.Net\Service\ChannelFactory.cs

     文件        234  2013-04-21 15:39  Wodsoft.Net\Service\Credential.cs

     文件       3898  2013-04-21 15:39  Wodsoft.Net\Service\DataFormatter.cs

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

评论

共有 条评论