tcpserver port

cardmaker

Member
Joined
Jan 12, 2019
Messages
21
Programming Experience
Beginner
hello, is there a way to load port of tcplistener server when form loads?
example:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;



namespace teste_server_tcp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        int port = //load int port content from textBox1.Text(already fill up by settings);

        TcpListener server = new TcpListener(IPAddress.Any, port);
    }
}

any sugestion?
 
(texbox is already fillup by setting)
Explain please? What setting.

A Textbox .Text property is a string variable. The port is normally an integer value. To do that you would need to convert to int, or cast if you are sure of the input provided by the user.
 
TcpListener server = new TcpListener(IPAddress.Any, Convert.ToInt16(textBox1.Text)); => Not recommended.

int.TryParse(textBox1.Text, out int port); TcpListener server = new TcpListener(IPAddress.Any, port); => Better, but not perfect.

If your user will be inputting the port to the textbox, you should be using the relevant validation events to validate the user input first, similarly to how it is on that link.

You can call server.Start(); from your form load event to start the listener. Just to add, its generally best practice to execute your code on NON-UI threads. Ie, start a new thread from your form load event and run your execution methods through a new thread.
 
@cardmaker: It is a really poor practice to make your UI your data model. Your data is actually the configuration setting. Why are you using your UI to carry the value over to the form load event handler? You should instead be reading the setting directly at form load.

Yes back in the 80's and 90's it was very common for the UI to hold the data model. It's why Microsoft, Borland, and a host of other companies were "controls" crazy. All you had to do back then was just load you data into a whizbang control and it could make you a 3-D graph, a projected rate of increase, and an omelette for breakfast in the morning. By the 2000's though, developers wised up the the much better MV* approaches: MVC, MVP, MVVM, etc, where the data (e.g the M for Model) is separate from the UI (e.g. the V for View).
 
@Sheepings your option works only if inside a function, when calling TcpListener glabaly not like in the example.

@Skydiver , yes i could read dorm setting but how?

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Configuration;


namespace teste_server_tcp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MyUserSettings port;
            port = new MyUserSettings();
            textBox5.Text = port.porta;
        }
// how do i pass the value of my setting for int port in Tcplistener
        //int port = //load int port content from textBox1.Text(already fill up by settings);

        TcpListener server = new TcpListener(IPAddress.Any, port);
    }
}

thia way how do i pass the value of my setting for int port in Tcplistener?
 
You can read from the settings the same way you currently are reading from it on lines 25-27. So either you don't understand the code that you wrote, or you didn't write that code in the first place.
 
thia way how do i pass the value of my setting for int port in Tcplistener?
Well not like that anyway....

I advise reading up on declarations.

public static int Port { get; set; }

Then once you validate your text property of your textbox control, you can set the static property from said event.

So either you don't understand the code that you wrote, or you didn't write that code in the first place.
Evidently true. But sure this is the new age now, were upcoming developers google their way through creating applications.
 
in this case:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Configuration;


namespace teste_server_tcp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        
        }
        
        MyUserSettings port;
        port = new MyUserSettings();
        port.porta = textBox5.Text;
        

        TcpListener server = new TcpListener(IPAddress.Any, 33333);
    }

gives error: Invalide token = etc
 
Where are you using a validation event in that code?

Why move the variables?

Where are you parsing the data from the .text property of the textbox control to int?
 
Don't take this as a literal solution. You should spend time looking up the validation events as well as the text changed event and change the code as you see fit and perhaps change the events to more suitable events relative to what you're doing or trying to achieve. This is just to prove that you can set the port anywhere from any method. If you are struggling with this, you really should go back and reflect on your study material, as this is something you should have been able to do yourself, as It's my assumption that you skipped the chapters involving scope and declarations. Anyway.

The below code checks per each char typed into the textbox to ensure the user is typing a numeric value. If not, they get told off for it. This event fires when the .text property is changed. If the value in the textbox is numerical, it sets a bool field depending if numerical or not :
C#:
        public bool textBox1__Validated;
TextChanged Event :
C#:
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.All(char.IsDigit))
                textBox1__Validated = true;
            else
            {
                textBox1__Validated = false;
                MessageBox.Show("You entered a non numerical character. Try Again...", "The Port Must Be Numerical", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
When the user leaves the textbox control, it fires a validated event once focus has been lost :
C#:
        private void textBox1_Validated(object sender, EventArgs e)
        {
            if (textBox1__Validated)
            {
                int.TryParse(textBox1.Text, out int parsed);
                Port = parsed;
            }
        }
This event checks the bool field for its current state, and if the state is true (confirming the port is numerical from previously set in other TextChanged event), then the port is parsed and the port of your public field is also set to use the port the user wrote into the textbox :
C#:
        public static int Port { get; set; }
Then when you call your server.Start() from your form load, it will have the port which was parsed from your textbox through your port field. Note port is spelt as Port with an uppercase P for the sake of satisfying naming convention rules in C# :
C#:
private TcpListener server = new TcpListener(IPAddress.Any, Port);

In this instance, I've started it with a button and it is working fine, and I even checked it with netstat -a in CMD.
C#:
        private void button1_Click(object sender, EventArgs e)
        {
            server.Start(5);
        }
 
Hmm, if you want to read directly from a property, ie a setting in settings or something to that effect, then you should bind the textbox to your property instead, and use binding events for that textbox including the INotifyPropertyChanged Interface for when the value of your property changes. Yes, INotifyPropertyChanged Interface is more commonly used in WPF, and yes, it can also be used in winforms too.
 
Back
Top Bottom