コード例 #1
0
ファイル: Client.cs プロジェクト: maziesmith/CSCourseCode
        private void ThreadLoop()
        {
            while (DateTime.Now < end)
            {
                try
                {
                    Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    s.Connect(new IPEndPoint(IPAddress.Loopback, 8081));

                    SocketHelper.WriteMessage(s, "Garry");
                    SocketHelper.ReadMessage(s);
                    s.Shutdown(SocketShutdown.Both);
                    s.Close();

                    SocketHelper.Increment();
                }
                catch (SocketException se)
                {
                    Console.WriteLine(se);
                    Console.ReadLine();
                    // local address at e
                }
                Thread.Sleep(threadWait);
            }
        }
コード例 #2
0
        // Process the request from an accepted socket
        public static void ProcessConnection(object o)
        {
            // Had to have object to make signature compatible with WaitCallback
            Socket s = (Socket)o;

            // get the message
            string name = (string)SocketHelper.ReadMessage(s);

            // process the message
            System.Threading.Thread.Sleep(500);             // simulate the database work

            // send the response
            SocketHelper.WriteMessage(s, "Goodbye " + name);

            // client will close the socket
            SocketHelper.ReadMessage(s);             // will be null
            s.Shutdown(SocketShutdown.Both);
            s.Close();

            if (rc != null)
            {
                Increment();
            }
        }