Welcome to HBH! If you had an account on hellboundhacker.org you will need to reset your password using the Lost Password system before you will be able to login.

Visual Basic - Winsock sending files


Mr_Cheese's Avatar
0 1

is it possible for winsock to send files easily.

i google for a bit, but the main findings are just ftp programs, which im not really interested in, i only want a simple command to use to send the file.

like send the data as a byte or something?!?!

the reason i ask this is because a program im making, im tring to get a jpg file to be sent between 2 computers.

any ideas?


ghost's Avatar
0 0

I found this article

Hope it helps;)

Client side: Option Explicit

Dim buffer() As Byte Dim lBytes As Long Dim temp As String Dim ok As Boolean

Private Sub Form_Load()

' cmdSend.Enabled = False

ok = False
lBytes = 0

Load wsTCP(1)
wsTCP(1).RemoteHost = "localhost"
wsTCP(1).RemotePort = 1111
wsTCP(1).Connect

End Sub

Private Sub wsTCP_Close(Index As Integer)

Unload wsTCP(1) End Sub

Private Sub wsTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long)

wsTCP(1).GetData temp If temp = "a" Then

 wsTCP(1).SendData "open,a.jpg" & vbCrLf

 Exit Sub

End If

If InStr(temp, vbCrLf) <> 0 Then temp = Left(temp, InStr(temp, vbCrLf) - 1)

If temp = "OK" Then

&#39;wsTCP(1).SendData &quot;finish,a&quot;

wsTCP(1).SendData buffer
Text1.Text = &quot;clientsend&quot;

End If

End Sub

Server side:

Option Explicit

Dim lPos As Long Dim bOK As Boolean Dim fname As String Dim intConnection As Integer Dim showOrNot As Boolean

Private Sub cmdStart_Click() wsTCP(intConnection).SendData "a" showOrNot = False End Sub

Private Sub Form_Load()

wsTCP(0).LocalPort = 1111
wsTCP(0).Listen

End Sub

Private Sub wsTCP_Close(Index As Integer)

Close #1 Unload wsTCP(intConnection) bOK = False

End Sub

Private Sub wsTCP_ConnectionRequest(Index As Integer, ByVal requestID As Long)

intConnection = intConnection + 1 Load wsTCP(intConnection) wsTCP(intConnection).Accept requestID

End Sub

Private Sub wsTCP_DataArrival(Index As Integer, ByVal bytesTotal As Long)

Dim Command      As String
Dim NewArrival   As String
Dim Data         As String
Static DataCnt   As Long

wsTCP(intConnection).GetData NewArrival$

&#39; Extract the command from the Left
&#39; of the comma (default divider)
Command = EvalData(NewArrival$, 1)
&#39; extract the data being sent from the
&#39; right of the comma (default divider)
Data = EvalData(NewArrival$, 2)

Select Case Command
             
Case &quot;finish&quot;
    Text1.Text = Command
    Text2.Text = Data
    pic1.Picture = LoadPicture(App.Path & &quot;&#92;a.jpg&quot;)
    Close #1
    exit sub

Case &quot;open&quot;
   
    If InStr(Data, vbCrLf) &lt;&gt; 0 Then Data = Left(Data, InStr(Data, vbCrLf) - 1)
    Open App.Path & &quot;&#92;&quot; & Data For Binary As 1
   
    lPos = 1
     
 &#39; send notification to sender to let it know the receiver is ready to receive the file
 wsTCP(intConnection).SendData &quot;OK&quot; & vbCrLf
   
Case Else
   
    Dim buffer() As Byte
    wsTCP(intConnection).GetData buffer
    Put #1, lPos, buffer
    lPos = lPos + UBound(buffer) + 1

End Select

End Sub

Public Function EvalData(sIncoming As String, iRtLt As Integer, _ Optional sDivider As String) As String Dim i As Integer Dim tempStr As String ' Storage for the current Divider Dim sSplit As String

' the current character used to divide the data If sDivider = "" Then sSplit = "," Else sSplit = sDivider End If

' getting the right or left? Select Case iRtLt

  Case 1
      &#39; remove the data to the Left of the Current Divider
      For i = 0 To Len(sIncoming)
        tempStr = Left(sIncoming, i)
       
        If Right(tempStr, 1) = sSplit Then
          EvalData = Left(tempStr, Len(tempStr) - 1)
          Exit Function
        End If
      Next
     
  Case 2
      &#39; remove the data to the Right of the Current Divider
      For i = 0 To Len(sIncoming)
        tempStr = Right(sIncoming, i)
       
        If Left(tempStr, 1) = sSplit Then
          EvalData = Right(tempStr, Len(tempStr) - 1)
          Exit Function
        End If
      Next

End Select


ghost's Avatar
0 0

THink about how files are sent. You must first have a client and a server application, or p2p style connection, and then you send packets. Decide on the size of the packets first of all. Think in groups of 1024 bytes. Here's basically what you're trying to make happen-

Client sending file to server-

Client sends attempted connection, Server Responds Client Connected to server Client sends a packet that says 'The file I'm sending is 3.5mb in Binary mode' Server recieves packet and says 'I Understand, accepted, go ahead' Client opens the file in binary mode, Server opens in binary mode Client reads the first 1024 bytes of the file in binary mode, sends them as a packet with all needed TCP/IP headers, totalling a packet of 1028bytes or so Server recieves, dumps the packet in its newly opened file, sends a 'Recieved' Packet Client continues sending, Server recieves Client says ' File done', server recieves and says 'The file is the correct size, done' Client makes a file checksum, sends to server Server Recieves and compares the checksum and repsponds 'File is Ok. Connection closing" and finalizes all the files.

Theres more to it than that, but thats about how files are transferred. You could try things like searching the packet for whitespace and repeated characters or patterns, and makign a Key for replacments to shrink the file, and then have the server uncompress. It's really fun to play with file transfers.