2010-01-06 198 views
0

我现在使用默认端口号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

您应该限制端口大于1024:http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers – 2010-01-06 02:26:30

+1

添加到Rubens注释中,* nix不会让root以外的用户使用注册端口。 DNS请求通过端口53 UDP(TCP用于执行传输的DNS服务器),25用于SMTP(电子邮件)等。粘贴到未注册的区域。 – 2010-01-06 02:29:26

回答

1

设置添加到您的程序,这样就可以更改端口...

项目属性 - >设置选项卡 - >创建一个名为PORTNUMBER Int32类型的设置,与9999

默认值在代码中,有Properties.Settings.Default.PortNumber


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

  1. 在这个范围内许多港口都已经被其他进程
  2. 你不需要听这么多的端口上,...

如果你担心有多个用户同时连接,这不是一个问题:只使用一个新的线程来处理每个传入的连接,并在主线程上的听众再次呼吁AcceptTcpClient

+0

“你不需要听这么多端口” - 除非你'inetd' :-) – paxdiablo 2010-01-06 02:41:10

+0

嗯我的意思是,居然让在客户端用户从范围内数手动选择1端口号0 - 9999。 – lewis 2010-01-06 03:31:56

+0

您需要在服务器端选择此项 - 服务器必须已经侦听客户端进行连接。 – 2010-01-06 17:16:32

-1

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

0

最好的办法是使通过application configuration file自定义的端口号。 这将允许服务器更改端口而无需重新编译。

至于代码的话,你只需要设置的端口用在这里:

TcpListener serverSocket = new TcpListener(portFromAppConfig); 

而且,你不应该使用的端口低于1024 - 这些被保留端口的系统服务。你应该坚持更高范围的数字(一般来说)端口。

+0

哪里可以找到应用程序配置文件? – lewis 2010-01-06 03:30:58

+0

它将在调试期间为app.config,在部署时为yourprogram.exe.config。请参阅我发布的链接了解详细信息。 – 2010-01-06 17:19:38