コード例 #1
0
        private void Socket_Server_Training_Session_Management(object arg)
        {
            Socket Socket_Client_Training = arg as Socket;
            Session_Packet_Server_Training Session_Packet = new Session_Packet_Server_Training();

            try
            {
                //while (true)
                //{
                int size_header = Socket_Client_Training.Receive(Session_Packet.header, packet_header_length_training, SocketFlags.None);
                Session_Packet.Packet_Decode();
                if (Session_Packet.IsFormatWell)
                {
                    if (Session_Packet.packet_body_info_length > 0)
                    {     // accept self defined string
                        int size_body_info = Socket_Client_Training.Receive(Session_Packet.body_info, Session_Packet.packet_body_info_length, SocketFlags.None);
                    }
                    if (Session_Packet.packet_body_binary_length > 0)
                    {     // accept self defined binary
                        int size_body_binary = Socket_Client_Training.Receive(Session_Packet.body_binary, Session_Packet.packet_body_binary_length, SocketFlags.None);
                    }
                    byte[] decode_binary_md5 = Get_Binary_MD5(Session_Packet.body_binary);
                    //ref-http://stackoverflow.com/questions/43289/comparing-two-byte-arrays-in-net
                    bool IsDecodeWell = decode_binary_md5.SequenceEqual(Session_Packet.binary_md5);
                    Session_Packet.Create_Packet_Statue(IsDecodeWell);
                    if (IsDecodeWell)
                    {
                        Monitor.Enter(client_info);
                        client_info.Add(new Client_Info()
                        {
                            info              = Encoding.UTF8.GetString(Session_Packet.body_info),
                            client_id         = Session_Packet.client_id.ToString(),
                            remote_endpoint   = Socket_Client_Training.RemoteEndPoint.ToString(),
                            datetime_transmit = Session_Packet.datetime_transmit.ToString("yyyy-MM-dd HH:mm:ss"),
                        });
                        Monitor.Exit(client_info);
                        //Write_Session_Log_Server(Session_Packet, Socket_Client_Training);
                        //Write_Session_Binary_Server(Session_Packet, Socket_Client_Training);
                        Socket_Client_Training.Send(new byte[] { 0x00, 0x08, 0x08, 0x06 });
                    }
                    else
                    {
                        Socket_Client_Training.Send(new byte[] { 0x00, 0x08, 0x00, 0x00 });
                    }
                }
                //}
                Socket_Client_Training.Close();
            }
            catch (Exception ex)
            {
                Write_Exception_Log_Server("Socket_Server_Training_Session_Management " + ex.ToString());
                Thread.Sleep(5 * 1000);
                Socket_Server_Training_Session_Management(arg);
            }
            finally
            {
            }
        }
コード例 #2
0
 private void Write_Session_Log_Server(Session_Packet_Server_Training Session_Packet, Socket Socket_Client_Training)//Server收到正確後寫Session Info檔案
 {
     try
     {
         lock (_sesion_log_server_Lock)
         {
             if (!Directory.Exists(GetCurrentFilePath(@"Server\Session Log")))
             {
                 DirectoryInfo di = Directory.CreateDirectory(GetCurrentFilePath(@"Server\Session Log"));
             }
             string file = GetCurrentFilePath(@"Server\Session Log\Session_Log_" + DateTime.Now.ToString("yyyyMM") + ".csv");
             string first_line_in_file = "Record Time, Transmit Time, Client_id, RemoteEndPoint, Info, ";
             if (!File.Exists(file))
             {
                 using (StreamWriter sw = File.CreateText(file))
                 {
                     sw.WriteLine(first_line_in_file);
                     string info = "";
                     if (Session_Packet.packet_body_info_length != 0)
                     {
                         info = Encoding.UTF8.GetString(Session_Packet.body_info);
                     }
                     sw.WriteLine("@" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ", " +
                                  "@" + Session_Packet.datetime_transmit.ToString("yyyy-MM-dd HH:mm:ss") + ", " +
                                  Session_Packet.client_id.ToString() + ", " +
                                  Socket_Client_Training.RemoteEndPoint + ", " +
                                  info
                                  );
                 }
             }
             else if (File.Exists(file))
             {
                 using (StreamWriter sw = File.AppendText(file))
                 {
                     string info = "";
                     if (Session_Packet.packet_body_info_length != 0)
                     {
                         info = Encoding.UTF8.GetString(Session_Packet.body_info);
                     }
                     sw.WriteLine("@" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ", " +
                                  "@" + Session_Packet.datetime_transmit.ToString("yyyy-MM-dd HH:mm:ss") + ", " +
                                  Session_Packet.client_id.ToString() + ", " +
                                  Socket_Client_Training.RemoteEndPoint + ", " +
                                  info
                                  );
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Write_Exception_Log_Server("Write_Session_Log_Server " + ex.ToString());
     }
     finally
     {
     }
 }
コード例 #3
0
 private void Write_Session_Binary_Server(Session_Packet_Server_Training Session_Packet, Socket Socket_Client_Training)//Server收到正確後寫Session Binary檔案
 {
     try
     {
         lock (_sesion_binary_server_Lock)
         {
             if (!Directory.Exists(GetCurrentFilePath(@"Server\Session Binary")))
             {
                 DirectoryInfo di = Directory.CreateDirectory(GetCurrentFilePath(@"Server\Session Binary"));
             }
             string file = GetCurrentFilePath(@"Server\Session Binary\Client" + Session_Packet.client_id + @"_Binary_" + Session_Packet.datetime_transmit.ToString("yyyyMMddhhmmss") + ".bin");
             File.WriteAllBytes(file, Session_Packet.body_binary);
         }
     }
     catch (Exception ex)
     {
         Write_Exception_Log_Server("Write_Session_Binary_Server " + ex.ToString());
     }
     finally
     {
     }
 }