Ejemplo n.º 1
0
        private async void BtnSendRequest_Click(object sender, RoutedEventArgs e)
        {
            if (btnSendRequest.Content.Equals(REQUEST_TIMER_STOP))
            {
                tbxRequestInterval.IsReadOnly = false;

                client.AutomaticRequestInterval = Client.DEF_AUTOMATIC_REQUEST_INTERVAL;
                client.AutomaticRequestEndPoint = null;
                client.AutomaticRequestString   = string.Empty;

                btnSendRequest.Content = REQUEST_TIMER_START;
                return;
            }

            IPAddress ip;

            try
            {
                clientStatusReporter.Report("Resolving remote host adress " + tbxRemoteIp.Text + "...");
                ip = await TcpIp.Helper.GetHostIpAsync(tbxRemoteIp.Text);
            }
            catch (Exception ex)
            {
                clientStatusReporter.Report("An error occurred:\n" + ex.Message);
                return;
            }

            IPEndPoint requestEndpoint = new IPEndPoint(ip, tbxRemotePort.Value);

            if (btnSendRequest.Content.Equals(REQUEST_SEND))
            {
                await client.RequestAsync(tbxRequest.Text, requestEndpoint);
            }
            else
            {
                tbxRequestInterval.IsReadOnly = true;

                client.AutomaticRequestInterval = tbxRequestInterval.Value;
                client.AutomaticRequestEndPoint = requestEndpoint;
                client.AutomaticRequestString   = tbxRequest.Text;

                btnSendRequest.Content = REQUEST_TIMER_STOP;
            }
        }
Ejemplo n.º 2
0
        public Task RequestAsync(String requestString, IPEndPoint remoteEndPoint)
        {
            return(Task.Run(() =>
            {
                StateReporter statusReporter = new StateReporter(++CLIENT_ID, this);

                try
                {
                    using (TcpIpClient client = new TcpIpClient(remoteEndPoint.AddressFamily, Preferences.ClientStringEncoding, Preferences.ReceiveTimeout, Preferences.SendTimeout, TcpIpClient.DEF_RECEIVE_BUFFER_SIZE))
                    {
                        statusReporter.Report("Connecting to " + remoteEndPoint + "...");
                        client.Connect(remoteEndPoint);

                        if (Preferences.ReplaceHex)
                        {
                            requestString = Utilities.ConvertAllHex(requestString, Preferences.ClientStringEncoding);
                        }

                        statusReporter.Report("Connected to " + client.RemoteEndPoint + ".");

                        client.Send(requestString);
                        statusReporter.Report("Request sent to " + client.RemoteEndPoint + ":", requestString, true);

                        string respsonseString;

                        while (true)
                        {
                            respsonseString = client.Receive();

                            if (respsonseString == null)
                            {
                                statusReporter.Report("The socket at " + client.RemoteEndPoint + " was shut down.");
                                break;
                            }
                            else
                            {
                                statusReporter.Report("Response received from " + client.RemoteEndPoint + ":", respsonseString, true);
                            }
                        }
                    }
                }
                catch (SocketException ex)
                {
                    statusReporter.Report("Socket Error " + ex.ErrorCode + ":\n" + ex.Message);
                }

                statusReporter.Report("Client stopped.");
            }));
        }
Ejemplo n.º 3
0
        public async void BtnToggleServer_Click(object sender, RoutedEventArgs e)
        {
            if (server.Running)
            {
                server.Stop();
            }
            else
            {
                UInt16 port = (UInt16)tbxLocalPort.Value;

                if (TcpIp.Helper.IsAvailablePort(port))
                {
                    await server.StartAsync(port);
                }
                else
                {
                    serverStatusReporter.Report("Server can't bind to port " + port + ", it's already in use.");
                }
            }
        }
Ejemplo n.º 4
0
        public Task StartAsync(int port)
        {
            startCancellationTokenSource = new CancellationTokenSource();
            CancellationToken cancellationToken = startCancellationTokenSource.Token;
            IPEndPoint        localEndPoint     = new IPEndPoint(IPAddress.Any, port);

            return(Task.Run(() =>
            {
                try
                {
                    using (server = new TcpIpServer(localEndPoint, localEndPoint.AddressFamily, Preferences.ServerStringEncoding, Preferences.ReceiveTimeout, Preferences.SendTimeout, TcpIpServer.DEF_LISTENER_QUEUE_SIZE, TcpIpServer.DEF_RECEIVE_BUFFER_SIZE))
                    {
                        displayedLocalEndPoint = new IPEndPoint(TcpIp.Helper.GetHostIpAsync(Dns.GetHostName()).Result, port);
                        server.Listen();
                        stateReporter.Report("Server started at " + LocalEndPoint + ".");

                        while (!cancellationToken.IsCancellationRequested)
                        {
                            server.AcceptSocket();
                            stateReporter.Report("Connected to " + server.RemoteEndPoint + ".");

                            lastRequestString = string.Empty;

                            while (!cancellationToken.IsCancellationRequested)
                            {
                                try
                                {
                                    lastRequestString = server.Receive();
                                }
                                catch (SocketException e)
                                {
                                    stateReporter.Report("Socket Error " + e.ErrorCode + ":\n" + e.Message);
                                    break;
                                }

                                if (lastRequestString == null)
                                {
                                    stateReporter.Report("The socket at " + server.RemoteEndPoint + " was shut down.");
                                    break;
                                }
                                else
                                {
                                    stateReporter.Report("Request received from " + server.RemoteEndPoint + ":", lastRequestString, true);
                                }

                                if (AutomaticResponseEnabled)
                                {
                                    RespondAutomatically();
                                }
                            }

                            //if (server.Connected)
                            //{
                            EndPoint remoteEndPoint = server.RemoteEndPoint;
                            server.Disconnect();
                            stateReporter.Report("Disconnected from " + remoteEndPoint + ".");
                            //}
                        }
                    }
                }
                catch (SocketException e)
                {
                    // SocketError.Interrupted is thrown when Dispose() is called
                    // while the TcpIpServer is waiting for a connection using AcceptSocket()
                    // See Stop() below
                    if (e.SocketErrorCode != SocketError.Interrupted)
                    {
                        stateReporter.Report("Socket Error " + e.SocketErrorCode + ":\n" + e.Message);
                    }
                }

                displayedLocalEndPoint = null;
                stateReporter.Report("Server stopped.");
            }));
        }