Esempio n. 1
0
        static void Main(string[] args)
        {
            LoadProperties();
            int i = 0;

            while (i <= SystemProperties.TotalClients)
            {
                try
                {
                    //  string[] serverInfo = getConnectionString();

                    if (SystemProperties.ServerIP == null || SystemProperties.ServerPort == 0)
                    {
                        Console.WriteLine("No Server Available for this");
                    }
                    else
                    {
                        // Convert.ToInt32(serverInfo[1])
                        System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

                        clientSocket.ReceiveBufferSize = 1024000;


                        // Set the send buffer size to 8k.
                        clientSocket.SendBufferSize = 1024000;
                        clientSocket.Connect(SystemProperties.ServerIP, SystemProperties.ServerPort);
                        // clientSocket.Connect(serverInfo[0], Convert.ToInt32(serverInfo[1]));

                        MyTcpClient newClient = new MyTcpClient(clientSocket);


                        clientList.Add(newClient);
                        ClientsList.TryAdd(newClient.getID(), newClient);


                        byte[] data   = new Byte[100];
                        string text   = newClient.getID().ToString();
                        var    buffer = Encoding.UTF8.GetBytes(text);
                        Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length);
                        newClient.TcpClient.GetStream().Write(data, 0, data.Length);

                        clientSocket.GetStream().BeginRead(newClient.bytes, 0, newClient.bytes.Length, new System.AsyncCallback(AsyncRead), newClient);
                    }
                    i++;
                }
                catch (Exception ex)
                {
                    threadcount--;
                    //  Console.WriteLine("Exception at CConnecting Time "+ex.GetBaseException());
                }
            }
            Console.WriteLine("Clients Connected ");
            SetupLogger();
            SetupPingService();
            Console.WriteLine("Press Enter to send messages ");
            Console.ReadKey();

            SetupBroker();
        }
Esempio n. 2
0
        public static void ReconnectConnection(MyTcpClient socket)
        {
            try
            {
                if (SystemProperties.ServerIP == null || SystemProperties.ServerPort == 0)
                {
                    Console.WriteLine("No Server Available for this");
                }
                else
                {
                    System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();

                    clientSocket.ReceiveBufferSize = 1024000;


                    // Set the send buffer size to 8k.
                    clientSocket.SendBufferSize = 1024000;
                    clientSocket.Connect(SystemProperties.ServerIP, SystemProperties.ServerPort);


                    socket.setClient(clientSocket);
                    byte[] data = new Byte[100];
                    string text = socket.getID().ToString();

                    var buffer = Encoding.UTF8.GetBytes(text);
                    Buffer.BlockCopy(buffer, 0, data, 0, buffer.Length);
                    clientSocket.GetStream().Write(data, 0, data.Length);

                    lock (recQueueObj)
                    {
                        socket.flag = true;
                    }

                    lock (ClientListLock)
                    {
                        if (ClientsList.ContainsKey(socket.getID()))
                        {
                            MyTcpClient otp;
                            ClientsList.TryRemove(socket.getID(), out otp);
                            ClientsList.TryAdd(socket.getID(), socket);
                        }
                        else
                        {
                            ClientsList.TryAdd(socket.getID(), socket);
                        }
                    }

                    clientSocket.GetStream().BeginRead(socket.bytes, 0, socket.bytes.Length, new System.AsyncCallback(AsyncRead), socket);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Something Wrong with Reconnection ");
            }
        }
Esempio n. 3
0
        public static void AsyncRead(IAsyncResult ar)
        {
            MyTcpClient mysocket = (MyTcpClient)ar.AsyncState;

            try
            {
                int bytesRead = mysocket.TcpClient.GetStream().EndRead(ar);
                if (bytesRead == mysocket.bytes.Length)
                {
                    mysocket.offset   = 0;
                    mysocket.ReadSize = mysocket.bytes.Length;

                    byte[] Byte = mysocket.bytes;
                    lock (mainQueueObj)
                    {
                        Interlocked.Increment(ref recMsg);
                        messageQueue.Enqueue(Byte);
                        mysocket.bytes = new byte[212];
                        MsgEvent.Set();
                    }
                    mysocket.TcpClient.GetStream().BeginRead(mysocket.bytes, 0, mysocket.bytes.Length, new System.AsyncCallback(AsyncRead), mysocket);
                }
                else
                {
                    mysocket.offset   = mysocket.offset + bytesRead;
                    mysocket.ReadSize = mysocket.ReadSize - bytesRead;

                    mysocket.TcpClient.GetStream().BeginRead(mysocket.bytes, mysocket.offset, mysocket.ReadSize, new System.AsyncCallback(AsyncRead), mysocket);
                }
            }
            catch (Exception ex)
            {
                lock (recQueueObj)
                {
                    mysocket.flag = false;
                }
                return;
            }
        }