begin process at 2010 02 09 20:09:17
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Réseau & Internet

 > TRANSFERT DE FICHIER VIA WINSOCK

TRANSFERT DE FICHIER VIA WINSOCK


 Information sur la source

Note :
9 / 10 - par 1 personne
9,00 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Réseau & Internet Classé sous :filetransfert, winsock, filetransfer, client, server Niveau :Débutant Date de création :05/04/2006 Vu / téléchargé :6 642 / 1 795

Auteur : thel0rd

Ecrire un message privé
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

Salut, Ce code vous montre la maniere de faire un transfert de fichier via le composant Winsock. Evidemment ce n'est pas la seule maniere, il y en a d'autres.

Source

  • 'Simple File transfert using Winsock.ocx, if you dont want of this dependancy you can use csocket @vbip.com
  • 'Greetz: undergroundkonnekt team
  • 'this is an exemple, if you use it, give the credit to its author.
  • Option Explicit
  • Dim BlnTflag As Boolean 'Flag de transfert, Transfert Flag
  • Dim LngCursor As Long 'Pointeur de position dans fichier source, source file position pointer
  • ''''''''''''''''CLIENT'''''''''''''''''
  • '''''''''''''''''''''''''''''''''''''''
  • '''''''''''''''''''''''''''''''''''''''
  • Private Sub CmdConnect_Click()
  • WskC.Connect TxtIp.Text, TxtCPort.Text 'Connection
  • End Sub
  • Private Sub CmdSend_Click()
  • On Error GoTo actcancel
  • If BlnTflag = False Then 'si on est pas dans un transfert, if we're not transferring
  • LngCursor = 0 'reinitialisation du pointeur, pointer reinitialisation
  • If WskC.State <> 7 Then
  • 'Call ErrorHandler(2)
  • Else
  • DlgSend.ShowOpen 'common dialog
  • WskC.SendData "Transfert" & "|" & DlgSend.FileTitle & "|" & FileLen(DlgSend.FileName) 'on envoie le nom du fichier, we send the file name
  • End If
  • Else
  • Exit Sub
  • End If
  • actcancel: Exit Sub
  • End Sub
  • Private Sub WskC_Close()
  • Me.Caption = "Disconnected"
  • CmdConnect.Caption = "Connect"
  • End Sub
  • Private Sub WskC_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
  • Me.Caption = "Disconnected"
  • CmdConnect.Caption = "Connect"
  • End Sub
  • Private Sub WskC_Connect()
  • Me.Caption = "Connected"
  • CmdConnect.Caption = "Disconnect"
  • End Sub
  • Private Sub WskC_DataArrival(ByVal bytesTotal As Long)
  • Dim strdata As String 'datas reçues, received datas
  • Dim strBuffer As String 'Buffer
  • WskC.GetData strdata 'get data
  • 'si il reste moins de 2048 octets
  • 'if there are more than 2048 left
  • If FileLen(DlgSend.FileName) - LngCursor < 2048 Then
  • 'on ajuste la taille du buffer
  • 'Buffer size adjustment
  • strBuffer = Space(FileLen(DlgSend.FileName) - LngCursor)
  • 'Si il reste plus de 2048 octets
  • 'if there are more than 2048 bytes left
  • ElseIf FileLen(DlgSend.FileName) - LngCursor > 2048 Then
  • 'buffer = 2048
  • strBuffer = Space(2048)
  • End If
  • 'Si le pointeur est egal à la taille fichier source
  • 'if pointer value = source file size
  • If FileLen(DlgSend.FileName) = LngCursor Then
  • 'on a fini le transfert, on ferme le fichier on le dit au server
  • 'we have finished the transfert, we close the opened file, and we tell the server the job is done
  • LblStatut.Caption = "Statut: " & DlgSend.FileName & " successfully uploaded"
  • WskC.SendData "E"
  • Close #1
  • Exit Sub
  • End If
  • 'Le server nous demande de commencer le transfert
  • 'the server ask for the transfert beginning
  • If Left(strdata, 1) = "S" Then
  • LblStatut.Caption = "Statut: Uploading " & DlgSend.FileName
  • 'on ouvre le fichier en binaire
  • 'we open the file in binary mode
  • Open DlgSend.FileName For Binary As #1
  • Get #1, , strBuffer 'on prend un bout de code equivalent au buffer, we get some datas according to the buffer size
  • WskC.SendData strBuffer 'on l'envoie, we send it
  • LngCursor = Len(strBuffer) 'on update le pointeur au debut du transfert il est de la taille du buffer(2048), we update the pointer, at the beginning of a transfert it's 2048
  • 'Le server demande un autre bout de fichier
  • 'The server ask for another file chunk
  • ElseIf Left(strdata, 1) = "N" Then
  • Get #1, LngCursor + 1, strBuffer 'on reprend l'equivalent du buffer, we take some datas (=buffersize)
  • WskC.SendData strBuffer 'on envoie, we send
  • LngCursor = LngCursor + Len(strBuffer) 'on update le pointeur, we update the pointer
  • End If
  • End Sub
  • '''''''''''SERVER''''''''''''
  • '''''''''''''''''''''''''''''
  • '''''''''''''''''''''''''''''
  • Private Sub CmdListen_Click()
  • On Error Resume Next
  • If TxtSPort.Text <> "" Then
  • WskS.LocalPort = TxtSPort.Text
  • WskS.Close
  • WskS.Listen 'Listening
  • Else
  • 'Call ErrorHandler(1)
  • End If
  • End Sub
  • Private Sub WskS_ConnectionRequest(ByVal requestID As Long)
  • WskS.Close
  • WskS.Accept requestID
  • End Sub
  • Private Sub WskS_DataArrival(ByVal bytesTotal As Long)
  • Dim strdata As String
  • Dim StrSplited() As String
  • Dim StrFilename As String
  • Dim LngFileSize As Long
  • WskS.GetData strdata 'getdata
  • StrSplited = Split(strdata, "|") 'On split les datas, we split data (delimiter = |)
  • If strdata = "E" Then 'si c'est la fin d'un transfert, if it's the end of a transfert
  • Close #2 'on ferme le fichier destination, we close the dest file
  • BlnTflag = False 'we update the flag
  • Exit Sub
  • End If
  • If BlnTflag = False Then 'si on est pas dans un transfert, if we're not in a file transfert
  • 'on l'initialise en updatant le flag, en récuperant le nom du fichier , on ouvre un fichier vierge en binaire, puis en demande un bout de fichier
  • 'transfert initialisation: flag update, file name get, we open a free file, then we ask for the first chunk
  • If StrSplited(0) = "Transfert" Then
  • StrFilename = StrSplited(1)
  • BlnTflag = True
  • WskS.SendData "S"
  • If Dir(App.Path & "\" & StrFilename) <> "" Then 'on efface le fichier si il est déja present, we erase the file if it exists
  • Kill (App.Path & "\" & StrFilename)
  • End If
  • Open App.Path & "\" & StrFilename For Binary As #2
  • End If
  • Else
  • Put #2, LOF(2) + 1, strdata 'on ecrit les bout de donner en fin de fichier, we write data at the end of the file
  • WskS.SendData "N" 'we ask for another chunk
  • End If
  • End Sub
'Simple File transfert using Winsock.ocx, if you dont want of this dependancy you can use csocket @vbip.com
'Greetz: undergroundkonnekt team
'this is an exemple, if you use it, give the credit to its author.

Option Explicit

Dim BlnTflag As Boolean 'Flag de transfert, Transfert Flag
Dim LngCursor As Long 'Pointeur de position dans fichier source, source file position pointer


''''''''''''''''CLIENT'''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''
Private Sub CmdConnect_Click()
WskC.Connect TxtIp.Text, TxtCPort.Text 'Connection

