2011-08-17 3 views
0

GSM/GPRS 모뎀을 사용하여 작은 tcp/ip 연결을 설정하고 있는데, 서버 (수신기) 프로그램을 실행하는 서버 PC가 있고 여러 장소에 여러 모뎀 (100 개 이상)이 있습니다. 특정 기간에 서버에 대한 데이터 패킷.TCP/IP 서버가 여러 클라이언트를 수신하는 방법은 무엇입니까?

하나의 클라이언트로 시스템을 테스트했는데 여러 클라이언트에서 어떻게 테스트 할 수 있습니까? 내 서버가 여러 클라이언트에 어떻게 반응합니까?

private TcpListener tcpListener; 
    private Thread listenThread; 
    public static TcpClient client; 

    public Form1() 
    { 
     InitializeComponent(); 
     this.tcpListener = new TcpListener(IPAddress.Any, 2020); 
     this.listenThread = new Thread(new ThreadStart(ListenForClients)); 
     this.listenThread.Start(); 
    } 
    private void ListenForClients() 
    { 
     this.tcpListener.Start(); 

     while (true) 
     { 
      //blocks until a client has connected to the server 
      client = this.tcpListener.AcceptTcpClient(); 


      //create a thread to handle communication 
      //with connected client 
      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); 
      clientThread.Start(client); 
     } 
    } 
    private void HandleClientComm(object client) 
    { 
     TcpClient tcpClient = (TcpClient)client; 
     NetworkStream clientStream = tcpClient.GetStream(); 

     //TcpClient client = new TcpClient(servername or ip , port); 
     //IpEndPoint ipend = tcpClient.RemoteEndPoint; 
     //Console.WriteLine(IPAddress.Parse(ipend.Address.ToString()); 

     //label3.Text = IPAddress.Parse(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()).ToString(); 
     SetControlPropertyThreadSafe(label3, "Text", IPAddress.Parse(((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()).ToString()); 

     byte[] message = new byte[4096]; 
     int bytesRead; 

     while (true) 
     { 
      bytesRead = 0; 

      try 
      { 
       //blocks until a client sends a message 
       bytesRead = clientStream.Read(message, 0, 4096); 
      } 
      catch 
      { 
       //a socket error has occured 
       break; 
      } 

      if (bytesRead == 0) 
      { 
       //the client has disconnected from the server 
       break; 
      } 

      //message has successfully been received 
      ASCIIEncoding encoder = new ASCIIEncoding(); 
      //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead)); 

      string received_text = encoder.GetString(message, 0, bytesRead).ToString(); 

      SetControlPropertyThreadSafe(label1, "Text", received_text); 

      if (received_text == "cmdtim") 
      { 
       SendData(DateTime.Now.ToString()); 
      } 
     } 

     tcpClient.Close(); 
    } 

당신은 내가 고객을 듣기위한 별도의 스레드를 생성 한 보듯이, 어떻게 여러 클라이언트를들을 수 있습니다 : 여기

내 서버 코드?

각 클라이언트마다 스레드를 만들어야합니까?
매 순간마다 얼마나 많은 클라이언트를 보유할지 모르겠다면 서버가 특정 클라이언트를 청취 할 때 다른 클라이언트의 데이터를 버퍼링합니까?

여러 클라이언트를 수신하고 모든 데이터를 보내는 가장 좋은 전략은 무엇입니까?

답변

1

어떤 조언 :

제거 :

public static TcpClient client; 

바꾸기 :

client = this.tcpListener.AcceptTcpClient(); 

TcpClient client = this.tcpListener.AcceptTcpClient(); 
관련 문제