Visual Basic - Winsock sending files
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?
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
'wsTCP(1).SendData "finish,a"
wsTCP(1).SendData buffer
Text1.Text = "clientsend"
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$
' Extract the command from the Left
' of the comma (default divider)
Command = EvalData(NewArrival$, 1)
' extract the data being sent from the
' right of the comma (default divider)
Data = EvalData(NewArrival$, 2)
Select Case Command
Case "finish"
Text1.Text = Command
Text2.Text = Data
pic1.Picture = LoadPicture(App.Path & "\a.jpg")
Close #1
exit sub
Case "open"
If InStr(Data, vbCrLf) <> 0 Then Data = Left(Data, InStr(Data, vbCrLf) - 1)
Open App.Path & "\" & Data For Binary As 1
lPos = 1
' send notification to sender to let it know the receiver is ready to receive the file
wsTCP(intConnection).SendData "OK" & 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
' 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
' 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
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.