Esempio n. 1
0
 public void Save(XmlTextWriter xml)
 {
     xml.WriteStartElement("sender");
     xml.WriteAttributeString("serverIP", ServerIP.ToString());
     xml.WriteAttributeString("serverPort", ServerPort.ToString());
     xml.WriteEndElement();
 }
Esempio n. 2
0
        /// <summary>
        /// Start connect to the server.
        /// </summary>
        public void StartConnectToServer()
        {
            if (ServerIP == null)
            {
                log.Warn("Client has no configured IP adress");
                log.Warn("Client cannot connect to the server. Please open the clientSocket configuration and enter the clientSocket IP");
            }

            try
            {
                // Creates one SocketPermission object for access restrictions
                SocketPermission permission = new SocketPermission(
                    NetworkAccess.Connect,    // Allowed to accept connections
                    TransportType.Tcp,        // Defines transport types
                    ServerIP.ToString(),      // The IP addresses of local host
                    SocketPermission.AllPorts // Specifies all ports
                    );

                // Ensures the code to have permission to access a Socket
                permission.Demand();

                // Remote endpoint is the server
                IPEndPoint remoteEP = new IPEndPoint(ServerIP, ServerPort);

                // Create a TCP/IP socket
                // Using IPv4 as the network protocol
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Begin to connect to the server.
                _clientSocket.BeginConnect(remoteEP, new AsyncCallback(this.ConnectCallback), _clientSocket);

                // Wait until the connection is done
                connectDone.WaitOne();

                // Start method which checks periodically if the Socket is still connected
                new Thread(() => SocketConnected(_clientSocket))
                {
                    IsBackground = true
                }.Start();

                this.IsSocketConnected = true;

                // Begin to wait for logs request signal from the server.
                new SychronousClientFunctions(_clientSocket).WaitForSignal();
            }
            catch (Exception ex)
            {
                log.Info(ex.Message, ex);
            }
        }
Esempio n. 3
0
        public void Save(string fileName)
        {
            using (XmlTextWriter xml = new XmlTextWriter(fileName, Encoding.UTF8))
            {
                xml.Formatting = System.Xml.Formatting.Indented;
                xml.WriteStartDocument(true);
                xml.WriteStartElement("configuration");
                xml.WriteStartElement("settings");

                xml.WriteStartElement("server");
                xml.WriteAttributeString("ip", ServerIP.ToString());
                xml.WriteAttributeString("port", ServerPort.ToString());
                xml.WriteEndElement();

                xml.WriteEndElement();
                xml.WriteEndElement();
                xml.WriteEndDocument();
            }
        }
        public void buildConnectionString()
        {
            // Folder with CSV
            if (DriverSel.SelectedItem.ToString() == "ODBC File (*.csv)")
            {
                Driver   = "{Microsoft Text Driver (*.txt; *.csv)}";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + FilePath.ToString() + ";Extensions=asc,csv,tab,txt;";
            }
            //Excel 97/2000 File
            if (DriverSel.SelectedItem.ToString() == "ODBC Excel (*.xls)")
            {
                Driver   = "Driver={Microsoft Excel Driver (*.xls)};";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + FilePath.ToString() + ";";
            }
            //MySQl Server
            if (DriverSel.SelectedItem.ToString() == "ODBC MySQL (IP)")
            {
                Driver   = "{MySQL ODBC 5.1 Driver}";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Driver={MySQL ODBC 5.1 Driver};Server=" + ServerIP.ToString() + ";Database=" + Database.ToString() + ";uid=" + Username.ToString() + ";pwd=" + Password.ToString() + ";";
            }
            //SQLite File
            if (DriverSel.SelectedItem.ToString() == "ODBC SQLite (*.db)")
            {
                Driver   = "Driver=SQLite3 ODBC Driver;";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "DRIVER=SQLite3 ODBC Driver;Database=" + FilePath.ToString() + ";LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;";
            }
            //PostgreSQL
            if (DriverSel.SelectedItem.ToString() == "ODBC PostgreSQL (IP)")
            {
                Driver   = "Driver{PostgreSQL}";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Driver={PostgreSQL};Server=" + ServerIP.ToString() + ";Port=5432;Database=" + Database.ToString() + ";Uid=" + Username.ToString() + ";Pwd=" + Password.ToString() + ";";
            }

            //MySQL Server
            if (DriverSel.SelectedItem.ToString() == "ADO MySQL (IP)")
            {
                Driver   = "{MySQL ADO Driver}";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "SERVER=" + ServerIP.ToString() + ";DATABASE=" + Database.ToString() + ";UID=" + Username.ToString() + ";PASSWORD="******";PORT=3306;";
            }
            //Microsoft (Transakt) SQL Server (97/2000)
            if (DriverSel.SelectedItem.ToString() == "ADO Microsoft SQL (IP)")
            {
                Driver   = "{SQL Server}";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Driver={SQL Server};Server=" + ServerIP.ToString() + ";Database=" + Database.ToString() + ";Uid=" + Username.ToString() + ";Pwd=" + Password.ToString() + ";";
            }
            // Provider=MSDASQL;Driver=MySQL ODBC 5.1 Driver;Server=127.0.0.1;Database=datebank_name;Uid=mein_db_user;Pwd=pa_pa_passwort
            if (DriverSel.SelectedItem.ToString() == "ADODB MSDASQL/MYSQL (IP)")
            {
                Driver   = "Provider=MSDASQL;Driver=MySQL ODBC 5.1 Driver";
                ServerIP = ServerTxt.Text.ToString();
                FilePath = ServerTxt.Text.ToString();
                Database = DatabaseTxt.Text.ToString();
                Username = UsernameTxt.Text.ToString();
                Password = PasswordTxt.Text.ToString();
                conStr   = "Provider=MSDASQL;Driver=MySQL ODBC 5.1 Driver;Server=" + ServerIP.ToString() + ";Database=" + Database.ToString() + ";Uid=" + Username.ToString() + ";Pwd=" + Password.ToString() + ";";
            }
            conStrBox.Text = conStr.ToString();
        }
