Solved

Sending files over Winsock

Posted on 1998-12-03
9
266 Views
Last Modified: 2013-11-13
I know how to send small amounts of text, but what about sending larger files in excess of 10kb? Please provide some source code as I have tried to do it, but utterly failed...I would be wanting to DUPLICATE EXACTLY a file from one PC to another PC.  (They are already connected with winsock etc etc)
0
Comment
Question by:bomax
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 3

Expert Comment

by:a111a111a111
ID: 1448122
Would you go with ftp (small) and its source code?
0
 
LVL 1

Expert Comment

by:chualk
ID: 1448123
Use filecopy (API) then if u are not developing a web application.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1448124
Private Const ftpDIR As Integer = 0

                    Private Const ftpPUT As Integer = 1

                    Private Const ftpGET As Integer = 2

                    Private Const ftpDEL As Integer = 3

                    Private iLastFTP As Integer



                    Private Sub cmdConnect_Click()

                        On Error GoTo ConnectError

                        Inet1.URL = txtURL

                        Inet1.UserName = txtUserName

                        Inet1.Password = txtPassword

                        Inet1.Protocol = icFTP

                        iLastFTP = ftpDIR



                        Inet1.Execute Inet1.URL, "DIR"

                    End Sub



                    Private Sub Inet1_StateChanged(ByVal _

                        State As Integer)

                        Select Case State

                            Case icNone

                             sbFTP.Panels("status").Text = ""

                            Case icResolvingHost

                             sbFTP.Panels("status").Text

                               = "Resolving Host"

                            Case icHostResolved

                             sbFTP.Panels("status").Text _

                               = "Host Resolved"

                            Case icConnecting

                             sbFTP.Panels("status").Text _

                               = "Connecting..."

                            Case icConnected

                             sbFTP.Panels("status").Text _

                               = "Connected!"

                            Case icRequesting

                             sbFTP.Panels("status").Text _

                               = "Requesting..."

                            Case icRequestSent

                             sbFTP.Panels("status").Text _

                               = "Request Sent"

                            Case icReceivingResponse

                             sbFTP.Panels("status").Text _

                               = "Receiving Response..."

                            Case icResponseReceived

                             sbFTP.Panels("status").Text _

                               = "Response Received!"

                            Case icDisconnecting

                             sbFTP.Panels("status").Text _

                               = "Disconnecting..."



                            Case icDisconnected

                             sbFTP.Panels("status").Text _

                               = "Disconnected"

                            Case icError

                             sbFTP.Panels("status").Text _

                               = "Error! " & Trim(CStr( _

                               Inet1.ResponseCode)) & _

                               ": " & Inet1.ResponseInfo

                            Case icResponseCompleted

                             sbFTP.Panels("status").Text _

                               = "Response Completed!"

                                ReactToResponse iLastFTP

                        End Select

                    End Sub



                    Public Function _

                        ReactToResponse(ByVal _

                        iLastCommand As Integer) As Long

                        Select Case iLastCommand

                            Case ftpDIR

                                ShowRemoteFileList

                            Case ftpPUT

                                MsgBox "File Sent from " & CurDir()

                            Case ftpGET

                                MsgBox "File Received "& "in " & CurDir()

                            Case ftpDEL

                        End Select

                    End Function



                    Public Function ShowRemoteFileList() As Long

                        Dim sFileList As String

                        Dim sTemp As String

                        Dim p As Integer

                        sTemp = Inet1.GetChunk(1024)

                        Do While Len(sTemp) > 0

                            DoEvents

                            sFileList = sFileList & sTemp

                            sTemp = Inet1.GetChunk(1024)

                        Loop

                        lstRemoteFiles.Clear

                        Do While sFileList > ""

                            DoEvents

                            p = InStr(sFileList, vbCrLf)

                            If p > 0 Then

                                lstRemoteFiles.AddItem

                                    Left(sFileList, p - 1)

                                If Len(sFileList) > (p + 2) Then

                                    sFileList = Mid(sFileList, p + 2)

                                Else

                                    sFileList = ""

                                End If

                            Else

                                lstRemoteFiles.AddItem sFileList

                                sFileList = ""

                            End If

                        Loop

                    End Function



                    Public Function GetFiles(sFileList As String) As Long

                        Dim sFile As String

                        Dim sTemp As String

                        Dim p As Integer

                        iLastFTP = ftpGET

                        sTemp = sFileList

                        Do While sTemp > ""

                            DoEvents

                            p = InStr(sTemp, "|")

                            If p Then

                                sFile = Left(sTemp, p - 1)

                                sTemp = Mid(sTemp, p + 1)

                            Else

                                sFile = sTemp

                                sTemp = ""

                            End If

                            Inet1.Execute Inet1.URL, "GET " & sFile & _

                                " " & sFile

                        'wait until this execution is done

                        `before going to next file

                            Do

                                DoEvents

                            Loop Until Not _

                                Inet1.StillExecuting

                        Loop

                        iLastFTP = ftpDIR

                        Inet1.Execute Inet1.URL, "DIR"

                    End Function



                    Public Function PutFiles(sFileList As String) As Long

                        Dim sFile As String

                        Dim sTemp As String

                        Dim p As Integer

                        iLastFTP = ftpPUT

                        sTemp = sFileList

                        Do While sTemp > ""

                            DoEvents

                            p = InStr(sTemp, "|")

                            If p Then

                                sFile = Left(sTemp, p - 1)

                                sTemp = Mid(sTemp, p + 1)

                            Else

                                sFile = sTemp

                                sTemp = ""

                            End If

                            Inet1.Execute Inet1.URL, "PUT" & sFile & _

                                " " & sFile

                        'wait until this execution is done

                        `before going to next file

                            Do

                                DoEvents

                            Loop Until Not Inet1.StillExecuting

                        Loop

                        iLastFTP = ftpDIR

                        Inet1.Execute Inet1.URL, "DIR"

                    End Function



                    Private Sub dirLocal_Change()

                        filLocal.Path = dirLocal.Path

                    End Sub



                    Private Sub drvLocal_Change()

                        dirLocal.Path = drvLocal.Drive

                    End Sub



                    Private Sub filLocal_DragDrop(Source _

                            As Control, X As Single, Y As Single)

                        'receiving files from FTP site.

                        Dim I As Integer

                        Dim sFileList As String

                        If TypeOf Source Is ListBox Then

                            For i = 0 _

                                To Source.ListCount - 1

                                If Source.Selected(i) Then

                                    sFileList = _

                                        sFileList & _

                                        Source.List(i) & "|"

                                End If

                            Next

                        End If

                        If Len(sFileList) > 0 Then

                            'strip off the last pipe

                            sFileList = Left(sFileList, _

                                Len(sFileList) - 1)

                            GetFiles sFileList

                        End If

                    End Sub



                    Private Sub _

                        filLocal_MouseDown(Button As _

                        Integer, Shift As Integer, X As _

                        Single, Y As Single)

                        filLocal.Drag vbBeginDrag

                    End Sub



                    Private Sub filLocal_MouseUp(Button _

                        As Integer, Shift As Integer, _

                        X As Single, Y As Single)

                        filLocal.Drag vbEndDrag

                    End Sub



                    Private Sub _

                        lstRemoteFiles_DragDrop(Source _

                        As Control, X As Single, Y As Single)

                        Dim I As Integer

                        Dim sFileList As String

                        If TypeOf Source Is FileListBox Then

                            For i = 0 To Source.ListCount - 1

                                If Source.Selected(i) Then

                                    sFileList = sFileList & _

                                      Source.List(i) & "|"

                                End If

                            Next

                        End If

                        If Len(sFileList) > 0 Then

                            'strip off the last pipe

                            sFileList = Left(sFileList, _

                                Len(sFileList) - 1)

                            PutFiles sFileList

                        End If

                    End Sub



                    Private Sub _

                        lstRemoteFiles_KeyDown(KeyCode _

                        As Integer, Shift As Integer)

                        If KeyCode = vbKeyDelete Then

                            Inet1.Execute Inet1.URL, "DEL " & _

                                lstRemoteFiles.List( _

                                lstRemoteFiles.ListIndex)

                            Do

                                DoEvents

                            Loop While Inet1.StillExecuting

                        End If

                        iLastFTP = ftpDIR

                        Inet1.Execute Inet1.URL, "DIR"

                    End Sub



                    Private Sub _

                        lstRemoteFiles_MouseDown(Button _

                        As Integer, Shift As Integer, )

                        X As Single, Y As Single)

                        lstRemoteFiles.Drag vbBeginDrag

                    End Sub



                    Private Sub lstRemoteFiles_MouseUp(Button As _

                        Integer, Shift As Integer, _

                        X As Single, Y As Single)

                        lstRemoteFiles.Drag vbEndDrag

                    End Sub
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1448125
FILE: VBFTP.EXE: Implementing FTP Using WinInet API from VB

