예제 #1
0
파일: Program.cs 프로젝트: twoood/CS570
        private static void SendMessage(the_server server)
        {
            // setup new timer to 1 second
            aTimer = new System.Timers.Timer(1000);
            while (true)
            {
                // Hook up CheckRetrieve for the timer, so it will be constantly called.
                aTimer.Elapsed  += CheckRetrieve;
                aTimer.AutoReset = true;
                aTimer.Enabled   = true;



                // Ask for user to grant input, if they enter nothing, keep allowing input
                Console.WriteLine("Enter a message, specify a client id in order to send message to client, or enter q to quit:");
                string message = Console.ReadLine();
                while (message.Length == 0)
                {
                    message = Console.ReadLine();
                }
                // if q is selected, disconnect client
                if (message == "q")
                {
                    Disconnect();
                }
                byte[] buffer = Encoding.ASCII.GetBytes(message);     // encode message into byte array
                mysock.Send(buffer, buffer.Length, SocketFlags.None); // send the byte buffer to server
            }
        }
예제 #2
0
파일: Program.cs 프로젝트: twoood/CS570
        static void Main(string[] args)
        {
            Console.Title = "Client";
            the_server my_server = new the_server();
            string     connection_information = ReachOut();
            int        position = connection_information.IndexOf(':');
            string     temp     = connection_information.Substring(position + 1);

            Console.WriteLine(temp);
            int port = Int32.Parse(temp);

            my_server.port = port;
            string the_ip = connection_information.Substring(0, position);

            my_server.ip = the_ip;
            ConstantConnect(my_server); // constantly connect loop
            SendMessage(my_server);     // send message loop
            //Console.Read();
        }
예제 #3
0
파일: Program.cs 프로젝트: twoood/CS570
        // function to constantly try to connect the client, and will loop until client is connected
        private static void ConstantConnect(the_server server)
        {
            int connection_attempts = 0;

            while (!mysock.Connected)
            {
                try
                {
                    connection_attempts++;
                    mysock.Connect(IPAddress.Parse(server.ip), server.port); // hardcoded the loopback address, same port as server
                }
                catch (SocketException)                                      // if doesn't successfully connect, then print the number of connection attempts
                {
                    Console.Clear();
                    Console.WriteLine("Connection attempts: " + connection_attempts.ToString());
                }
            }
            Console.Clear();
            Console.WriteLine("Client successfully connected");
        }