CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2001
    Location
    California
    Posts
    20

    winsock binary file transfer

    Can anybody give me the necessary code to send a graphic file (Binary) and receive the same into a local intranet ??? Help please thanks


  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: winsock binary file transfer

    Send binary file

    Private Sub cmdSend_Click()
    On Error Resume Next 'prevents error
    If txtFile.Text = "" Then Exit Sub 'wont let someone send something not there
    strFileName = "" 'resets the filename
    blnSend = False 'sets it to false
    strSize = "" 'resets the size

    Open txtFile.Text For Binary As #1 'opens the file u want to send so it can be read
    strLOF = LOF(1) 'saves length of file into memory
    Winsock.SendData "Name" & CD.FileTitle & ":" & strLOF 'sends server the name of the file and its length
    DoEvents 'needed =D

    Do While blnSend = False 'keeps looping until the server accepts the request
    DoEvents 'lets u do stuff while in loop
    Loop 'goes back to Do WHile statement

    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: winsock binary file transfer

    Receive

    Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    On Error Resume Next 'prevents error
    Dim strData As String 'holds data for select case
    Dim strData2 As String 'holds data
    Call Winsock.GetData(strData, vbString) 'gets the data sent by the server
    strData2 = Mid(strData, 5) 'gets data
    strData = Left(strData, 4) 'gets data for select case
    Select Case strData 'goes to the right case depending on strData
    Case "File" 'a file transfer is in progress
    Put 1, , strData2 'puts data into file
    PBar.Value = PBar.Value + bytesTotal 'shows how much is done so far
    strSoFar = strSoFar + bytesTotal 'calculates KBps
    Winsock.SendData "OKOK keep sending!" 'tells them ur done with the data and u want some more!
    DoEvents ' =D
    Case "Stop" 'the file exchange has ended
    Close #1 'closes the file
    'resets the progressbar
    PBar.Value = 0
    PBar.Max = 1
    '=====================
    Me.Enabled = True 'reenables the form!
    Case "Nope" 'tells u that they declined ur request to give em a file
    MsgBox strFriend & " declined your file transfer request.", vbInformation, "File Transfer Canceled!" '<=- easy to get again
    Close #1 'closes the file
    'stops the loops that was waiting for the boolean value to be true
    Do
    DoEvents
    Loop
    '==========================
    Case "OKOK" 'tells u they want more of the file
    blnSend = True 'tells u to keep sending
    Me.Enabled = False 'keeps form disabled
    PBar.Max = strLOF 'sets progressbar max to filesize
    If Not EOF(1) Then 'does this if not the end of the file
    If strLOF - Loc(1) < 2040 Then 'if you are at the last chunk of data
    strBlock = Space$(strLOF - Loc(1)) 'sets the block size to the size of the data (cause its less!)
    Get 1, , strBlock 'gets data
    Winsock.SendData "File" & strBlock 'sends data
    DoEvents ' =/
    PBar.Value = PBar.Value + Len(strBlock) 'sets progressbar
    strSoFar = strSoFar + (strLOF - Loc(1)) 'sets KBps
    Winsock.SendData "Stop the maddness!" 'tells server THE TRANSFER IS ENDED!
    Close #1 'closes file
    'resets the progressbar
    PBar.Max = 1
    PBar.Value = 0
    '====================
    Me.Enabled = True 'reenables the form
    Else 'if not the last chunk
    strBlock = Space$(2040) 'sets block up to receive only 2040 bytes of data
    End If
    strSoFar = strSoFar + 2040 'calculates KBps
    Get 1, , strBlock 'gets data
    Winsock.SendData "File" & strBlock 'sends data
    DoEvents 'hmmmm once again
    PBar.Value = PBar.Value + Len(strBlock) 'sets progressbar
    End If
    End Select
    End Sub


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured