简体   繁体   中英

Connecting to the same IP address and port many times from the same PC

I am testing a device and one of the items to test is they say I can have as many connections to it as I like. Obviously there is a limit but what is it. I thought I would put a simple app together to just make connections to the device.

I find in c# it is easy to make a connection so my theory is just give each connection a unique name eg tcpclnt_1, tcpclnt_2, tcpclnt_3 etc.

I started with just one connection and that works well. My problem is that I can hard code each name so I can declare

public TcpClient tcpclnt_1 = new TcpClient();
public TcpClient tcpclnt_2 = new TcpClient();

but this is not dynamic. Below is some code where I outline what I was trying to do. It will not work as I cannot find a way to dynamically change each tcpclnt_x to a unique name. I may not even be doing this the correct way so any ideas on how I can connect multiple times to the same device?

 public TcpClient tcpclnt_x = new TcpClient();
 int iterations = Decimal.ToInt32(numupdown_iterations.Value);

        private void btn_creatConnections_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < iterations; i++)
            {
                tcpclnt_x.Connect("192.168.127.254", 721);
            }

        }

        private void btn_delete_connections_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < iterations; i++)
            {
                tcpclnt_x.Close();
            }
        }

You need to investigate lists and other collections.

public List<TcpClient> tcpclnts = new List<TcpClient>();
int iterations = Decimal.ToInt32(numupdown_iterations.Value);

    private void btn_creatConnections_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < iterations; i++)
        {
            var client = new TcpClient();
            client.Connect("192.168.127.254", 721);
            tcpclnts.Add(client);
        }

    }

    private void btn_delete_connections_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < iterations; i++)
        {
            tcpclnts[x].Close();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM