Merci pour ta réponse en modifiant un peut le code j'obtiens l'erreur suivante.
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
Cette erreur semble provenir de la void connect()de ma classe.
Voici ma Classe :
using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace FTPlibcheck
{
public class ClassFTP
{
private static TcpClient tcpclient;
private static string Data = null;
private static NetworkStream nstream = null;
private static Queue<string> pck_queue;
private static System.Threading.Thread rcv_th = null;
public string strHost, strUser, strPass = "";
public string strSystem = "";
public bool servWork = false;
public ClassFTP()
{
tcpclient = new TcpClient();
pck_queue = new Queue<string>();
}
public void connect()
{
try
{
tcpclient.Connect(strHost, 21);
if (!tcpclient.Connected)
{
//Environment.Exit(0);
return;
}
nstream = tcpclient.GetStream();
rcv_th = new System.Threading.Thread(new System.Threading.ThreadStart(rcv));
rcv_th.Start();
}
catch (Exception)
{
nstream.Close();
pck_queue.Clear();
tcpclient.Close();
//connect();
}
}
void rcv()
{
while (tcpclient.Connected)
{
Byte[] databyt = new Byte[tcpclient.Available];
try
{
nstream.Read(databyt, 0, databyt.Length);
}
catch
{
}
string str00 = Encoding.ASCII.GetString(databyt);
string[] str01 = null;
str01 = str00.Split(new char[] { '\n' });
foreach (string str02 in str01)
{
pck_queue.Enqueue(str02);
ReceiveData();
}
}
}
static void send(string Data)
{
Data += "\r\n";
byte[] DataToSend = Encoding.UTF8.GetBytes(Data);
nstream.Write(DataToSend, 0, DataToSend.Length);
}
void ReceiveData()
{
if (pck_queue.Count != 0)
{
for (int i = 0; i < pck_queue.Count; i++)
{
Data = pck_queue.Dequeue();
if (Data != "")
{
string aData "", bData "";
bData = Data.Substring(0, 3);
Console.WriteLine("rcv : " + Data);
switch (bData)
{
case "220": Thread.Sleep(10);
send("USER " + strUser);
break;
case "331": Thread.Sleep(100);
send("PASS " + strPass);
break;
case "230": Thread.Sleep(100);
servWork = true;
send("SYST");
break;
case "530": Thread.Sleep(100);
servWork = false;
break;
case "215": Thread.Sleep(100);
aData = Data.Substring(4, Data.Length - 5);
strSystem = aData;
send("QUIT");
break;
}
}
}
}
}
}
}
Et la fonction appelée simultanément par les Threads :
Private Sub FTPcheck(ByVal cIndex As Integer, ByVal strHost As String, ByVal strUser As String, ByVal strPass As String)
Dim cFTP As ClassFTP = New ClassFTP
cFTP.strHost = strHost
cFTP.strUser = strUser
cFTP.strPass = strPass
Try
cFTP.connect()
fwait(3000) ' millisec
Debug.Print(cFTP.strSystem)
Catch
End Try
End Sub
Je pense que le problème a lieu dans le code ci-dessus.
Je préfère utiliser les sockets pour un protocole personnalisé.