public Tunnel(string IP, int Port, int BufferSize)
 {
     client            = new TcpClients(BufferSize);
     client.OnReceive += Client_OnReceive;
     client.OnClose   += Client_OnClose;
     client.Connect(IP, Port);
 }
Exemple #2
0
        public TcpSocketListener(ILogger logger, int clientCapacity, int maxPacketSize, int packetQueueCapacity)
        {
            this._logger = logger ?? throw new ArgumentNullException(nameof(logger));

            this.Clients      = new TcpClients(logger, clientCapacity);
            this._tcpReceiver = new TcpStreamMessageReader(logger, maxPacketSize, packetQueueCapacity);

            this.MaxPacketSize       = maxPacketSize;
            this.PacketQueueCapacity = packetQueueCapacity;
        }
        /// <inheritdoc cref="ServerCore.WaitingForClientConnection"/>
        protected override void WaitingForClientConnection()
        {
            while (true)
            {
                TcpClient client = Listener.AcceptTcpClient();
                TcpClients.Add(client);

                messageWaitingThread = new Thread(ReceiveMessage);
                messageWaitingThread.Start(client);
            }
        }
Exemple #4
0
        /// <summary>
        /// Send an object to all clients.
        /// The object will automatically be serialized to a message by the registered serializer.
        /// </summary>
        /// <param name="object">The object to send.</param>
        /// <param name="protocol">The protocol to use.</param>
        /// <returns>The sent bytes.</returns>
        async public Task Send <T>(T obj, ProtocolType protocol = ProtocolType.Tcp)
        {
            switch (protocol)
            {
            case ProtocolType.Udp:
                await Task.WhenAll(UdpEndpoints.Select(
                                       endpoint => Messenger.Send(_udpClient, endpoint, obj)
                                       )); break;

            case ProtocolType.Tcp:
            default:
                await Task.WhenAll(TcpClients.Select(
                                       client => Messenger.Send(client.GetStream(), obj)
                                       )); break;
            }
        }
Exemple #5
0
        public static void Recv(string destination_file)
        {
            Console.WriteLine("Destination file is '" + destination_file + "'. Waiting for connection...");
            string tempfile = Directory.GetCurrentDirectory() + Program.DecideWhichSlash() + destination_file + ".recv.temp";

            //Recieve file
            TcpClients.RecieveData(global_variables.use_port, tempfile);
            //fromHex calls from hexConvert.cs
            var stream = new FileStream(destination_file, FileMode.Append, FileAccess.Write);

            fromHex.convertHexStringToFile(File.ReadAllText(tempfile), stream);

            stream.Close();

            //cleanup the temp file
            File.Delete(tempfile);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            ////SocketClient调用
            //SocketClient socketClient = new SocketClient();
            //socketClient.ConnectServer();
            //socketClient.SendImage();
            //socketClient.SendMsg();

            //TcpClients调用
            TcpClients tcpClients = new TcpClients();

            #region 20190916
            //SocketClient socketClient = new SocketClient();

            //string msg = Console.ReadLine();
            //socketClient.StartServer(msg);

            ////20190916 code by ckjbug
            //Console.WriteLine("Client is running......");
            //TcpClient tcpClient;
            //for (int i = 0; i < 10000; i++)
            //{
            //    try
            //    {
            //        tcpClient = new TcpClient();
            //        tcpClient.Connect("localhost", 8500);
            //        Console.WriteLine("Server Connected!{0} --> {1}",
            //            tcpClient.Client.LocalEndPoint.ToString(), tcpClient.Client.RemoteEndPoint.ToString());
            //    }
            //    catch (Exception e)
            //    {
            //        Console.WriteLine(e.ToString());
            //        Console.Read();
            //        return;
            //    }
            //}
            //Console.Read();
            #endregion
        }
Exemple #7
0
        public static void Send(string target_file)
        {
            Console.WriteLine("Preparing file for sending...");
            //toHex calls from hexConvert.cs#
            toHex.fileToHex(target_file); //convert target_file to hex

            //Create data stream
            string tempfile = (Directory.GetCurrentDirectory() + Program.DecideWhichSlash() + target_file + ".send.temp");

            Console.WriteLine("Uploading to '" + global_variables.server_addr + "' on port " + global_variables.server_port + ".");
            TcpClients.Upload(global_variables.server_addr, global_variables.server_port, tempfile); //load converted file then send it to server

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("Cleaning up...");

            //cleanup the temp file
            try { File.Delete(tempfile); }
            catch (IOException)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error cleaning up temporary file '" + tempfile + "'. You may manually remove this file.");
            }
        }
Exemple #8
0
        public FileClient(uint headerFlag = 0xff, bool isOverride = false)
        {
            _override = isOverride;
            if (headerFlag < 0 || headerFlag > 1023)
            {
                headerFlag = 0;
            }
            this._headerFlag = headerFlag;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                _queue                 = new FileDataContainer();
                _tcpClients            = new TcpClients(1024);
                _tcpClients.OnConnect += TCPClients_OnConnect;
                _tcpClients.OnReceive += TCPClients_OnReceive;
                _tcpClients.OnClose   += TCPClients_OnClose;
                _tcpClients.OnSend    += TCPClients_OnSend;
            }));

            thread.IsBackground = true;
            thread.Start();
            _recievedThread = new Thread(HandleMessageThread);
            _recievedThread.IsBackground = true;
            _recievedThread.Start();
        }
 private int countTrackers(TcpClients tcpClients)
 {
     int count = 0;
     foreach (TcpTracker tcpTracker2 in tcpClients.Values) {
         if (!String.IsNullOrEmpty(tcpTracker2.Imei)) {
             count++;
         }
     }
     return count;
 }
Exemple #10
0
 /// <summary>
 /// Determine if a TCP client is handled by the server.
 /// </summary>
 /// <param name="client">The TCP client.</param>
 public bool ContainsClient(TcpClient client) => TcpClients.Contains(client);
Exemple #11
0
        public void Start()
        {
            try {
                this.Id = 0;
                this.TcpClients = new TcpClients();

                if (tcpListener == null) {
                    tcpListener = new TcpListener(new IPEndPoint(IPAddress.Parse(this.IpAddress), this.Port));
                }

                tcpListener.Start();
                tcpListener.BeginAcceptTcpClient(new AsyncCallback(asyncBeginAccept), tcpListener);

                this.IsActivated = true;
                Log serverLog = new Log("Server starts...", LogType.SERVER);
                Event(this, serverLog);
            } catch (Exception exception) {
                this.IsActivated = false;
                Log serverLog = new Log(exception.Message, LogType.SERVER);
                Event(this, serverLog);

                if (tcpListener != null) {
                    this.Stop();
                }
                triggerEvent(new Log(exception.Message, LogType.SERVER));
                throw exception;
            }
        }