예제 #1
0
        //receive client message and send to client
        public void ReceiveMsg(object client)
        {
            int connect   = 0;
            int connectid = id;

            id++;
            Socket    connection = (Socket)client;
            IPAddress clientIP   = (connection.RemoteEndPoint as IPEndPoint).Address;
            int       clientPort = (connection.RemoteEndPoint as IPEndPoint).Port;

            while (true)
            {
                try
                {
                    byte[] result = new byte[1024];
                    //receive message from client
                    int    receive_num = connection.Receive(result);
                    String receive_str = Encoding.ASCII.GetString(result, 0, receive_num);

                    if (receive_num > 0)
                    {
                        dynamic receive_json = JsonConvert.DeserializeObject(receive_str);
                        if (listen == 1)
                        {
                            if (connect == 1)
                            {
                                if (receive_json.Msg == "shut down")
                                {
                                    if (listen == 1)
                                    {
                                        listen = 0;
                                        UserData userData = new UserData("", "", "server is not ready", "2");
                                        string   jstr     = System.Text.Json.JsonSerializer.Serialize(userData);
                                        //resend message to client
                                        receive_msg.Dispatcher.BeginInvoke(
                                            new Action(() => { receive_msg.Text += "server stop\n"; }), null);
                                        for (int i = 0; i < SocketLists.Count; i++)
                                        {
                                            if (SocketLists[i] != null)
                                            {
                                                try
                                                {
                                                    SocketLists[i].Send(Encoding.ASCII.GetBytes(jstr + "\n"));
                                                }
                                                catch
                                                {
                                                    Console.WriteLine("bad");
                                                }
                                            }
                                        }
                                    }
                                    //connection.Send(Encoding.ASCII.GetBytes(jstr));
                                }
                                else if (receive_json.Connect == 1)
                                {
                                    String   send_str = "" + receive_json.Username + "(" + clientIP + ") : " + receive_str + "\n";
                                    UserData userData = new UserData("", "", "" + receive_json.Username + ": " + receive_json.Msg, "1");
                                    string   jstr     = System.Text.Json.JsonSerializer.Serialize(userData);
                                    //resend message to client
                                    for (int i = 0; i < SocketLists.Count; i++)
                                    {
                                        if (SocketLists[i] != null)
                                        {
                                            try
                                            {
                                                SocketLists[i].Send(Encoding.ASCII.GetBytes(jstr + "\n"));
                                            }
                                            catch
                                            {
                                                Console.WriteLine("bad");
                                            }
                                        }
                                    }
                                    //connection.Send(Encoding.ASCII.GetBytes(jstr));


                                    //receive_msg.Dispatcher.BeginInvoke(
                                    //  new Action(() => { receive_msg.Text += send_str; }), null);
                                    receive_msg.Dispatcher.BeginInvoke(
                                        new Action(() => { receive_msg.Text += "" + receive_json.Username + "(" + clientIP + ":" + clientPort + ")= " + receive_str + "\n"; }), null);
                                }
                            }
                            else
                            {
                                UserData userData = new UserData("", "" + receive_json.Username, "Welcome " + receive_json.Username + " !", "1");
                                string   jstr     = System.Text.Json.JsonSerializer.Serialize(userData);
                                //resend message to client
                                for (int i = 0; i < SocketLists.Count; i++)
                                {
                                    if (SocketLists[i] != null)
                                    {
                                        try
                                        {
                                            SocketLists[i].Send(Encoding.ASCII.GetBytes(jstr + "\n"));
                                        }
                                        catch
                                        {
                                            Console.WriteLine("bad");
                                        }
                                    }
                                }
                                //connection.Send(Encoding.ASCII.GetBytes(jstr+"\n"));
                                receive_msg.Dispatcher.BeginInvoke(
                                    new Action(() => { receive_msg.Text += "" + receive_json.Username + "(" + clientIP + ":" + clientPort + ")  connect\n"; }), null);
                                //send welcome message to client
                                connect = 1;
                            }
                        }
                        else
                        {
                            UserData userData = new UserData("", "", "server is not ready", "2");
                            string   jstr     = System.Text.Json.JsonSerializer.Serialize(userData);
                            //resend message to client
                            connection.Send(Encoding.ASCII.GetBytes(jstr + "\n"));
                            //send welcome message to client
                        }
                    }
                }
                catch (Exception e)
                {
                    //exception close()
                    Console.WriteLine(e);
                    //connection.Shutdown(SocketShutdown.Both);
                    //connection.Close();
                    break;
                }
            }
        }
예제 #2
0
 public static void AddUserData(UserData Value)
 {
     DB.Insert(Value);
     DB.Commit();
 }