|
-
Jul 15th, 2006, 09:52 AM
#1
Thread Starter
Junior Member
Sending files via winsock
Hello,
For my client server program that I am using on my home network I have to send a file from the server to the client or vise versa.
I have been looking at the necessay code although it is quite complex.
Please could someone try and explain what this code would be like or where to find it.
I know that I have to use either a binary transfer or winsock control.
Thank you
-
Jul 15th, 2006, 10:00 AM
#2
Junior Member
Re: Sending files via winsock
You could take blocks of data from your file, convert the binary to hexadecimal values (in a format like string:FF071BEE, send it through your winsock control (ascii) and convert it on the other side (at the receiver's)
-
Jul 15th, 2006, 10:46 AM
#3
Thread Starter
Junior Member
Re: Sending files via winsock
That sounds complex, heres what I got so far:
Client
Code:
Private Sub command3_click()
Dim pb As PropertyBag
Set pb = New PropertyBag
Dim bArr() As Byte
Winsock1.GetData bArr()
pb.Contents = bArr()
screenCapture.Picture = pb.ReadProperty("hello")
End Sub
Server
Code:
Dim pb As PropertyBag
Set pb = New PropertyBag
Dim bArr() As Byte
pb.WriteProperty "hello", "c:\hello.bmp", Nothing
bArr = pb.Contents
Winsock1.SendData bArr()
DoEvents
DoEvents
DoEvents
does not work tho, please help
Cheers
-
Jul 15th, 2006, 11:14 AM
#4
Junior Member
Re: Sending files via winsock
try this:
VB Code:
Public Sub sendFile(location As String, socket As Winsock)
Dim fNum As Integer
Dim fLen As Integer
fNum = FreeFile
fLen = FileLen(location)
Dim bytes(1 To fLen) As Byte
Open location For Binary As fNum
Get #fNum, 1, bytes
Close fNum
socket.SendData (bytes)
End Sub
Public Sub getFile(location As String, socket As Winsock)
Dim fNum As Integer
fNum = FreeFile
Dim bytes() As Byte
socket.GetData (bytes)
Open location For Binary As fNum
Get #fNum, 1, bytes
Close fNum
End Sub
Haven't tested it yet, but something like this should work.. It's a bit less complicated than my first idea. Maybe you'll have to send file info like size first, to determine the array size on the receiver's side.
Goodluck!
-
Jul 15th, 2006, 11:21 AM
#5
Thread Starter
Junior Member
Re: Sending files via winsock
How would i customize this to make it work for me e.g. would lcoation be the file and path - c:\hello.bmp
Cheers
-
Jul 15th, 2006, 11:34 AM
#6
Junior Member
Re: Sending files via winsock
server:
Create a form with a winsock component in it.
Accept client connection
call the getfile function: location is the location of the new file to be received on your hdd, socket is the just connected socket (I do not know how the getData function of the winsock object works exactly, I think it'll just wait for data being received)
Client:
Create a form with a winsock component in it.
Setup a connection with the component (connect to server)
call the sendfile function: location is the location of the file to be sent on your hdd, socket is the just connected socket
Again: I haven't tested this yet
-
Jul 15th, 2006, 11:38 AM
#7
Thread Starter
Junior Member
Re: Sending files via winsock
i get a compile error:
constant expression required
and is "fLen" highlighted
cheers
-
Jul 15th, 2006, 12:57 PM
#8
Junior Member
Re: Sending files via winsock
Try this:
Dim bytes() As Byte
ReDim bytes(1 To fLen) As Byte
Instead of:
Dim bytes(1 To fLen) As Byte
-
Jul 15th, 2006, 03:43 PM
#9
Thread Starter
Junior Member
Re: Sending files via winsock
OK cheers that got rid of that error.
Server:
Code:
Dim location As String
location = "c:\desktop.bmp"
Dim fNum As Integer
Dim fLen As Integer
fNum = FreeFile
fLen = FileLen(location)
Dim bytes() As Byte
ReDim bytes(1 To fLen) As Byte
Open location For Binary As fNum
Get #fNum, 1, bytes
Close fNum
Winsock1.SendData (bytes)
Client:
Code:
Private Sub command3_click()
Dim location As String
Dim fNum As Integer
location = "c:\desktop.bmp"
fNum = FreeFile
Dim bytes() As Byte
Winsock1.GetData (bytes)
Open location For Binary As fNum
Get #fNum, 1, bytes
Close fNum
End Sub
Please can you tell me what is wrong with this code. It sends a file but it is of 0 bytes and therefoer does not show anything.
Cheers
-
Jul 15th, 2006, 10:12 PM
#10
PowerPoster
Re: Sending files via winsock
needs alot more than that .. you need to use the Winsock Functions ..
im working on a specific winsock Send/Receive File app, ill post the code when done ..
Meanwhile this is what im basing it off .. got this (or a version of it) off this forum somewhere... if its your code .. thanks .. cant remember who exactly posted it ..
I stripped it down from a VNC type app i was messing with .. so in this case it waits for a connection from a remote client .. then once it accepts the connection it simply sends the file to the client (tmp.jpg in this instance).. very basic sample but this may give you an idea ..
Both projects use a Winsock1 and a Timer1 .. Load Send Program First .. Receive Program Connects on Form Load. Port is 1003 and default IP is 192.168.1.2 for both of them ... (testing on local PC!). Missing alot of other features which i have in my other winsock app/s but it will work as is, for an example. An image called tmp.jpg is required in the Send Project's path to test .. or change the file name in the code.
EDIT: i uploaded the 2 projects to make it easier..
Send Project:
VB Code:
Option Explicit
Private iFileNum As Integer, lPacketSize As Long
Private Sub Form_Load()
On Error GoTo Err
Winsock1.Close
Winsock1.LocalPort = 1003
Winsock1.Listen
Me.Caption = "Listening: Port 1003"
Exit Sub
Err:
MsgBox "Socket Error!" & vbNewLine & _
Err.Description
Unload Me
Exit Sub
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
Timer1.Interval = 0
Timer1.Enabled = False
End Sub
Private Sub Winsock1_Close()
If Winsock1.State = sckClosing Then
Winsock1.Close
End If
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close
End If
Winsock1.Accept requestID
SendFile Winsock1, App.Path & "\tmp.jpg"
End Sub
Public Sub SendFile(SocketObject As Winsock, ByVal FilePath As String, Optional ByVal PacketSize As Long = 1024)
Dim Buffer() As Byte
lPacketSize = PacketSize ' save the PacketSize for the timer
Timer1.Enabled = False ' make suze timer is not enabled
iFileNum = FreeFile ' get free file number
Open FilePath For Binary Access Read As iFileNum ' open file
' if file size is smaller than PacketSize, then send the whole file, but not more
ReDim Buffer(lngMIN(LOF(iFileNum), PacketSize) - 1)
Get iFileNum, , Buffer ' read data
SocketObject.SendData Buffer ' send data
End Sub
Public Function lngMIN(ByVal L1 As Long, ByVal L2 As Long) As Long
If L1 < L2 Then
lngMIN = L1
Else
lngMIN = L2
End If
End Function
Private Sub Winsock1_SendComplete()
Timer1.Enabled = False
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim Buffer() As Byte, BuffSize As Long
Timer1.Enabled = False
If iFileNum <= 0 Then Exit Sub
If Loc(iFileNum) >= LOF(iFileNum) Then ' FILE COMPLETE
Close iFileNum ' close file
iFileNum = 0 ' set file number to 0, timer will exit if another timer event
BuffSize = 0
Winsock1.Close
Winsock1.LocalPort = 1003
Winsock1.Listen
Me.Caption = "Listening: Port 1003"
Exit Sub
End If
'if the remaining size in the file is smaller then PacketSize, the read only whatever is left
BuffSize = lngMIN(LOF(iFileNum) - Loc(iFileNum), lPacketSize)
ReDim Buffer(BuffSize - 1) ' resize buffer
Get iFileNum, , Buffer ' read data
Winsock1.SendData Buffer ' send data
' Show progress
Me.Caption = "Sending: " & Format(Loc(iFileNum) / CDbl(LOF(iFileNum)) * 100#, "#0.00") & "% Done"
' timer event will be called again when last packet is sent, close the file then
End Sub
Receive Project;
VB Code:
Option Explicit
'// PROGRAM SETTINGS
Const TCP_IP As String = "192.168.1.2"
Const TCP_PORT As Long = 1003
Private ReceiveData As String
Private FileBytes As Long
'// CLOSE WINSOCK
Private Sub Winsock1_Close()
Dim strTempFile As String
Dim intFile As Integer
If Winsock1.State = sckClosing Then
Winsock1.Close
End If
strTempFile = App.Path & "\tmp.jpg"
intFile = FreeFile
Open strTempFile For Binary As #intFile
Put #intFile, , ReceiveData
Close #intFile
ReceiveData = ""
Me.Caption = "Done"
End Sub
'// DATA ARRIVES
Private Sub Winsock1_dataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
ReceiveData = ReceiveData & strData
FileBytes = FileBytes + bytesTotal
Me.Caption = "Downloading: " & FileBytes & " bytes"
End Sub
'// ERROR
Private Sub Winsock1_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)
Winsock1.Close
MsgBox "Error Connecting!"
End Sub
'// FORM LOAD
Private Sub Form_Load()
Timer1.Enabled = True
Timer1.Interval = 100
With Winsock1
.RemoteHost = TCP_IP
.RemotePort = TCP_PORT
.Connect
End With
End Sub
'// UNLOAD FORM
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
Timer1.Interval = 0
Timer1.Enabled = False
End Sub
Last edited by rory; Jul 16th, 2006 at 12:27 AM.
-
Jul 16th, 2006, 05:55 AM
#11
Thread Starter
Junior Member
Re: Sending files via winsock
Thank you for that code, this is most helpful.
I finally got a file to send over the network although it onyl works when I use the default gateway for the IP - why is this?
Cheers
-
Jul 16th, 2006, 08:51 AM
#12
Thread Starter
Junior Member
Re: Sending files via winsock
The other problem is that when the file is sent and then recieved it is of size 0 bytes.
Just about to give up on this, unless someone can tell what I have to chaneg in the code above. Not critising the code, just cant get it to work.
Cheers
-
Jul 16th, 2006, 12:32 PM
#13
PowerPoster
Re: Sending files via winsock
works for me .. make sure any firewalls are open etc ..
-
Jul 16th, 2006, 12:43 PM
#14
Thread Starter
Junior Member
Re: Sending files via winsock
So it will work when not using default gateway i.e. using external IP instead?
cheers
-
Jul 16th, 2006, 12:45 PM
#15
PowerPoster
Re: Sending files via winsock
You need to set an IP address .. eg. 24.244.23.545
not the gateway ..
-
Jul 16th, 2006, 12:54 PM
#16
Thread Starter
Junior Member
Re: Sending files via winsock
OK ill try again. If you change the IP in your code, do you only have to change one thing. Just thought that I might not be changing enough code for it to work.
-
Jul 16th, 2006, 12:58 PM
#17
PowerPoster
Re: Sending files via winsock
just the IP and the Port Number ... Port Number has to be the same in both, it will give a Message box error anyway if it cant connect.
and make sure you either have a file called tmp.jpg in the app path, or rename that file to something else that exists ..
-
Jul 16th, 2006, 01:22 PM
#18
Thread Starter
Junior Member
Re: Sending files via winsock
Well sorry but its still not working - the file tmp.jpg is on the machine with the Recieve.exe on right - and then tmp.jpg arrives on machnie with Send.exe on.
I keep getting supscript out of range error (9).
Would really like to get this going.
cheers for helps so far
-
Jul 16th, 2006, 01:26 PM
#19
Thread Starter
Junior Member
Re: Sending files via winsock
OK got it working, dunno what i did but it works
-
Nov 27th, 2006, 01:38 AM
#20
Hyperactive Member
Re: Sending files via winsock
The File tranfers work wonder..but what if I want to tranfer an Entire folder...(which may contains some files and folders)..how to go about it..please Help....
-
Nov 27th, 2006, 08:30 AM
#21
Re: Sending files via winsock
 Originally Posted by rory
Meanwhile this is what im basing it off .. got this (or a version of it) off this forum somewhere... if its your code .. thanks .. cant remember who exactly posted it .. 
That would be me, and you got it from here: http://www.vbforums.com/showthread.php?t=331990
Here's another client/server to transfer files:
http://www.vbforums.com/showthread.php?t=377648
To send multiple files, list the files in a collection (or something like that), send each file one by one.
Don't forget to WAIT until file is complectly sent, and then send the other file, and so on...
-
Nov 27th, 2006, 08:34 AM
#22
Re: Sending files via winsock
 Originally Posted by Kuamr
The File tranfers work wonder..but what if I want to tranfer an Entire folder...(which may contains some files and folders)..how to go about it..please Help....
Another way to do it, is to ZIP the whole directory, therefore having one file (and less data to transfer because is compressed), then send that file, and on the other side, just un-zip the file at your target location...
-
Nov 27th, 2006, 02:10 PM
#23
PowerPoster
Re: Sending files via winsock
 Originally Posted by CVMichael
Thanks
-
Dec 5th, 2006, 09:04 AM
#24
Hyperactive Member
Re: Sending files via winsock
-
Jan 29th, 2008, 11:50 PM
#25
New Member
Re: Sending files via winsock
Hi, CVMicheal....
Nice to meet you..., i am new member of this forum....
i already trial your program,..
but if i send Exe File, server never complete receive data....
do you know why am i always fail(never complete) send exe file ???
Thanks.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|