/// <summary>
        /// Sends a reply to client
        /// </summary>
        /// <param name="client"></param>
        private static void SendReply(ConnectedObject client)
        {
            if (client == null)
            {
                Console.WriteLine("Unable to send reply: client null");
                return;
            }

            Console.Write("Sending Reply: ");

            // Create reply
            client.CreateOutgoingMessage("Message Received");
            var byteReply = client.OutgoingMessageToBytes();

            // Listen for more incoming messages
            try
            {
                client.Socket.BeginSend(byteReply, 0, byteReply.Length, SocketFlags.None, new AsyncCallback(SendReplyCallback), client);
            }
            catch (SocketException)
            {
                // Client was forcebly closed on the client side
                CloseClient(client);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Example #2
0
        /// <summary>
        /// Sends a message to the server
        /// </summary>
        /// <param name="client"></param>
        private static void Send(ConnectedObject client, string userinfo)
        {
            // Build message

            string msg = userinfo;

            client.CreateOutgoingMessage(msg);
            byte[] data = client.OutgoingMessageToBytes();

            // Send it on a 1 second interval

            try
            {
                client.Socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
            }
            catch (SocketException)
            {
                Console.WriteLine("Server Closed");
                client.Close();
                Thread.CurrentThread.Abort();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Thread.CurrentThread.Abort();
            }
        }
        /// <summary>
        /// Sends a message to the server
        /// </summary>
        /// <param name="client"></param>
        private static void Send(ConnectedObject client)
        {
            // Build message
            client.CreateOutgoingMessage($"Message from {Console.Title}");
            byte[] data = client.OutgoingMessageToBytes();

            // Send it on a 1 second interval
            while (true)
            {
                Thread.Sleep(3000);
                try
                {
                    client.Socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
                }
                catch (SocketException)
                {
                    Console.WriteLine("Server Closed");
                    client.Close();
                    Thread.CurrentThread.Abort();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Thread.CurrentThread.Abort();
                }
            }
        }
Example #4
0
        /// <summary>
        /// Sends a message to the server
        /// </summary>
        /// <param name="client"></param>
        public void Send(String MsgSend)
        {
            // Build message
            client.CreateOutgoingMessage(MsgSend);
            byte[] data = client.OutgoingMessageToBytes();

            // Send it on a 1 second interval


            if (client.Socket.Connected)
            {
                try
                {
                    client.Socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), client);
                }
                catch (SocketException)
                {
                    Console.WriteLine("Server Closed");
                    client.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("ket thuc send roi day");
                // Connect();
                Thread.Sleep(3000);
            }
        }