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
XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|