Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How many clients can connect to this server? How do I change the server code?

C#
private void btnserverconect_Click(object sender, EventArgs e)
{
    Form1_Load(sender, e);
}

private void Form1_Load(object sender, EventArgs e)
{
    try {
        server =
            new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                   ProtocolType.Tcp);

        //IPAddress local = IPAddress.Parse("127.0.0.1");
        EndPoint destination = new IPEndPoint(IPAddress.Any, 8000);
        server.Bind(destination);
        server.Listen(5);
        Thread wait = new Thread(wa);
        wait.Start();
    }
    catch(Exception) 
    {
        MessageBox.Show("connection error !");
    }
}

void wa()
{
    label1.Text = "please wait...";
    server = server.Accept();
    label1.Text = "connect";
    while (true) {
        try 
       {
            byte[]by = new byte[100];
            int n = server.Receive(by);
            lstserver.Items.Add("client :" +
                        Encoding.ASCII.GetString(by, 0, n));
        }
        catch(Exception) 
        {
        }
    }
}
Posted
Updated 28-Dec-11 14:19pm
v3

1 solution

On the same port, you can connect unlimited number of clients to each listening socket of instance of System.Net.Sockets.TcpListener (limited by factors like available memory and the like). In this case, on each socket or TcpClient accepted by the server side, the server side obtains an instance of socket or TcpClient representing remote socket of the connected client. These instances can be used by a server side to send/recieve or write/read to/from the network stream (in case if TcpListener).

I don't know how to change your code just because I don't know your goal. However, the code you've shown does not look like having full sense. First of all, this is because you are not using threads. A server side needs two additional thread as minimum: one to accept new connections, another one to send/receive socket data or write/read to/from the network streams.

If you are trying to perform any blocking calls in your UI, consider you are done — it can never work properly.

Not clear why do you want to use Forms application for the server side. The adequate form of server-side application is a Windows Service. However, if you want to use the Form application for introductory experiments, research and/or prototyping, it's perfectly fine.

You can find some ideas I shared on this topic in my past solution:
Multple clients from same port Number[^].

For threads working with UI (which is more typically developed on the client side), you need to use UI thread invocation. Please see:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

For more on threading, please see my collection of my past solutions:

How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 
Comments
fjdiewornncalwe 28-Dec-11 23:23pm    
Excellent analysis of the problem. +5
Sergey Alexandrovich Kryukov 28-Dec-11 23:27pm    
Thank you, Marcus.
--SA
thatraja 29-Dec-11 0:01am    
Nice, 5!
Sergey Alexandrovich Kryukov 29-Dec-11 1:04am    
Thank you, Raja.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900