2011-10-03 6 views
9

वर्तमान में मैं ऐसा कुछ कर रहा हूं:क्या टीसीपी लिस्टर वर्तमान में सुन रहा है या नहीं यह निर्धारित करने के लिए कोई संपत्ति/विधि है?

public void StartListening() 
{ 
    if (!isListening) 
    { 
     Task.Factory.StartNew(ListenForClients); 

     isListening = true; 
    } 
} 

public void StopListening() 
{ 
    if (isListening) 
    { 
     tcpListener.Stop(); 

     isListening = false; 
    } 
} 

क्या टीसीपी लिस्टनर के भीतर कोई विधि या संपत्ति नहीं है यह निर्धारित करने के लिए कि कोई टीसीपीलिस्टर सुनना शुरू कर चुका है (यानी टीसीपी लिस्टनर.स्टार्ट() को बुलाया गया था)? वास्तव में टीसीपी लिस्टनर तक नहीं पहुंच सकता है। सर्वर क्योंकि अगर यह शुरू नहीं हुआ है, तो इसे अभी भी तत्काल नहीं किया गया है। यहां तक ​​कि अगर मैं इसे एक्सेस कर सकता हूं तो भी मुझे यकीन नहीं है कि इसमें एक सुनवाई संपत्ति भी शामिल है।

क्या यह वाकई सबसे अच्छा तरीका है?

+0

आप कैसे नहीं जानते कि * आपका अपना कोड * प्रारंभ() कहलाता है? थोड़ा सा फिर से सोचो। –

+0

@ हंसपैसेंट: एक उपयोगकर्ता इंटरफ़ेस है। प्रारंभ तब कहा जाता है जब उपयोगकर्ता विंडोज़ फॉर्म पर स्टार्ट बटन पर क्लिक करता है। –

+0

क्लिक इवेंट हैंडलर के लिए कोड किसने लिखा था? तुम नहीं? बड़ा सवाल: उपयोगकर्ता एक बटन पर क्लिक क्यों करना चाहेंगे? –

उत्तर

18

टीसीपी लिस्टनर वास्तव में सक्रिय नामक एक संपत्ति है जो वास्तव में आप चाहते हैं। हालांकि, संपत्ति को किसी कारण से संरक्षित किया गया है ताकि आप तब तक इसका उपयोग नहीं कर सकें जब तक कि आप टीसीपीलिस्टर वर्ग से प्राप्त नहीं होते।

आप अपनी परियोजना में एक साधारण रैपर जोड़कर इस सीमा के आसपास हो सकते हैं।

/// <summary> 
/// Wrapper around TcpListener that exposes the Active property 
/// </summary> 
public class TcpListenerEx : TcpListener 
{ 
    /// <summary> 
    /// Initializes a new instance of the <see cref="T:System.Net.Sockets.TcpListener"/> class with the specified local endpoint. 
    /// </summary> 
    /// <param name="localEP">An <see cref="T:System.Net.IPEndPoint"/> that represents the local endpoint to which to bind the listener <see cref="T:System.Net.Sockets.Socket"/>. </param><exception cref="T:System.ArgumentNullException"><paramref name="localEP"/> is null. </exception> 
    public TcpListenerEx(IPEndPoint localEP) : base(localEP) 
    { 
    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="T:System.Net.Sockets.TcpListener"/> class that listens for incoming connection attempts on the specified local IP address and port number. 
    /// </summary> 
    /// <param name="localaddr">An <see cref="T:System.Net.IPAddress"/> that represents the local IP address. </param><param name="port">The port on which to listen for incoming connection attempts. </param><exception cref="T:System.ArgumentNullException"><paramref name="localaddr"/> is null. </exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="port"/> is not between <see cref="F:System.Net.IPEndPoint.MinPort"/> and <see cref="F:System.Net.IPEndPoint.MaxPort"/>. </exception> 
    public TcpListenerEx(IPAddress localaddr, int port) : base(localaddr, port) 
    { 
    } 

    public new bool Active 
    { 
     get { return base.Active; } 
    } 
} 

कौन सा आप किसी भी TcpListener वस्तु के स्थान पर उपयोग कर सकते हैं।

TcpListenerEx tcpListener = new TcpListenerEx(localaddr, port); 
संबंधित मुद्दे

 संबंधित मुद्दे