End Sub

Private Sub CmdSend_Click()
On Error GoTo actcancel

If BlnTflag = False Then 'si on est pas dans un transfert, if we're not transferring
    LngCursor = 0 'reinitialisation du pointeur, pointer reinitialisation
    If WskC.State <> 7 Then
        'Call ErrorHandler(2)
    Else
        DlgSend.ShowOpen 'common dialog
        WskC.SendData "Transfert" & "|" & DlgSend.FileTitle & "|" & FileLen(DlgSend.FileName) 'on envoie le nom du fichier, we send the file name
    End If
Else
    Exit Sub
End If

actcancel: Exit Sub
End Sub

Private Sub WskC_Close()
Me.Caption = "Disconnected"
CmdConnect.Caption = "Connect"
End Sub

Private Sub WskC_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Me.Caption = "Disconnected"
CmdConnect.Caption = "Connect"
End Sub

Private Sub WskC_Connect()

Me.Caption = "Connected"
CmdConnect.Caption = "Disconnect"
End Sub


Private Sub WskC_DataArrival(ByVal bytesTotal As Long)
Dim strdata As String 'datas reçues, received datas
Dim strBuffer As String 'Buffer

WskC.GetData strdata 'get data

'si il reste moins de 2048 octets
'if there are more than 2048 left
If FileLen(DlgSend.FileName) - LngCursor < 2048 Then
    'on ajuste la taille du buffer
    'Buffer size adjustment
    strBuffer = Space(FileLen(DlgSend.FileName) - LngCursor)
