tags:

views:

209

answers:

2

I am using a default port now.

TcpListener serverSocket = new TcpListener(9999);

using System;
using System.Threading;
using System.Net.Sockets;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        public static Hashtable clientsList = new Hashtable();

        static void Main(string[] args)
        {
            //TcpListener serverSocket = new TcpListener(portFromAppConfig);

            TcpListener serverSocket = new TcpListener(9999);
            TcpClient clientSocket = default(TcpClient);
            int counter = 0;

            serverSocket.Start();
            Console.WriteLine("Welcome to NYP Chat Server ");
            counter = 0;
            while ((true))
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();

                byte[] bytesFrom = new byte[10025];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                clientsList.Add(dataFromClient, clientSocket);

                broadcast(dataFromClient + " Connected ", dataFromClient, false);

                Console.WriteLine(dataFromClient + " has join the chat room ");
                handleClinet client = new handleClinet();
                client.startClient(clientSocket, dataFromClient, clientsList);
            }

            clientSocket.Close();
            serverSocket.Stop();
            Console.WriteLine("exit");
            Console.ReadLine();
        }

        public static void broadcast(string msg, string uName, bool flag)
        {
            foreach (DictionaryEntry Item in clientsList)
            {
                TcpClient broadcastSocket;
                broadcastSocket = (TcpClient)Item.Value;
                NetworkStream broadcastStream = broadcastSocket.GetStream();
                Byte[] broadcastBytes = null;

                if (flag == true)
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg);
                }
                else
                {
                    broadcastBytes = Encoding.ASCII.GetBytes(msg);
                }

                broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
                broadcastStream.Flush();
            }
        }  //end broadcast function
    }//end Main class


    public class handleClinet
    {
        TcpClient clientSocket;
        string clNo;
        Hashtable clientsList;

        public void startClient(TcpClient inClientSocket, string clineNo, Hashtable cList)
        {
            this.clientSocket = inClientSocket;
            this.clNo = clineNo;
            this.clientsList = cList;
            Thread ctThread = new Thread(doChat);
            ctThread.Start();
        }

        private void doChat()
        {
            int requestCount = 0;
            byte[] bytesFrom = new byte[10025];
            string dataFromClient = null;
            Byte[] sendBytes = null;
            string serverResponse = null;
            string rCount = null;
            requestCount = 0;

            while ((true))
            {
                try
                {
                    requestCount = requestCount + 1;
                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
                    Console.WriteLine("From client - " + clNo + " : " + dataFromClient);
                    rCount = Convert.ToString(requestCount);

                    Program.broadcast(dataFromClient, clNo, true);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }//end while
        }//end doChat
    } //end class handleClinet
}//end namespace

The question is:
How to make server accept port number from range of 5555-9999 instead or a single port?

+1  A: 

You will require one socket/listener per port you wish to listen on. Note that this will consume a large number of file handles on your server system for little to no gain.

Yann Ramin
+1  A: 

You need a listening socket on each port you care about. If you care about ports 5555 through to 9999, you need to open listening sockets on all of those ports.

For that many ports you will want to use asynchronous accepts, see BeginAcceptTcpClient. Just create your TcpListeners in a loop and on each one set up an asynchronous accept.

You will need to keep track of the sockets; use the container of your choice to store the references.

Quick overview, exception handling, socket management, etc., left out.

static void incoming_connection(IAsyncResult ar) {
    TcpListener l = (TcpListener) ar.AsyncState;
    TcpClient client = l.EndAcceptTcpClient(ar);
    l.BeginAcceptTcpClient(new AsyncCallback(incoming_connection), l);

    // Do something with "client"
}

void setup() {
    for (uint port = 5555; port < 10000; ++port) {
        TcpListener l = new TcpListener(port);
        l.Start();
        l.BeginAcceptTcpClient(new AsyncCallback(incoming_connection), l);
        save_listener(l);   // Keep a reference somewhere appropriate.
    }
}
janm
how can i do it?
lewis