예제 #1
0
 /// <summary>
 /// 写入数据库
 /// </summary>
 /// <param name="cmdStr"></param>
 /// <returns></returns>
 protected static bool WriteToDB(string cmdStr)
 {
     try
     {
         if (E_DB_CONNECT_MODE.DIRECT == Db_connect_mode)
         {
             // 直接写入数据库
             DBConnectMySQL mysql_object = new DBConnectMySQL(DbServerInfo);
             mysql_object.ExecuteMySqlCommand(cmdStr);
         }
         else if (E_DB_CONNECT_MODE.RELAY == Db_connect_mode)
         {
             // 通过中继服务器
             lock (SendBufferQueue)
             {
                 SendBufferQueue.Enqueue(cmdStr);
                 //LogOutput.LogAppend("WriteToDB  SendBufferQueue.Enqueue : " + cmdStr);
             }
         }
     }
     catch (Exception ex)
     {
         System.Diagnostics.Trace.WriteLine(ex.ToString());
         LogOutput.LogAppend(ex.ToString());
         return(false);
     }
     return(true);
 }
예제 #2
0
파일: Program.cs 프로젝트: gangjian/work
        static void Main(string[] args)
        {
            // 读设定文件
            LoadIniFile();

            SendTimer           = new System.Timers.Timer(1000);
            SendTimer.AutoReset = false;
            SendTimer.Elapsed  += SendTimer_Elapsed;
            SendTimer.Start();

            IPEndPoint ipep    = new IPEndPoint(IPAddress.Any, Port);                                   // 本机预使用的IP和端口
            Socket     sSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            sSocket.Bind(ipep);                                                                                                         // 绑定
            sSocket.Listen(100);                                                                                                        // 监听

            // 第一层的主循环, 循环接收来自各个客户端的数据
            while (true)
            {
                try
                {
                    Console.WriteLine("ServiceArea Relay Server(2015_11_15)");
                    Console.WriteLine("Waiting for a client...");
                    Socket     cSocket  = sSocket.Accept();                                                                     // 当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
                    IPEndPoint clientip = (IPEndPoint)cSocket.RemoteEndPoint;
                    Console.WriteLine("Connect with client:" + clientip.Address + " at port:" + clientip.Port);

                    //LogOutput.LogAppend("");

                    // 第二层循环, 接收某个客户端连接的全部数据
                    while (true)
                    {
                        string recvStr   = string.Empty;
                        byte[] recvBytes = new byte[1024];
                        int    bytes;
                        bytes = cSocket.Receive(recvBytes, recvBytes.Length, 0);                        // 从客户端接受消息
                        if (bytes > 0)
                        {
                            recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                            Console.WriteLine("Server get message:{0}", recvStr);                            // 把客户端传来的信息显示出来
                            // 加入到发送队列里
                            lock (SendBufferQueue)
                            {
                                SendBufferQueue.Enqueue(recvStr);
                                //LogOutput.LogAppend("Enqueue : " + recvStr);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    cSocket.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                finally
                {
                }
            }
        }