Ejemplo n.º 1
0
        public SocketCpp Accept()
        {
            AcceptPara para = new AcceptPara();

            SocketCpp.getAccept(this.socket, ref para);

            SimpleSocketException ex;

            if (para.errorCode != 0)
            {
                ex = new SimpleSocketException(
                    "Accept 发生错误, errorCode:" + para.errorCode,
                    para.errorCode);

                throw ex;
            }

            return(new SocketCpp(para.socket));
        }
Ejemplo n.º 2
0
        static void Receive(object obj)
        {
            SocketCpp socket = (SocketCpp)obj;

            byte[] bytes;
            int    receiveLength;
            int    sentLength;

            try
            {
                while (true)
                {
                    bytes = new byte[32];

                    receiveLength = socket.Receive(bytes);

                    //   receiveLength == 0  表示 客户端 连接 已关闭
                    if (receiveLength == 0)
                    {
                        Console.WriteLine("receiveLength == 0");
                        break;
                    }

                    sentLength = socket.Send(new byte[] { 98, 98 });
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


            Console.WriteLine("close socket");

            if (socket != null)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
        }
Ejemplo n.º 3
0
        public int Send(byte[] buffer)
        {
            SendPara para = new SendPara();

            para.socket       = this.socket;
            para.bufferLength = buffer.Length;

            SocketCpp.getSend(ref para, buffer);

            SimpleSocketException ex;

            if (para.errorCode != 0)
            {
                ex = new SimpleSocketException(
                    "Send 发生错误, errorCode:" + para.errorCode,
                    para.errorCode);

                throw ex;
            }

            return(para.resultLength);
        }
Ejemplo n.º 4
0
        public static SocketCpp CreateAndListen(string host, string port)
        {
            CreatePara para = new CreatePara();

            para.host = host;
            para.port = port;

            SocketCpp.getSocket(ref para);

            SimpleSocketException ex;

            if (para.errorCode != 0)
            {
                ex = new SimpleSocketException(
                    "CreateAndListen 发生错误, errorCode:" + para.errorCode,
                    para.errorCode);

                throw ex;
            }

            return(new SocketCpp(para.socket));
        }
Ejemplo n.º 5
0
        static void Listen()
        {
            SocketCpp socket = null;

            SocketCpp clientSocket;

            Thread thread;

            try
            {
                socket = SocketCpp.CreateAndListen("127.0.0.1", "9527");

                while (true)
                {
                    clientSocket = socket.Accept();

                    thread = new Thread(Receive);

                    thread.Start(clientSocket);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Listen while error: " + ex.ToString());
            }


            Console.WriteLine("Listen Close.");


            if (socket != null)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            SocketCpp.WSACleanup();
        }