| Usuario |
Necesito alguien que sepa como tra... |
Desconocido
|
| Enviado - 3/8/2004 |
| |
Necesito alguien que sepa como transferir archivos con el winsock, o de otra forma, si me pudieran ayudar estaria muy agradecido,gracias
|
| |
|
rob_master
9 Mensaje(s) |
| Enviado - 3/8/2004 |
| |
| con el comando sendata del winsock puedes hacer ese tipo de tranferencia |
| |
|
Fenris
138 Mensaje(s)
|
| Enviado - 4/8/2004 |
| |
Esto use yo y funciona de pelos
suerte
cliente:
Option Explicit
'
' ---------------------------------------------------------------------------------
' File...........: fClient.frm
' Author.........: Will Barden
' Created........: 09/06/03
' Modified.......: 11/06/03
' Version........: 1.1
' Website........: http://www.WinsockVB.com
' Contact........: [email protected]
'
' Sample client to the file transfer server (fServer.frm). This shows how to
' handle the very simple protocol between the client/server, and how to save
' the pieces of data as one consecutive file, in binary mode.
' ---------------------------------------------------------------------------------
'
' ---------------------------------------------------------------------------------
' Private variables.
' ---------------------------------------------------------------------------------
'
Private m_blnTransferring As Boolean
Private m_strFilename As String
'
' ---------------------------------------------------------------------------------
' Control events.
' ---------------------------------------------------------------------------------
'
Private Sub cmdConnect_Click()
'
' Connect to the localhost on port 10101.
Call lstEvents.AddItem("Connecting to 127.0.0.1")
With Winsock
Call .Close
.RemoteHost = "127.0.0.1"
.RemotePort = 10101
Call .Connect
End With
'
End Sub
'
Private Sub cmdClose_Click()
'
Call Unload(Me)
'
End Sub
'
' ---------------------------------------------------------------------------------
' Winsock events.
' ---------------------------------------------------------------------------------
'
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
'
Dim strData As String
Dim hFile As Long
'
' Grab the incoming data.
Call Winsock.GetData(strData)
'
' If we're not in a file transfer operation, this could well be a "BOF".
If (Not m_blnTransferring) Then
'
If (Mid$(strData, 1, 3) = "BOF") Then
'
' We have an incoming file - save the filename, and set the operation
' flag so that next time data arrives (file data) it will be saved.
Call lstEvents.AddItem("Received BOF - incoming file:" & Mid$(strData, 4))
m_blnTransferring = True
m_strFilename = Mid$(strData, 4)
'
' Send back a "NEXT" to get some of the file data.
Call lstEvents.AddItem("Requesting first piece")
Call Winsock.SendData("NEXT")
DoEvents
'
End If
'
Else
'
' If we're already in a transfer, this data could either be file data, or
' an EOF marker.
If (Mid$(strData, 1, 3) = "EOF") Then
'
' The transfer is complete.
m_blnTransferring = False
Call lstEvents.AddItem("Received EOF, transfer complete")
DoEvents
'
Else
'
' Open a temporary file in the current directory - this is where all the
' file data is saved.
hFile = FreeFile
Open App.Path & "\temp.bmp" For Binary As #hFile
'
' Move to the end of the file, and write the data.
Seek #hFile, LOF(hFile) + 1
Put #hFile, , strData
'
Close #hFile
'
' Send the server another "NEXT" command to get the next piece of data.
Call lstEvents.AddItem("Data arrived, requesting next piece")
Call Winsock.SendData("NEXT")
DoEvents
'
End If
'
End If
'
End Sub
'
Private Sub Winsock_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)
'
' An error has occurred. Log the event and shutdown the connection.
Call lstEvents.AddItem(Description)
Call lstEvents.AddItem("Shutting down")
'
Call Winsock.Close
m_blnTransferring = False
'
End Sub
'
' ---------------------------------------------------------------------------------
' EOF.
' ---------------------------------------------------------------------------------
'
server:
Option Explicit
'
' ---------------------------------------------------------------------------------
' File...........: fServer.frm
' Author.........: Will Barden
' Created........: 09/06/03
' Modified.......: 11/06/03
' Version........: 1.1
' Website........: http://www.WinsockVB.com
' Contact........: [email protected]
'
' A very simple and primitive file sender, this project will demonstrate
' binary file techniques, handling the binary data, and sending a file in
' chunks of xKB at a time.
'
' Three commands are used during the transfer process:
'
' BOF (Beginning Of File) - This is used to indicate to the remote host that a
' file is on it's way.
'
' NEXT (Next chunk please) - A host receiving a file will send this each time
' it wants the next piece of the file.
'
' EOF (End Of File) - This is send to the remote host once all the file data
' has been transmitted. The remote host can then let the user know the transfer
' is complete.
'
' The general sequence is this:
' 1. Both hosts connect. The server is going to send the client a file.
' 2. Server: Send "BOF[filename]"
' 3. Client: Send "NEXT"
' 4. Server: If there's more data, send it.
' If there's no more data, send "EOF".
' 5. Client: If data has arrived, send "NEXT".
' If "EOF" has arrived, transfer is complete.
'
' ------------------------------------------------------------------------------
'
' ------------------------------------------------------------------------------
' Constants.
' ------------------------------------------------------------------------------
'
Private Const CHUNK_SIZE As Long = 3072 ' 3KB.
'
' ------------------------------------------------------------------------------
' Private variables.
' ------------------------------------------------------------------------------
'
Private blnTransferring As Boolean
Private lngFilePos As Long
Private strFilename As String
'
' ------------------------------------------------------------------------------
' Form events.
' ------------------------------------------------------------------------------
'
Private Sub Form_Load()
'
' Show the client form, and start listening.
Call StartServer
'
End Sub
'
' ------------------------------------------------------------------------------
' Control events.
' ------------------------------------------------------------------------------
'
Private Sub cmdClose_Click()
'
Call Unload(Me)
'
End Sub
'
Private Sub cmdSendFile_Click()
'
' Start the file sending procedure.
Call lstEvents.AddItem("Initializing transfer")
Call SendFile(App.Path & "\..\logo.bmp")
'
End Sub
'
' ------------------------------------------------------------------------------
' Private helpers.
' ------------------------------------------------------------------------------
'
Private Sub StartServer()
'
' Set the Winsock up to listen on port 10101.
Call lstEvents.AddItem("Server started, listening on 10101")
With Winsock
Call .Close
.LocalPort = 10101
Call .Listen
End With
'
End Sub
'
Private Sub SendFile(ByVal strFile As String)
'
' Store the filename for later.
strFilename = strFile
'
Call lstEvents.AddItem("Sending BOF: " & strFilename)
'
' Set the transferring flag (since we're now connected), and send the BOF
' marker - this instructs the remote host that a file is on it's way.
blnTransferring = True
Call Winsock.SendData("BOF" & strFilename)
DoEvents
'
End Sub
'
Private Sub SendNextChunk()
'
Dim hFile As Long
Dim lngChunkSize As Long
Dim strData As String
'
' If we're not currently connected and transferring, exit. This shouldn't
' happen, but just in case...
If (Not blnTransferring) Then Exit Sub
'
' Open the file that we're sending, for BINARY mode. This means that we can
' read out bits regardless of what's in the file (text, image, music etc..).
hFile = FreeFile
Open strFilename For Binary As #hFile
'
' We need to read the next unsent piece of the file, so move through the
' file past all the bits we've already sent. Then, when we read into
' strData, the data will be the next consecutive bytes.
If (lngFilePos = 0) Then lngFilePos = 1
Seek hFile, lngFilePos
'
' Work out how large this chunk is going to be. Normally, it will be the
' standard CHUNK_SIZE, but if the last piece is less than that, decrease it.
lngChunkSize = LOF(hFile) + 1 - lngFilePos
If (lngChunkSize > CHUNK_SIZE) Then lngChunkSize = CHUNK_SIZE
'
' If the chunksize was 0, it means there's no data left to send, so our
' transfer is complete.
If (lngChunkSize = 0) Then
'
' Send the EOF marker so the remote host knows that's all the data.
strData = "EOF"
blnTransferring = False
Call lstEvents.AddItem("0 bytes, transfer completed. Sending EOF.")
'
' Send the data to the remote host.
Call Winsock.SendData(strData)
DoEvents
'
Else
'
' Grab the data from the file, and increment our file pointer so that next
' time we read, we are reading the next piece of the file.
strData = String$(lngChunkSize, 0)
Get #hFile, , strData
lngFilePos = lngFilePos + lngChunkSize
'
' Send the data to the remote host.
Call Winsock.SendData(strData)
Call lstEvents.AddItem("Sent " & lngChunkSize & " bytes")
DoEvents
'
End If
'
Close #hFile
'
End Sub
'
' ------------------------------------------------------------------------------
' Winsock events.
' ------------------------------------------------------------------------------
'
Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
'
' Accept the incoming connection from the client.
With Winsock
Call .Close
Call .Accept(requestID)
End With
Call lstEvents.AddItem("Incoming connection request, accepted")
'
cmdSendFile.Enabled = True
'
End Sub
'
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
'
Dim strData As String
'
' Grab the incoming data and check it.
Call Winsock.GetData(strData)
If (strData = "NEXT") Then
'
' The remote host has asked for the NEXT chunk.. so send it to them!
Call lstEvents.AddItem("Sending next chunk.")
Call SendNextChunk
'
Else
'
' Data has arrived and we're not sure what it is. This shouldn't happen,
' but for debugging purposes, display it.
Call MsgBox(strData)
'
End If
'
End Sub
'
Private Sub Winsock_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)
'
' An error has occurred somewhere. Log the event, and shutdown the connection.
Call lstEvents.AddItem(Description)
Call lstEvents.AddItem("Shutting down")
'
Call Winsock.Close
blnTransferring = False
'
End Sub
'
' ------------------------------------------------------------------------------
' EOF.
' ------------------------------------------------------------------------------
'
|
| |
|
|
|