Last reviewed: October 24, 1997
Article ID: Q175179 The information in this article applies to:

Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 4.0, 5.0







SUMMARY

VBFTP.EXE is a sample that implements FTP connection, download, and upload using the WinInet FTP API from Visual Basic. It demonstrates the APIs and techniques to set FTP connection attributes and transfer mode that are not exposed inthe MS Internet Transfer Control.

The sample requires Visual Basic SP2 or Visual Studio SP2 installed to run because it uses the updated comctl32.ocx from those Service Packs.

When adding FTP functionality to your application, it is important to understand the capability and limitation of each of the different Internet technologies. At the lowest level, you could use the Microsoft Winsock Control to send commands directly to FTP server port 21. The sequence and syntax of the commands you send to the server would have to follow the specification of the FTP protocol. The WinInet FTP API wraps the socket code and most low-level FTP commands and provides a set of much simpler task-oriented APIs that do not require detailed knowledge of FTP protocol. However, there is a chance that some FTP servers use FTP commands not implemented by WinInet. If this happens, you have to use the Microsoft Winsock Control to communicate with the server directly at the protocol level. Test your FTP server with the VBFTPJR sample before you decide whether to choose WinInet API or the Winsock Control. The Microsoft Internet Transfer Control, on the other hand, offers a more simplified interface than WinInet but offers less flexibility and cannot be used if you want to customize the connection and transfer mode.

