制作适当的TcpClient服务器C#

因此,我做了一个扩展的库,以将信息从客户端传输到服务器,但是自从稳定版本以来,我做了一些大的更改,包括多线程(其中在连接到其他多个TcpServer的多个端口上打开了多个TcpClient)这将是快速发送数据的关键功能。我在GitHub上有一个稳定的版本,并且对所有更改进行了实验。据我所知,它与尝试在每个服务器上尝试连接(library / Client.cs)的新任务有关,尽管我不知道有其他解决方法。任何帮助将不胜感激。

GitHub Project

“ TCP-Communication / libraries / SharedCode”是所有有用类的位置

预览:

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using static ClientServer.EncodingClasses;
using System.Threading;
using System.Linq;

namespace ClientServer
{
    public class Client
    {

        public ConnectionArguments args;//connection arguments
        NetworkData overread;//data storage for overread data
        public Queue<ClientMessage> command_queue = new Queue<ClientMessage>();
        public action<string,int> debug;//message and level  1=surface,2=base events,3=debug data
        public Client(ConnectionArguments args1)
        {
            args = args1;
            if (debug == null) debug = new action<string,int>((x,y) => { });
        }
        //-----------------
        public void Communicate(string operation,NetworkData message,action<NetworkData> onCompleted,action failed = null)
        {
            ClientMessage cm = new ClientMessage();
            cm.failed = failed;
            cm.completed = onCompleted;
            cm.operation = operation;
            cm.message = message;
            command_queue.Enqueue(cm);
            debug("[SYS]: added operation to queue",1);
        }

        List<TcpClient> clients = new List<TcpClient>();
        public void clientThread(out action start)
        {
            for (int x = 0; x < args.ports.Count; x++)
            {
                debug($"[SYS]: initialized port {args.ports[x]}",1);
                clients.Add(new TcpClient());
            }

            start = new action(() =>
            {

                //try connecting


                while (true)
                {


                    if (command_queue.Count > 0)
                    {

                        for (int i = 0; i < clients.Count; i++)
                        {
                            if (command_queue.Count == 0)
                                break;
                                int port = args.ports[i];
                            TcpClient client = clients[i];
                            if (client.Connected)
                            {
                                debug($"[{port}]: busy",2);
                                continue;
                            }
                            client.Connect(args.ip,args.ports[i]);//connect to server
                            ClientMessage cm = command_queue.Dequeue();//get args
                            debug($"[{port}]: communication pulled",1);

                            string msg = "";
                            if (cm.message != null)
                            {
                                msg = cm.message.GetDecodedString();//initialize message
                            }
                            NetworkData dat = NetworkData.fromEncodedString(String.Join(SendRecieveUtil.separator,new List<string>() { cm.operation,msg }.Select(x => NetworkData.fromDecodedString(x).GetEncodedString())));
                            dat.InitStream();//initialize encoded data

                            SendRecieveUtil.SendBytes(client,dat,args,port);//send bytes
                            debug($"[{port}]: Sending bytes",1);
                            int length = 0;
                            SendRecieveUtil.RecieveBytes(client,ref overread,null,new List<RetrievalNode>(){new RetrievalNode()
                            {

                            direct = (x) =>//length
                            {
                                if(x != null)
                                length = int.Parse(x.GetDecodedString());
                                debug($"[{port}]: length={length}",3);
                            }
                            },});//recieve bytes and get length

                            SendRecieveUtil.RecieveBytes(client,length,cm.progress,new List<RetrievalNode>(){new RetrievalNode()
                            {

                            direct=(x)=>{ cm.completed(x); client.Close(); debug($"[{port}]: Client closed",1); debug($"[{port}]: data={x.GetDecodedString()}",3); },motive="message"
                            } });//get final data and close client


                        }
                    }

                }

            });

        }
    }
}
a714618777 回答:制作适当的TcpClient服务器C#

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2855676.html

大家都在问