'Si il reste plus de 2048 octets
'if there are more than 2048 bytes left
ElseIf FileLen(DlgSend.FileName) - LngCursor > 2048 Then
    'buffer = 2048
    strBuffer = Space(2048)
End If

'Si le pointeur est egal à la taille fichier source
'if pointer value = source file size
If FileLen(DlgSend.FileName) = LngCursor Then
    'on a fini le transfert, on ferme le fichier on le dit au server
    'we have finished the transfert, we close the opened file, and we tell the server the job is done
    LblStatut.Caption = "Statut: " & DlgSend.FileName & " successfully uploaded"
    WskC.SendData "E"
    Close #1
    Exit Sub
End If

'Le server nous demande de commencer le transfert
'the server ask for the transfert beginning
If Left(strdata, 1) = "S" Then
    LblStatut.Caption = "Statut: Uploading " & DlgSend.FileName
    'on ouvre le fichier en binaire
    'we open the file in binary mode
    Open DlgSend.FileName For Binary As #1
        Get #1, , strBuffer 'on prend un bout de code equivalent au buffer, we get some datas according to the buffer size
    WskC.SendData strBuffer 'on l'envoie, we send it
    LngCursor = Len(strBuffer) 'on update le pointeur au debut du transfert il est de la taille du buffer(2048), we update the pointer, at the beginning of a transfert it's 2048
'Le server demande un autre bout de fichier
'The server ask for another file chunk
ElseIf Left(strdata, 1) = "N" Then
        Get #1, LngCursor + 1, strBuffer 'on reprend l'equivalent du buffer, we take some datas (=buffersize)
    WskC.SendData strBuffer 'on envoie, we send
    LngCursor = LngCursor + Len(strBuffer) 'on update le pointeur, we update the pointer
End If
End Sub

 
'''''''''''SERVER''''''''''''
'''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''
Private Sub CmdListen_Click()
On Error Resume Next
If TxtSPort.Text <> "" Then
    WskS.LocalPort = TxtSPort.Text
    WskS.Close
    WskS.Listen 'Listening
Else
    'Call ErrorHandler(1)
End If
End Sub


Private Sub WskS_ConnectionRequest(ByVal requestID As Long)

WskS.Close
WskS.Accept requestID
End Sub

Private Sub WskS_DataArrival(ByVal bytesTotal As Long)
Dim strdata As String
Dim StrSplited() As String
Dim StrFilename As String
Dim LngFileSize As Long

WskS.GetData strdata 'getdata
StrSplited = Split(strdata, "|") 'On split les datas, we split data (delimiter = |)

If strdata = "E" Then 'si c'est la fin d'un transfert, if it's the end of a transfert
    Close #2 'on ferme le fichier destination, we close the dest file
    BlnTflag = False 'we update the flag
    Exit Sub
End If

