Accueil > > > TRANSFERT DE FICHIER VIA WINSOCK
TRANSFERT DE FICHIER VIA WINSOCK
Information sur la source
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
Sources du même auteur
Sources de la même categorie
Commentaires et avis
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 !
|
Derniers Blogs
TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOURTECHDAYS PARIS 2010 : LA PLEINIèRE DU SECOND JOUR par ROMELARD Fabrice
Après un retour sur l'histoire des TechDays de Paris et le fait que ce soit le plus gros event MS au monde (du fait de sa gratuité), le président de MS France (Eric Boustoullier) a fait une présentation de la vision Microsoft pour les années à venir...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|