Esempio n. 1
0
        public override void OnReceiveData(ConnectionState state)
        {
            byte[] buffer = new byte[1024];
            while (state.AvailableData > 0)
            {
                int readBytes = state.Read(buffer, 0, 1024);
                if (readBytes > 0)
                {
                    _receivedStr += Encoding.UTF8.GetString(buffer, 0, readBytes);

                    if (_receivedStr.IndexOf("<EOF>") >= 0)
                    {
                        int headerStart    = _receivedStr.IndexOf("<FILENAME>");
                        int filenameStart  = headerStart + 10;
                        int filenameEnd    = _receivedStr.IndexOf("</FILENAME>");
                        int headerEnd      = filenameEnd + 11;
                        int filenameLength = filenameEnd - filenameStart;

                        _filename    = _receivedStr.Substring(filenameStart, filenameLength);
                        _receivedStr = _receivedStr.Remove(0, headerEnd);


                        int eofStart = _receivedStr.IndexOf("<EOF>");
                        _receivedStr = _receivedStr.Remove(eofStart, 4);
                        saveFile();
                    }
                }
                else
                {
                    state.EndConnection();  //If read fails then close connection
                }
            }
        }
Esempio n. 2
0
 public override void OnAcceptConnection(ConnectionState state)
 {
     _receivedStr = "";
     if (!state.Write(Encoding.UTF8.GetBytes("Hello World!\r\n"), 0, 14))
     {
         state.EndConnection(); //if write fails... then close connection
     }
 }
Esempio n. 3
0
        public void Stop()
        {
            lock (this)
            {
                server.ClientConnected    -= Server_ClientConnected;
                server.ClientDisconnected -= Server_ClientDisconnected;
                server.DataReceived       -= Server_DataReceived;

                connectionState.EndConnection();
                connectionState = null;
                server.Stop();
                server = null;

                LogExt.log.Debug($"SimpleTCP, Stop.");
            }
        }
        public override void OnReceiveData(ConnectionState state)
        {
            var funcName = "OnReceiveData(ConnectionState state)";
            StLog.DebugWriteLine ("Daten empfangen", funcName);
            StLog.DebugWriteLine ("Anzahl der Verbindungen: " + state._server.CurrentConnections, funcName);
            byte[] buffer = new byte[1024];
            while(state.AvailableData > 0)
            {
                int readBytes = state.Read(buffer, 0, 1024);
                if(readBytes > 0)
                {
                    StLog.DebugWriteLine ("Daten gelesen", funcName);
                    _receivedStr += enc_cp437.GetString (buffer, 0, readBytes);

                    OnNewMessage (_receivedStr);
                    _receivedStr = "";

                }
                //If read fails then close connection
            }
            state.EndConnection();
            StLog.DebugWriteLine ("Beende Verbindung. Anzahl der Verbindungen: " + state._server.CurrentConnections, funcName);
        }