If BlnTflag = False Then 'si on est pas dans un transfert, if we're not in a file transfert
'on l'initialise en updatant le flag, en récuperant le nom du fichier , on ouvre un fichier vierge en binaire, puis en demande un bout de fichier
'transfert initialisation: flag update, file name get, we open a free file, then we ask for the first chunk
    If StrSplited(0) = "Transfert" Then
        StrFilename = StrSplited(1)
        BlnTflag = True
        WskS.SendData "S"
        If Dir(App.Path & "\" & StrFilename) <> "" Then 'on efface le fichier si il est déja present, we erase the file if it exists
            Kill (App.Path & "\" & StrFilename)
        End If
        
        Open App.Path & "\" & StrFilename For Binary As #2
    End If
Else
    Put #2, LOF(2) + 1, strdata 'on ecrit les bout de donner en fin de fichier, we write data at the end of the file
    WskS.SendData "N" 'we ask for another chunk
End If
End Sub


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip APPEL DYNAMIQUE D'APIS (NO-DECLARATION)
Source avec Zip MULTI FILE BINDER
Source avec Zip MULTI REVERSE CONNECTION
Source avec Zip EXEMPLE D'APPLICATION MULTITHREAD

 Sources de la même categorie

Source avec Zip Source avec une capture UPLOAD FTP VB 6 par Onin42
Source avec Zip Source avec une capture Source .NET (Dotnet) FAVORIS URL par Le Pivert
Source avec Zip Source avec une capture Source .NET (Dotnet) DEVSSH CLIENT SFTP/SSH par iblsysteme
Source avec Zip Source avec une capture CONNEXION RÉSEAU par ABUMAI
Source avec Zip CSOCKET - REMPLACEZ WINSOCK PAR LA VERSION 2 DES API par Renfield

 Sources en rapport avec celle ci

Source avec Zip SLYSPY(MINI-RAT) par ageryder
Source avec Zip Source avec une capture PETIT EXEMPLE DE CHAT AVEC WINSOCK par robapt
Source avec une capture SERVEUR/MULTI CLIENTS - WINSOCK REMOTE CONTROL (EXPLORATEUR,... par djine
Source avec Zip MULTI REVERSE CONNECTION par thel0rd
Source avec Zip UTILISATION PRATIQUE DE L'OCX WINSOCK (UN PETIT SYSTEME DE M... par Francki

Commentaires et avis

Commentaire de bts_informatique le 05/04/2006 18:02:45

salut/
merci pour ce code c le meme code que je cherche mais j'ai un probleme le voila:
j'ai crée un connection avec winsock entre multiclient et un serveur et j'ai besion d'un code qui peut prissisé le client indiqué par sont adresse IP puis le transmetre le fichier svp si vousavez une idée envoyé la a moi
merci davance
e-mail taoufiq_nejjar@hotmail.com

Commentaire de Le newbie le 15/11/2006 16:35:58

Salut

Je suis dans le même cas si vous avez des idées pour transférer un fichier à une ip donnée je suis preneur.

Merci

Commentaire de ghuysmans99 le 21/07/2007 14:27:21

"pointer reset" et pas "pointer reinitialisation"

Commentaire de tdt63 le 12/01/2009 11:22:17

Slt, cette source marche bien, mais que ce soit avec elles ou d'autres, le débit reste très inférieur aux solutions commerciales. Par exemple, lorsque j'envoie un fichier depuis chez moi, je ne dépasse jamais les 50 Ko/sec avec toutes les sources de Transfert que j'ai trouvées, alors qu'avec un programme comme FileZilla Server, j'atteins les 95-100 Ko/sec. Quelqu'un sait-il à quoi c'est dû, peut-être au contrôle Winsock ?

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

TRES IMPORTANT !! [ par Lead ] Avis aux utilisateur de winsock.Voila mon probleme :- Application client/serveur Démarrage : Server en écoute et client se connecte au server.Fermetur Peut-on avoir plusieurs connections sur un winsock? [ par crendel ] Je suis en train de faire un logiciel qui marche sur le principeclient/server. J'aimerai mettre plusieurs client connecté au meme server. C'est possib Prob avec un projet Winsock help plz [ par YnOThAr ] Lut all !!! Donc voila, je ne suis encore qu'un débutant en Visual Basic, mais comme apparementc'est un "langage" simple et comme je fais deja un peu URGENT ! Winsock [ par idealprog ] salut,j ai cre 2 prog avec winsock, 1 server et 1 client :lorsque je lance le server puis le client et que je me connecte au server ( a partir du clie Winsock [ par Teradonis ] J'ai fait un server et un client en reseau local. J'arrive à me conecter mais si C le server ki deconnecte, le client n'arrive plus à se connecter!! E server/client winsock [ par idealprog ] salut,je veu réaliser 2 prog du type server et client par winsock, le but etant juste de lance le server sur un pc et connecter le client au server pa Petit probleme avec winsock [ par stumpy ] Voilà je suis en train de programmer un petit chat (classique je sais :p) mais par la suite je voudrais essayer (si mon emploi du temps me le permet) __..-- URGENT --..__ PB AVEC WINSOCK [ par willwilly ] lut tlm,g un pb avec winsock, je m'explik:g modifier plusieurs sources, puis remit en fr...me il n'envoi po le msg demander !C'est un chat multi-user. client/serveur [ par deubal ] salut,salutdans mon appli je souhaite faire une liaison client/serveur.j'ai donc un winsock client (wkcli) et un winsock serveur (wkserv). Or la conne Je chercher un connaiseur de winsock [ par Sibelle07 ] Salut le forum ,Je cherche , un programmeur qui connait hyper bien le winsock , j'ai des questions a posé , et puis d'ailleur je vais vous les posés !


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,796 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales