Exemplo n.º 1
0
        //static void SetPortForward(int port)
        //{
        //    try
        //    {
        //        var discoverer = new NatDiscoverer();
        //        var cts = new CancellationTokenSource(10000);
        //        var device = discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts).Result;


        //        device.CreatePortMapAsync(new Mapping(Protocol.Udp, 5001, port, "Portunus")).Wait();
        //    }
        //    catch
        //    {
        //        Console.Write(' ');
        //    }
        //}

        static async void StartTCPClient()
        {
            TCPSocket socket = new TCPSocket();


            new Thread(() =>
            {
                socket.StartListening(config.Client.Port);
            });

            socket.Send(config.Servers[0].Address, config.Servers[0].Port, "Hello Server 1, this is the client");


            while (true)
            {
                try
                {
                    socket.Send(config.Client.Address, config.Client.Port, "Hello Client, this is the host");
                    Thread.Sleep(500);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Exemplo n.º 2
0
        public void TestLongSMTPCommand()
        {
            TCPSocket socket = new TCPSocket();

            socket.Connect(25);

            // Build a large string...
            StringBuilder sb = new StringBuilder();

            sb.Append("A01");
            for (int i = 0; i < 1000000; i++)
            {
                sb.Append("01234567890");
            }
            int length = sb.Length;

            sb.Append(Environment.NewLine);

            for (int i = 0; i < 100; i++)
            {
                try
                {
                    socket.Send(sb.ToString());
                }
                catch (Exception)
                {
                    return;
                }
            }

            socket.Send("\r\n");
            string result = socket.Receive();

            socket.Disconnect();
        }
Exemplo n.º 3
0
        public void TestLongLineInPOP3Conversation()
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 400; i++)
            {
                sb.Append("1234567890");
            }

            sb.Append(".com");

            string command = "NOOP " + sb.ToString();

            TCPSocket socket = new TCPSocket();

            Assert.IsTrue(socket.Connect(110));
            socket.Receive();
            socket.Send(command + "\r\n");

            string response = socket.Receive();

            Assert.IsFalse(response.StartsWith("+OK"));

            socket.Disconnect();
        }
Exemplo n.º 4
0
        public void TestLongLineInIMAPConversation()
        {
            // hMailServer is kind and allows up to 1000000 octets.
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < 100000; i++)
            {
                sb.Append("1234567890");
            }

            sb.Append(".com\r\n");

            string command = "A03 NOOP " + sb.ToString();

            TCPSocket socket = new TCPSocket();

            Assert.IsTrue(socket.Connect(143));
            socket.Receive();
            socket.Send(command);

            try
            {
                string response = socket.Receive();
                Assert.IsTrue(response.StartsWith("* BYE"));
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                Assert.IsTrue(ex.ErrorCode == 10054);
            }


            socket.Disconnect();
        }
Exemplo n.º 5
0
        public void TestLongLineInIMAPConversation()
        {
            // hMailServer is kind and allows up to 1000000 octets.
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 100000; i++)
            {
                sb.Append("1234567890");
            }

            sb.Append(".com\r\n");

            string command = "A03 NOOP " + sb.ToString();

            TCPSocket socket = new TCPSocket();
            Assert.IsTrue(socket.Connect(143));
            socket.Receive();
            socket.Send(command);

            try
            {
                string response = socket.Receive();
                Assert.IsTrue(response.StartsWith("* BYE"));
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                Assert.IsTrue(ex.ErrorCode == 10054);
            }

            socket.Disconnect();
        }
Exemplo n.º 6
0
        static public void Send(byte[] data)
        {
            byte[] packet = new byte[data.Length + 4];

            Array.Copy(data, 0, packet, 4, data.Length);
            Array.Copy(BitConverter.GetBytes(data.Length), packet, 4);

            proxyConnection.Send(packet);
        }
