IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C# Discussion :

Transfert FTP - problème de sockets


Sujet :

C#

Mode arborescent

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2009
    Messages
    10
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Maroc

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Décembre 2009
    Messages : 10
    Par défaut Transfert FTP - problème de sockets
    Bonjour,

    Dans mon application c# un bouton qui permet l'upload des fichiers dans un serveur FTP, pour celà j'utilise la classe en pièce-jointe.

    les informations du serveur FTP sont dans le web.config:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <FtpConfigClass ftpServer="xx.xx.xx.xx" ftpUser="user_upload" ftpPassword="a_123456" ftpPort="21" bufferSize="512" timeOut="10"/>
    Je commence dans un premier temps par recupérer ces informations depuis le fichier de configuration puis j'utilise la fonction FTP.FtpLogin(); de la classe FTP (en pièce-jointe).

    Cette fonction fait appel à readResponse(); qui permet de récupérer la réponse du serveur grace à ParseHostResponse();.
    dans les 3/4 des cas le serveur ne m'envoi aucune réponses

    FtpLogin:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
     
    public void FtpLogin()
            {
                //check if the connection is currently open
                if (_isLoggedIn)
                {
                    //its open so we need to close it
                    CloseConnection();
                }
                //message that we're connection to the server
                Debug.WriteLine("Opening connection to " + _ftpServer, "FtpClient");
                //create our ip address object
                IPAddress remoteAddress = null;
                //create our end point object
                IPEndPoint addrEndPoint = null;
                try
                {
                    //create our ftp socket
                    ftpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    //retrieve the server ip
                    remoteAddress = Dns.GetHostEntry(_ftpServer).AddressList[0];
                    //set the endpoint value
                    addrEndPoint = new IPEndPoint(remoteAddress, _ftpPort);
                    //connect to the ftp server
                    ftpSocket.Connect(addrEndPoint);
                }
                catch (Exception ex)
                {
                    // since an error happened, we need to
                    //close the connection and throw an exception
                    if (ftpSocket != null && ftpSocket.Connected)
                    {
                        ftpSocket.Close();
                    }
                    throw new FtpException("Couldn't connect to remote server", ex);
                }
                //read the host response
                readResponse();
                //check for a status code of 220
                if (_statusCode != 220)
                {
                    //failed so close the connection
                    CloseConnection();
                    //throw an exception
                    throw new FtpException(result.Substring(4));
                }
                //execute the USER ftp command (sends the username)
                Execute("USER " + _ftpUsername);
                //check the returned status code
                if (!(_statusCode == 331 || _statusCode == 230))
                {
                    //not what we were looking for so
                    //logout and throw an exception
                    LogOut();
                    throw new FtpException(result.Substring(4));
                }
                //if the status code isnt 230
                if (_statusCode != 230)
                {
                    //execute the PASS ftp command (sends the password)
                    Execute("PASS " + _ftpPassword);
                    //check the returned status code
                    if (!(_statusCode == 230 || _statusCode == 202))
                    {
                        //not what we were looking for so
                        //logout and throw an exception
                        LogOut();
                        throw new FtpException(result.Substring(4));
                    }
                }
                //we made it this far so we're logged in
                _isLoggedIn = true;
                //verbose the login message
                Debug.WriteLine("Connected to " + _ftpServer, "FtpClient");
                //set the initial working directory
                ChangeWorkingDirectory(_ftpPath);
            }
    readResponse:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    private void readResponse()
            {
                statusMessage = "";
                result = ParseHostResponse();
                _statusCode = int.Parse(result.Substring(0, 3));
            }
    ParseHostResponse:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
     
    private string ParseHostResponse()
            {
                //while (true)
                //{
     
                //    //retrieve the host response and convert it to
                //    //a byte array
                //    bytes = ftpSocket.Receive(buffer, buffer.Length, 0);
                //    //decode the byte array and set the
                //    //statusMessage to its value
                //    statusMessage += ASCII.GetString(buffer, 0, bytes);
                //    //check the size of the byte array
                //    if (bytes < buffer.Length)
                //    {
                //        break;
                //    }
                //}
                do
                {
                    try
                    {
                        bytes += ftpSocket.Receive(buffer, 0 + bytes, buffer.Length - bytes, SocketFlags.None);
                        statusMessage += ASCII.GetString(buffer, 0, bytes);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.WouldBlock ||
                          ex.SocketErrorCode == SocketError.IOPending ||
                          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                        {
                            // socket buffer is probably empty, wait and try again
                            Thread.Sleep(30);
                        }
                        else
                            throw ex;  // any serious error occurr
     
                    }
                } while (ftpSocket.Available > 0);
     
     
                //split the host response
                string[] msg = statusMessage.Split('\n');
                //check the length of the response
                if (statusMessage.Length > 2)
                    statusMessage = msg[msg.Length - 2];
                else
                    statusMessage = msg[0];
     
                //check for a space in the host response, if it exists return
                //the message to the client
                if (!statusMessage.Substring(3, 1).Equals(" ")) return ParseHostResponse();
                //check if the user selected verbose Debugging
                if (_doVerbose)
                {
                    //loop through the message from the host
                    for (int i = 0; i < msg.Length - 1; i++)
                    {
                        //write each line out to the window
                        Debug.Write(msg[i], "FtpClient");
                    }
                }
                //return the message
                return statusMessage;
            }
    Quelqu'un à une idée ?

    Merci d'avance
    Fichiers attachés Fichiers attachés
    • Type de fichier : cs FTP.cs (28,8 Ko, 197 affichages)

Discussions similaires

  1. Réponses: 0
    Dernier message: 11/02/2008, 10h52
  2. Problème de transfert ftp
    Par zoumzoum59 dans le forum Free
    Réponses: 6
    Dernier message: 22/02/2007, 22h40
  3. [FTP] problème de transfert
    Par adaneels dans le forum Serveurs (Apache, IIS,...)
    Réponses: 1
    Dernier message: 17/01/2007, 12h42
  4. [FTP] Problème transfert FTP en PHP
    Par tiger63 dans le forum Langage
    Réponses: 4
    Dernier message: 16/12/2006, 12h19
  5. Problème de transfert FTP sous IIS
    Par thanathz dans le forum Développement
    Réponses: 2
    Dernier message: 12/07/2002, 15h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo