Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            var ds = new DefaultServer(5585);

            ds.Start();

            var channelServer = new DefaultServer(5555);

            channelServer.Start();

            while (true)
            {
                Thread.Sleep(5000);
            }

            return;

            TcpListener serverSocket = new TcpListener(5585);
            int         requestCount = 0;
            TcpClient   clientSocket = default(TcpClient);

            serverSocket.Start();
            Console.WriteLine(" >> Server Started");
            clientSocket = serverSocket.AcceptTcpClient();
            Console.WriteLine(" >> Accept connection from client");
            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    byte[]        bytesFrom     = new byte[65536];
                    Console.WriteLine("Received: " + clientSocket.ReceiveBufferSize);
                    networkStream.Read(bytesFrom, 0, 4);
                    //networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    Console.WriteLine(HexDump(bytesFrom));

                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine(" >> Data from client - " + dataFromClient);

                    /*string serverResponse = "Last Message from client" + dataFromClient;
                     * Byte[] sendBytes = Encoding.ASCII.GetBytes(serverResponse);
                     * networkStream.Write(sendBytes, 0, sendBytes.Length);
                     * networkStream.Flush();
                     * Console.WriteLine(" >> " + serverResponse);*/
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine(" >> exit");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        public Client(TcpClient tcp, DefaultServer parent)
        {
            _tcp    = tcp;
            _parent = parent;

            _ns        = tcp.GetStream();
            _connected = true;

            /*
             *  unsigned short	size;
             * unsigned short	code;
             * S_C_ACCEPT
             *  UINT8	nCode;	// Á¢¼ÓÇÑ ¼­¹ö ±¸ºÐ?
             * WCHAR	szClientIP[16];	// ä³Î ¼­¹ö¿¡¼­ º¸³»´Â °Í¸¸ À¯È¿
             */

            var ack = new Packet(CommonProtocolType._S2C_ACCEPT);

            if (parent.Port == 5585)
            {
                ack.Writer.Write((byte)0);
            }
            else
            {
                ack.Writer.Write((byte)1);
            }
            ack.Writer.WriteUnicodeStatic(EndPoint.Address.ToString(), 16);
            Send(ack);

            try
            {
                _buffer      = new byte[4];
                _bytesToRead = _buffer.Length;
                _ns.BeginRead(_buffer, 0, 4, OnHeader, null);
            }
            catch (Exception ex)
            {
                KillConnection(ex);
            }
        }