2017-04-20 3 views
0


Ich habe ein Problem mit den Sockets in meinem Programm. Ich entwickle einen FTP-Server, der in einem anderen Programm eingebettet ist, und um das zu tun, habe ich einen Socket und einen TCPListener erstellt. Mein Problem tritt in diesem Fall auf:
- Ich öffne die Verbindung zum ersten Mal, wenn niemand mich verbindet und ich schließe, alles in Ordnung; Sonst funktioniert alles, wenn sich jemand mit meinem FTP-Server verbindet;
- Wenn, nach dem Schließen der Verbindung das erste Mal und öffnen Sie es wieder ohne dass jemand zuvor verbunden wurde, bekomme ich eine SocketException von "socket/ip address/port" bereits in Verwendung und ich kann es nicht erneut instanziieren.
Der Code meiner Funktion ist, dass:Empfangen Timeout in Compact Framework 2.0

public void Start() 
    { 
     ServerStarted = true; 

     if (this.authHandler == null) 
      this.authHandler = new DefaultAuthHandler(); 

     if (this.fsHandler == null) 
      this.fsHandler = new DefaultFileSystemHandler(); 

     Socket socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 

     TcpListener listenerServer = new TcpListener(endpoint); 
     listenerServer.Start(); // The exception is here at the second time 
           // I open the connection without anybody connected before 

     try 
     { 
      while (ServerStarted) 
      { 
       socketServer = listenerServer.AcceptSocket(); 

       try 
       { 
        IPEndPoint socketPort = (IPEndPoint)socketServer.RemoteEndPoint; 
        Session session = new Session(socketServer, bufferSize, this.authHandler.Clone(socketPort), this.fsHandler.Clone(socketPort)); 

        session.Start(); 
        sessions.Add(session); 

        p_HostIPAddress = socketPort.Address; 

        if (p_HostIPAddress != null) 
         HostConnected = true; 

        // Purge old sessions 
        for (int i = sessions.Count - 1; i >= 0; --i) 
        { 
         if (!sessions[i].IsOpen) 
         { 
          sessions.RemoveAt(i); 
          --i; 
         } 
        } 
       } 
       catch(Exception ex) 
       { 
       } 
      } 
     } 
     catch (SocketException socketEx) 
     { 
     } 
     finally 
     { 
      foreach (Session s in sessions) 
      { 
       s.Stop(); 
       HostConnected = false; 
      } 
      TcpSocketListener = null; 
     } 
    } 

Ich weiß, dass in Compact Framework gibt es nicht die Timeout Anrufe und so, was kann ich, dass meine Fassung noch offen im Hintergrund zu vermeiden tun. Ohw, rechts, ein andere zusätzliche Informationen:
mein FTP-Server tatsächlich die Bibliothek OpenNETCF FTP verwenden

Antwort

0

Ok, ich habe die SetSocketOption gelöst mit und ich habe ein wenig den Code geändert. Hier ist es jetzt:

public void Start() 
    { 
     ServerStarted = true; 

     if (this.authHandler == null) 
      this.authHandler = new DefaultAuthHandler(); 

     if (this.fsHandler == null) 
      this.fsHandler = new DefaultFileSystemHandler(); 

     socketServer = new Socket(AddressFamily.InterNetwork, 
            SocketType.Stream, 
            ProtocolType.Tcp); 

     /* Here is the change. In this way I can reuse multiple times the 
     * same address without getting the SocketException.*/ 
     socketServer.SetSocketOption(SocketOptionLevel.Socket, 
            SocketOptionName.ReuseAddress, 
            1); 

     Socket handler = null; 

     try 
     { 
      socketServer.Bind(endpoint); 
      socketServer.Listen(10); 

      while (ServerStarted) 
      { 
       handler = socketServer.Accept(); 

       try 
       { 
        IPEndPoint socketPort = (IPEndPoint)handler.RemoteEndPoint; 
        Session session = new Session(handler,bufferSize, 
             this.authHandler.Clone(socketPort), 
             this.fsHandler.Clone(socketPort)); 

        session.Start(); 
        sessions.Add(session); 

        p_HostIPAddress = socketPort.Address; 

        if (p_HostIPAddress != null) 
         HostConnected = true; 

        // Purge old sessions 
        for (int i = sessions.Count - 1; i >= 0; --i) 
        { 
         if (!sessions[i].IsOpen) 
         { 
          sessions.RemoveAt(i); 
          --i; 
         } 
        } 
       } 
       catch(Exception ex) 
       { 
       } 
      } 
     } 
     catch (SocketException socketEx) 
     { 
     } 
     finally 
     { 
      handler.Close(); 

      foreach (Session s in sessions) 
      { 
       s.Stop(); 
       HostConnected = false; 
      } 
     } 
    } 
Verwandte Themen