Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Gday Guys/Girls,

I am trying to create a TCP server and clients. I want the server to be able to handle multiple active clients 24/7 (approx 10-15 clients, possibly more). The server will send a string to the client and the client runs a command based on the string sent. I have a basic TCP Server and Client. The TCP Server only accepts 1 client at a time, could someone please provide and example or modify my code to allow the server to allow multiple clients. Also could someone provide an example on how to make the server send the command and the client wait for the command as I can only get it to work the other way around. Any help is greatly appreciated.

Also, the client will need to stay connected so that the server can send commands at any time.

Matt


SERVER CODE
C#
public static void Main(string[] args)
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                TCPListener myList = new TcpListener(ipAd, 8001);
                myList.Start();
                Console.WriteLine("The server is running at port 8001...");
                Console.WriteLine("The local end point is : " + myList.LocalEndpoint);
                Console.WriteLine("Waiting for a connection");
               // Connection();
               // Connection();
                Socket s = myList.AcceptSocket();
                Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
                byte[] b = new byte[100];
                int k = s.Receive(b);
                Console.WriteLine("Recieved...");
                string command = string.Empty;
                for (int i = 0; i < k; i++)
                {
                    command = command + Convert.ToChar(b[i]);
                }
                Console.WriteLine(command);
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes("String Recievced"));
                Console.WriteLine("Sent Acknowledgement");
                s.Close();
                myList.Stop();
                Console.ReadLine();
            }

            catch(Exception ex)
            {
                Console.WriteLine("ERROR : " + ex.Message);
            }
        }


CLIENT CODE

C#
static void Main(string[] args)
        {
            try
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect("127.0.0.1", 8001);
                string str = Console.ReadLine();
                Stream stm = tcp.GetStream();
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                stm.Write(ba, 0, ba.Length);
                byte[] bb = new byte[100];
                int k = stm.Read(bb, 0, 100);
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(bb[i]));
                
                tcp.Close();

                Console.ReadLine();
            }
            catch(Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }
        }
Posted

Microsoft has some pretty good examples for sockets, both synchronous and asynchronous.
It is a good start for you I think.

Socket Code Examples[^]

When it comes to your wish to make the server send a command and the client to wait, what you basically want is to let the client become the server and vice versa.
The client-server model means that the server waits for something to do and the clients make requests.

What you can do is to let the client hook up to the server and then create a new connection where the client is the server, and then close the first connection.
It seems like a complicated setup to me, but it is technically possible.
 
Share this answer
 
It's quite weird that you want to use multithreading and do two things in a single thread of a server side: listening for new connection and sending/receiving messages to/from a network stream. They certainly should be separate threads. For further detail, please see my past answers:
an amateur question in socket programming[^],
Multple clients from same port Number[^].


—SA
 
Share this answer
 

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