If you are using a proxy to access an FTP server, the proxy has to be capable of handling FTP commands and cannot be a CERN proxy. Please see the following article in the Microsoft Knowledge Base for more information:



   ARTICLE-ID: Q166961
   TITLE     : HOWTO: FTP with CERN-Based Proxy Using WinInet API




The following file is available for download from the Microsoft Software Library:



 ~ vbftp.exe (size: 47597 bytes)




For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:



   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services



Keywords          : VB4WIN vb5all kbsample
Technology        : kbInetDev
Version           : WINDOWS:4.0,5.0
Platform          : WINDOWS
Issue type        : kbhowto
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:bomax
ID: 1448126
Lots of ideas! Hmmm....most of them seem to use the INet control, and not winsock...Is there a way to do it with winsock? If not, I'll award the pts to whoever duz it with Inet(which I will be checking after this...)
0
 

Author Comment

by:bomax
ID: 1448127
Nah....I can't figure out how to use the damned Inet...maybe just implement the code for simply sending a file...I dont need a fully pledged FTP client
0
 
LVL 1

Accepted Solution

by:
kacklehorn earned 100 total points
ID: 1448128
It's easy to use the winsock control to do this bro.. Heres a sample. If you want me to e-mail the actual sample, gimme your email.

Here's the client..it has a timer , a winsock control, a commondialog control, two textboxes, and three buttons.

***************************begin client ***********************
Option Explicit
Private Sub Command1_Click()
    If cliSock.State <> 0 Then
        cliSock.Close
    End If
    Unload Me
