Пример #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
        static void SendTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            lock (SendBufferQueue)
            {
                int bufferCount = SendBufferQueue.Count;
                if (0 != bufferCount)
                {
                    try
                    {
                        string dataStr = string.Empty;
                        for (int i = 0; i < bufferCount; i++)
                        {
                            dataStr = SendBufferQueue.Dequeue();

                            TcpSocketCommunicator reporter = new TcpSocketCommunicator();
                            reporter.Connect(RelayServerInfo.Host_name, RelayServerInfo.Port_num, 5000);
                            reporter.Send(Encoding.UTF8.GetBytes(dataStr));
                            reporter.Close();

                            //LogOutput.LogAppend("SendTimer_Elapsed Send : " + dataStr);
                            Thread.Sleep(10);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine(ex.ToString());
                        LogOutput.LogAppend(ex.ToString());
                    }
                }
            }

            SendTimer.Start();
        }
Пример #3
0
 static void SendTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
 {
     while (true)
     {
         lock (SendBufferQueue)
         {
             if (0 == SendBufferQueue.Count)
             {
                 break;
             }
             string dataStr = SendBufferQueue.Dequeue();
             SendClientData2DB(dataStr);
         }
     }
     SendTimer.Start();
 }
Пример #4
0
        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
                {
                }
            }
        }