Exemplo n.º 7
0
        private void Send(byte[] data, TCPSocket to)
        {
            byte[] packet = new byte[data.Length + 4];

            Array.Copy(data, 0, packet, 4, data.Length);
            Array.Copy(BitConverter.GetBytes(data.Length), packet, 4);

            to.Send(packet);
        }
        public void TestSMTPServerAuthPlain()
        {
            _settings.AllowSMTPAuthPlain = true;

            TCPSocket sock = new TCPSocket();

            sock.Connect(25);
            Assert.IsTrue(sock.Receive().StartsWith("220"));
            sock.Send("EHLO test.com\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("250"));
            sock.Send("AUTH PLAIN\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("334"));

            string str = "\t" + GetUsername() + "\t" + GetPassword();

            sock.Send(EncodeBase64(str) + "\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("535"));
            EnsureNoPassword();
        }
Exemplo n.º 9
0
        public void TestLongSMTPDataSessionIncludingNewline()
        {
            long memoryUsage = Shared.GetCurrentMemoryUsage();

            _application.Settings.MaxMessageSize = 0;

            TCPSocket socket = new TCPSocket();

            socket.Connect(25);
            socket.Receive();

            socket.Send("HELO test.com\r\n");
            socket.Receive();

            // Build a large string...
            StringBuilder sb = new StringBuilder();

            sb.Append("A01");
            for (int i = 0; i < 10000; i++)
            {
                sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890\r\n");
            }
            int length = sb.Length;

            sb.Append(Environment.NewLine);

            socket.Send("MAIL FROM: [email protected]\r\n");
            socket.Receive();
            socket.Send("RCPT TO: [email protected]\r\n");
            socket.Receive();
            socket.Send("DATA\r\n");
            socket.Receive();

            for (int i = 0; i < 100; i++)
            {
                Shared.AssertLowMemoryUsage(memoryUsage + 30);

                try
                {
                    socket.Send(sb.ToString());
                }
                catch (Exception)
                {
                    return;
                }
            }

            socket.Send("\r\n.\r\n");
            string result = socket.Receive();

            Assert.IsTrue(result.StartsWith("250"));

            socket.Send("QUIT\r\n");
            result = socket.Receive();
            socket.Disconnect();

            POP3Simulator.AssertMessageCount(_domain.Accounts[0].Address, "test", 1);
        }
Exemplo n.º 10
0
        public void TestSendToTooManyRecipients()
        {
            hMailServer.Accounts accounts      = _domain.Accounts;
            hMailServer.Account  senderAccount =
                SingletonProvider <Utilities> .Instance.AddAccount(accounts, string.Format("*****@*****.**"), "test");

            List <string> recipients = new List <string>();


            TCPSocket sock = new TCPSocket();

            sock.Connect(25);
            string result = sock.ReadUntil("\r\n");

            sock.Send("EHLO test.com\r\n");
            result = sock.ReadUntil("\r\n");
            Assert.IsTrue(result.StartsWith("250"));
            sock.Send("MAIL FROM: [email protected]\r\n");
            result = sock.ReadUntil("\r\n");
            Assert.IsTrue(result.StartsWith("250"));

            Stopwatch watch = new Stopwatch();

            watch.Start();

            for (int i = 0; i < 51000; i++)
            {
                string address = string.Format("test{0}@gmail.com", i);

                sock.Send(string.Format("RCPT TO: <{0}>\r\n", address));

                result = sock.ReadUntil("\r\n");
                if (i < 50000)
                {
                    Assert.IsTrue(result.StartsWith("250"));
                }
                else
                {
                    long perfor = watch.ElapsedMilliseconds;
                    Assert.IsFalse(result.StartsWith("250"));
                }
            }
        }
Exemplo n.º 11
0
        private void Send(byte[] data)
        {
            // The same error as in Proxy/Connection.cs -.-'
            byte[] packet = new byte[data.Length + 4];

            Array.Copy(data, 0, packet, 4, data.Length);
            Array.Copy(BitConverter.GetBytes(data.Length), packet, 4);

            socket.Send(packet);
        }
Exemplo n.º 12
0
        public void TestPOP3DownloadOfLargeMessage()
        {
            TestLongSMTPDataSessionIncludingNewline();

            POP3Simulator.AssertMessageCount("*****@*****.**", "test", 1);

            TCPSocket socket = new TCPSocket();

            socket.Connect(110);

            // Receive welcome message.
            string sData = socket.Receive();

            socket.Send("USER " + "*****@*****.**" + "\r\n");
            sData = socket.ReadUntil("+OK");

            socket.Send("PASS " + "test" + "\r\n");
            sData = socket.ReadUntil("+OK");

            socket.Send("RETR 1\r\n");

            string sRetVal = "";

            while (sRetVal.IndexOf("\r\n.\r\n") < 0)
            {
                if (sRetVal.IndexOf("-ERR no such message") >= 0)
                {
                    socket.Disconnect();
                    Assert.Fail("Nope");
                }

                sRetVal = socket.Receive();
            }

            socket.Send("DELE 1\r\n");
            sData = socket.ReadUntil("+OK");

            socket.Send("QUIT\r\n");
            sData = socket.ReadUntil("+OK");

            socket.Disconnect();
        }
Exemplo n.º 13
0
        public void TestSMTPServerAuthLoginUsernameAsThirdParameter()
        {
            _settings.AllowSMTPAuthPlain = true;

            TCPSocket sock = new TCPSocket();

            sock.Connect(25);
            Assert.IsTrue(sock.Receive().StartsWith("220"));
            sock.Send("EHLO test.com\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("250"));

            string base64EncodedUsername = EncodeBase64(GetUsername());

            sock.Send("AUTH LOGIN " + base64EncodedUsername + "\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("334"));

            sock.Send(EncodeBase64(GetPassword()) + "\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("535"));
            EnsureNoPassword();
        }
Exemplo n.º 14
0
        private void Send(byte[] data)
        {
            // The biggest stupid error I've ever seen, I havent added the size indicator to the packet data
            // This was causing the client to not answer...
            // Fixed now
            byte[] packet = new byte[data.Length + 4];

            Array.Copy(data, 0, packet, 4, data.Length);
            Array.Copy(BitConverter.GetBytes(data.Length), packet, 4);

            socket.Send(packet);
        }
Exemplo n.º 15
0
        public void TestLongSMTPDataSessionWithoutNewline()
        {
            long memoryUsage = Shared.GetCurrentMemoryUsage();

            TCPSocket socket = new TCPSocket();

            socket.Connect(25);
            socket.Receive();

            socket.Send("HELO test.com\r\n");
            socket.Receive();

            // Build a large string...
            StringBuilder sb = new StringBuilder();

            sb.Append("A01");
            for (int i = 0; i < 10000; i++)
            {
                sb.Append("01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890");
            }
            int length = sb.Length;

            socket.Send("MAIL FROM: [email protected]\r\n");
            socket.Receive();
            socket.Send("RCPT TO: [email protected]\r\n");
            socket.Receive();
            socket.Send("DATA\r\n");
            socket.Receive();

            for (int i = 0; i < 1000; i++)
            {
                Shared.AssertLowMemoryUsage(memoryUsage + 30);

                try
                {
                    socket.Send(sb.ToString());
                }
                catch (Exception)
                {
                    return;
                }
            }

            socket.Send("\r\n.\r\n");
            string result = socket.Receive();

            Assert.IsTrue(result.StartsWith("554 Too long line was received. Transmission aborted."));

            socket.Send("QUIT\r\n");
            result = socket.Receive();


            socket.Disconnect();
        }
Exemplo n.º 16
0
        public void AuthShouldUpdateLastLogonTime()
        {
            hMailServer.Account account = SingletonProvider<Utilities>.Instance.AddAccount(_domain, "*****@*****.**", "test");
             DateTime lastLogonTimeBefore = Convert.ToDateTime(account.LastLogonTime);

             System.Threading.Thread.Sleep(1000);
             TCPSocket sock = new TCPSocket();
             sock.Connect(25);
             Assert.IsTrue(sock.Receive().StartsWith("220"));
             sock.Send("EHLO test.com\r\n");
             Assert.IsTrue(sock.Receive().StartsWith("250"));

             string base64EncodedUsername = EncodeBase64("*****@*****.**");
             sock.Send("AUTH LOGIN " + base64EncodedUsername + "\r\n");
             Assert.IsTrue(sock.Receive().StartsWith("334"));

             sock.Send(EncodeBase64("test") + "\r\n");
             Assert.IsTrue(sock.Receive().StartsWith("235"));

             DateTime lastLogonTimeAfter = Convert.ToDateTime(SingletonProvider<Utilities>.Instance.GetApp().Domains[0].Accounts[0].LastLogonTime);
             Assert.AreNotEqual(lastLogonTimeBefore, lastLogonTimeAfter);
        }
Exemplo n.º 17
0
 private void Update()
 {
     if (TCPSocket.IsConnect())
     {
         //发送数据
         if (0 == Time.frameCount % SendInterval && 0 < SendQueue.Count)
         {
             TCPSocket.Send(SendQueue.Dequeue());
         }
         if (0 == Time.frameCount % RecvInterval)
         {
             onReceive();
         }
     }
 }
Exemplo n.º 18
0
    public void Connect()
    {
        TCPSocket socket = new TCPSocket();

        socket.Connect(address.text, int.Parse(port.text));

        // Faire la gestion d'erreurs si connect renvoie false

        Byte[] message = socket.Recv();

        System.Text.Encoding enc = System.Text.Encoding.ASCII;

        Debug.Log(enc.GetString(message));

        socket.Send(enc.GetBytes("bite"));
    }
Exemplo n.º 19
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            tcpSocket.Connect("127.0.0.1", 18002);
        }



        if (Input.GetKeyDown(KeyCode.B))
        {
            byte[] tmpBytes = System.Text.Encoding.Default.GetBytes(" 1707 !!!");
            tcpSocket.Send(tmpBytes);
        }


        if (tcpSocket != null && tcpSocket.Connected())
        {
            tcpSocket.Recive();
        }
    }
Exemplo n.º 20
0
        public void TestLongLineInPOP3Conversation()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 400; i++)
            {
                sb.Append("1234567890");
            }

            sb.Append(".com");

            string command = "NOOP " + sb.ToString();

            TCPSocket socket = new TCPSocket();
            Assert.IsTrue(socket.Connect(110));
            socket.Receive();
            socket.Send(command + "\r\n");

            string response = socket.Receive();
            Assert.IsFalse(response.StartsWith("+OK"));

            socket.Disconnect();
        }
Exemplo n.º 21
0
        public void TestEHLOKeywords()
        {
            hMailServer.Application application = SingletonProvider<Utilities>.Instance.GetApp();
              hMailServer.Settings settings = _settings;
              settings.HostName = "examplify.com";

              TCPSocket socket = new TCPSocket();
              Assert.IsTrue(socket.Connect(25));
              string result = socket.Receive();
              socket.Send("EHLO example.com\r\n");
              result = socket.Receive();
              socket.Disconnect();
              Assert.IsTrue(result.Contains("250-" + settings.HostName));
        }
        public void TestSMTPServerAuthLoginUsernameAsThirdParameter()
        {
            _settings.AllowSMTPAuthPlain = true;

               TCPSocket sock = new TCPSocket();
               sock.Connect(25);
               Assert.IsTrue(sock.Receive().StartsWith("220"));
               sock.Send("EHLO test.com\r\n");
               Assert.IsTrue(sock.Receive().StartsWith("250"));

               string base64EncodedUsername = EncodeBase64(GetUsername());
               sock.Send("AUTH LOGIN " + base64EncodedUsername + "\r\n");
               Assert.IsTrue(sock.Receive().StartsWith("334"));

               sock.Send(EncodeBase64(GetPassword()) + "\r\n");
               Assert.IsTrue(sock.Receive().StartsWith("535"));
               EnsureNoPassword();
        }
        public void TestSMTPServerAuthPlain()
        {
            _settings.AllowSMTPAuthPlain = true;

            TCPSocket sock = new TCPSocket();
            sock.Connect(25);
            Assert.IsTrue(sock.Receive().StartsWith("220"));
            sock.Send("EHLO test.com\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("250"));
            sock.Send("AUTH PLAIN\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("334"));

            string str = "\t" + GetUsername() + "\t" + GetPassword();

            sock.Send(EncodeBase64(str) + "\r\n");
            Assert.IsTrue(sock.Receive().StartsWith("535"));
            EnsureNoPassword();
        }
Exemplo n.º 24
0
        private static byte[] SendMessageToSocketAndListenToCallback(IPEndPoint source, IPEndPoint destination, byte[] bufferToSend, BytesReceivedDelegate callBack, ProtocolType protocol, byte[] heartbeatMessage, Regex responseFormat, BytesReceivedDelegate heartbeatCallback, bool isSynchronious, bool useSocketPool)
        {
            Socket TCPSocket = null;

            if (useSocketPool)
            {
                TCPSocket = CheckIfSocketAlreadyOpen(source, destination);
            }
            HeartbeatInfo info = null;

            try {
                if (TCPSocket == null)
                {
                    if (protocol == ProtocolType.Udp)
                    {
                        TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, protocol);
                    }
                    else if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, protocol);
                    }

                    TCPSocket.Bind(source);
                    if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket.Connect(destination);
                        if (useSocketPool)
                        {
                            info = new HeartbeatInfo(TCPSocket, heartbeatMessage, responseFormat, heartbeatCallback, 10);
                            info.CallbackDelegate += new BytesReceivedDelegate(CheckIfHeartbeatFailed);
                            lock (openSocketsList)
                            {
                                openSocketsList.Add(info);
                            }
                        }
                    }
                }
//                lock (TCPSocket) {
                if (bufferToSend != null)
                {
                    if (protocol == ProtocolType.Tcp)
                    {
                        TCPSocket.Send(bufferToSend);
                    }
                    else if (protocol == ProtocolType.Udp)
                    {
                        TCPSocket.SendTo(bufferToSend, destination);
                    }
                }

                StateObject state = new StateObject();
                if (isSynchronious)
                {
                    TCPSocket.ReceiveTimeout = 5000;
                    TCPSocket.Receive(state.buffer);
                    if (!useSocketPool)
                    {
                        TCPSocket.Shutdown(SocketShutdown.Both);
                        TCPSocket.Close();
                    }
                    return(state.buffer);
                }
                else if (callBack != null)
                {
                    state.workSocket        = TCPSocket;
                    state.callbackDelegate += callBack;
                    if (info != null)
                    {
                        state.callbackDelegate += info.ProcessMessageReceivedFromSocket;
                    }
                    TCPSocket.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                           new AsyncCallback(ReadCallback), state);
                }
                //               }
                return(null);
            }
            catch (Exception ex) {
                LoggingHelper.LogExceptionInApplicationLog(ex.Source, ex, EventLogEntryType.Error);
                LoggingHelper.WriteExceptionLogEntry(ex.Source, ex);

                if (TCPSocket != null && TCPSocket.Connected)
                {
                    TCPSocket.Shutdown(SocketShutdown.Both);
                    TCPSocket.Close();
                }
            }
            return(null);
        }