End Sub

Private Sub Command2_Click()
cliSock.Connect Text1.Text, Text2.Text
End Sub

Private Sub Command3_Click()
Dim filename As String
On Error GoTo errhan
    'open a file, and send it to the client as binary
    CommonDialog1.ShowOpen
    filename = CommonDialog1.filename
    Dim str() As Byte
    Open filename For Binary As #1
    ReDim str(FileLen(filename))
    Get #1, , str
    Close #1
    cliSock.SendData str
    Exit Sub
errhan:
msgbox "error"
End Sub



Private Sub Timer1_Timer()
  Select Case cliSock.State
Case 0
    Me.Caption = "Closed"
Case 1
    Me.Caption = "Open"
Case 2
    Me.Caption = "Listening"
Case 3
    Me.Caption = "Connection pending"
Case 4
    Me.Caption = "Resolving host"
Case 5
    Me.Caption = "Host resolved"
Case 6
    Me.Caption = "Connecting"
Case 7
    Me.Caption = "Connected"
Case 8
    Me.Caption = "Peer is closing the connection"
Case 9
    Me.Caption = "Error"
End Select
End Sub


********************end client *******************************

Here's a server:
It's got a winsock control, a textbox, a timer, and two command buttons.

******************start server********************************
Option Explicit
Dim x As Long       'Byte counter variable
Dim str() As Byte   'Byte variable

Private Sub Command1_Click()
   'Check the connection state before closing
    If svrsock.State <> 0 Then
        svrsock.Close
    End If
    Unload Form1
    Unload Me
End Sub


Private Sub Command2_Click()
svrsock.LocalPort = Text1.Text 'Set the local port to listen on
svrsock.Listen
End Sub

Private Sub Form_Load()
'Command1.Top = Me.ScaleHeight - Command1.Height
x = 1
End Sub

Private Sub Form_Resize()
'Command1.Top = Me.ScaleHeight - Command1.Height
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Unload Form1

End Sub

Private Sub svrSock_ConnectionRequest(ByVal requestID As Long)
svrsock.Close             'Close the socket
svrsock.Accept requestID  'Accept incoming call
End Sub

Private Sub svrSock_DataArrival(ByVal bytesTotal As Long)
       On Error GoTo errhan
        svrsock.GetData str, vbArray + vbByte        'get binary data
        Open App.Path & "\tempfile" For Binary As #1 'open a tempfile for storage
        Put #1, x, str                               'Start creating file at first Byte
        Close #1                                     'Close the file
        x = (x + 1) + UBound(str) ' This counter is to save the last byte stroed in the file and resume
        Command1.Caption = x
        Exit Sub
errhan:
       MsgBox Err.Number & Err.Description
End Sub

Private Sub Timer1_Timer()
  Select Case svrsock.State
Case 0
    Me.Caption = "Closed"
Case 1
    Me.Caption = "Open"
Case 2
    Me.Caption = "Listening"
Case 3
    Me.Caption = "Connection pending"
Case 4
    Me.Caption = "Resolving host"
Case 5
    Me.Caption = "Host resolved"
Case 6
    Me.Caption = "Connecting"
Case 7
    Me.Caption = "Connected"
Case 8
    Me.Caption = "Peer is closing the connection"
    svrsock.Close
    svrsock.Listen
Case 9
    Me.Caption = "Error"
End Select
End Sub




*******************end server********************************

good luck.

KaCkLeHoRn..master of winsock..hehehe
0
 
LVL 1

Expert Comment

by:kacklehorn
ID: 1448129
P.S. If you wanna see some more cool winsock stuff, go to my web site. http://members.tripod.com/~Kacklehorn. Theres a OOB tester code out there, and a winsock mailer, to send email.

L8ta
KaCkLeHoRn
0
 

Author Comment

by:bomax
ID: 1448130
please send me the sample as well.....bomax@bigfoot.com will do..  THANK YOU!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now