服务器端口号

时间:2010-01-06 02:21:46

标签: c#

我现在使用默认端口号TcpListener serverSocket = new TcpListener(9999); 但是因为在我的客户端,我已经放了一个文本框,允许用户手动键入端口号。那么如何让我的服务器端允许从端口1到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(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

3 个答案:

答案 0 :(得分:1)

为您的程序添加设置,以便您可以更改端口...

项目属性 - >设置标签 - >创建名为PortNumber的设置,类型为Int32,默认值为9999

在您的代码中,使用Properties.Settings.Default.PortNumber

检索值
编辑:我误解了这个问题。你想同时收听1到9999的所有端口吗?这没有意义,因为

  1. 该范围内的许多端口已被其他进程使用
  2. 你不需要来收听这么多端口......
  3. 如果您担心同时连接多个用户,那不是问题:只需使用新线程来处理每个传入连接,并在主线程上的侦听器上再次调用AcceptTcpClient

答案 1 :(得分:0)

最好的选择是通过application configuration file使端口号可自定义。   这将允许服务器在不重新编译的情况下更改端口。

就代码而言,您只需要在此处设置端口:

TcpListener serverSocket = new TcpListener(portFromAppConfig);

此外,您不应使用低于1024的端口 - 这些是系统服务的保留端口。您应该坚持使用更高数字范围的端口(通常)。

答案 2 :(得分:-1)

您通常没有一台服务器在多个端口上侦听;这很奇怪。你所做的是接受连接,然后将它们传递给另一个线程进行处理,然后再回到接受更多连接。