Esempio n. 5
0
        /// <summary>
        /// Server start to listen the client connection.
        /// </summary>
        public void StartServerListeningClientConnections()
        {
            try
            {
                // WE have to start the server delayed
                Thread.Sleep(2000);

                // Creates one SocketPermission object for access restrictions
                SocketPermission permission = new SocketPermission(
                    NetworkAccess.Accept,     // Allowed to accept connections
                    TransportType.Tcp,        // Defines transport types
                    ServerIP.ToString(),      // The IP addresses of local host
                    SocketPermission.AllPorts // Specifies all ports
                    );

                // Ensures the code to have permission to access a Socket
                permission.Demand();

                // Establish the server endpoint for the socket.
                IPEndPoint serverEndPoint = new IPEndPoint(ServerIP, ServerPort);

                // Create a TCP/IP socket
                // Using IPv4 as the network protocol
                _listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Bind the socket to the server endpoint and listen for incoming connections.
                _listener.Bind(serverEndPoint);
                _listener.Listen(AsynchronousServer.SOCKET_LISTENER_BACKLOG);

                log.Info("Log server is ready for client connections.");
                log.Info("");

                //Start method which checks periodically if the server is running
                new Thread(() => CheckServer())
                {
                    IsBackground = true
                }.Start();

                IsServerRunning = true;

                // Loop server listening to client connections.
                while (IsServerRunning)
                {
                    // Start an asynchronous socket to listen for connections.
                    _listener.BeginAccept(new AsyncCallback(this.AcceptCallback), _listener);

                    // Wait until a connection is made before continuing.
                    connectDone.WaitOne();
                }
            }
            catch (SocketException ex)
            {
                log.Error(ex.Message, ex);
                log.Warn("Log Server listener closed. Try to restart Server");

                // Socket is not listening --> Restart Server
                this.RestartServer();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
            }
        }
Esempio n. 6
0
        public void ConnectToServer()
        {
            ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            int tryCount   = 20;
            int tryCounter = 0;

            while (tryCounter < tryCount)
            {
                try
                {
                    IPEndPoint ep = new IPEndPoint(ServerIP, Port);
                    ClientSocket.Connect(ep);
                    return;
                }
                catch (SocketException e)
                {
                    tryCounter++;
                    Thread.Sleep(10000);

                    if (tryCounter == tryCount)
                    {
                        throw new Exception(string.Format(@"Failed to connect to {0}:{1}, {2}", ServerIP.ToString(), Port, e));
                    }
                }
            }
        }
        /// <summary>
        /// 向服务器发送ServerListPing相关的包并解析返回的json
        /// </summary>
        /// <exception cref="ArgumentNullException"/>
        /// <exception cref="InvalidPacketException"/>
        /// <exception cref="SocketException"/>
        /// <exception cref="PacketException"/>
        /// <exception cref="JsonException"/>
        public PingReply Send(Socket socket, bool reuseSocket)
        {
            if (socket == null)
            {
                throw new ArgumentNullException(nameof(socket));
            }

            PingReply PingResult; string JsonResult;

            if (EnableDnsRoundRobin && IPAddressList != null && IPAddressList.Length > 1)
            {
                DnsRoundRobinHandler();
            }
            if (!socket.Connected)
            {
                socket.Connect(ServerIP, this.ServerPort);
            }
            if (ReceiveTimeout != default)
            {
                socket.ReceiveTimeout = ReceiveTimeout;
            }

            //Send Ping Packet
            Packet Handshake = new HandshakePacket(string.IsNullOrWhiteSpace(Host) ? ServerIP.ToString() : Host, this.ServerPort, -1, HandshakePacket.State.GetStatus);

            socket.Send(Handshake.ToBytes());
            Packet PingRequest = new PingRequestPacket();

            socket.Send(PingRequest.ToBytes());

            //Receive Packet
            int PacketLength = ProtocolHandler.GetPacketLength(socket);

            if (PacketLength > 0)
            {
                List <byte> Packet   = new List <byte>(ProtocolHandler.ReceiveData(0, PacketLength, socket));
                int         PacketID = ProtocolHandler.ReadVarInt(Packet);
                if (PacketID != PingResponsePacket.GetPacketID())
                {
                    throw new InvalidPacketException("Invalid ping response packet id ", new Packet(PacketID, Packet));
                }

                JsonResult = ProtocolHandler.ReadString(Packet);
                if (!string.IsNullOrWhiteSpace(JsonResult))
                {
                    PingResult         = ResolveJson(JsonResult);
                    PingResult.Elapsed = EnableDelayDetect ? GetTime(socket) : null;
                    PingResult.Json    = JsonResult;
                }
                else
                {
                    PingResult = null;
                }
                socket.Shutdown(SocketShutdown.Both);
                socket.Disconnect(reuseSocket);
            }
            else
            {
                throw new PacketException($"Response Packet Length too Small (PacketLength:{PacketLength})");
            }

            return(PingResult);
        }