|
-
Mar 28th, 2003, 03:51 AM
#1
[RESOLVED] Trying winsock File Transfer (RESOLVED)
Hi,
I had a go today at making my own file transfer example. This is what I have so far but I can't get in to work properly. What code do I need to add?
VB Code:
'Client Code
Dim file As String
Dim Blocksize As Long
Dim NF As Integer
Private Sub Command1_Click()
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileTitle
End Sub
Private Sub Command2_Click()
Winsock1.Connect "127.0.0.1", "7000"
End Sub
Private Sub Command3_Click()
NF = FreeFile
Open CommonDialog1.filename For Binary Access Read As #NF
file = Space$(Blocksize)
Get #NF, , file
Do Until EOF(NF)
Winsock1.SendData file
Loop
Close #NF
End Sub
'Server Code
Dim file As String
Dim dat As String
Dim NF As Integer
Private Sub Form_Load()
Winsock1.LocalPort = 7000
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Winsock1.GetData dat
If InStr(1, dat, "|DATA|") <> 0 Then
NF = FreeFile
Open App.Path & "\test.exe" For Binary Access Write As #NF
Put #NF, , file
Close #NF
End If
End Sub
Please give me some help to point me in the right direction.
TIA
NW
Last edited by Nightwalker83; Apr 1st, 2003 at 04:30 AM.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 28th, 2003, 05:18 AM
#2
Lively Member
hi!!
Here is how I did it...
Send File
VB Code:
Dim filenum As Integer
Dim var_data As String
filenum = FreeFile
Open g_FilePath For Binary Access Read Lock Read As #filenum
Do While (Loc(filenum) < LOF(filenum))
DoEvents
var_data = Input(4096, #filenum)
If Winsock1.State = 7 Then
Winsock1.SendData var_data
End If
Loop
Close #filenum
Receive file
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim bData As String
Winsock1.GetData bData, vbString
If g_PutByte = 1 Then
g_FileOpen = FreeFile
Open g_FilePath For Binary As #g_FileOpen
End If
Put #g_FileOpen, g_PutByte, bData
g_PutByte = g_PutByte + Len(bData)
' The File Transfer is complete
If g_PutByte >= g_InFileSize Then
Close #g_FileOpen
End If
End Sub
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Winsock1.GetData dat
If InStr(1, dat, "|DATA|") <> 0 Then
NF = FreeFile
Open App.Path & "\test.exe" For Binary Access Write As #NF
Put #NF, , file
Close #NF
End If
End Sub
perheps you only need to set the byte pos where to start
storing the received bytes in the file
Put #NF, nBytePos, file
[vbcode]
SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
[/vbcode]
-
Mar 29th, 2003, 12:08 AM
#3
VB Code:
'Client Code
Dim NF As Integer
Dim file As String
Private Sub Command1_Click()
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.filename
End Sub
Private Sub Command2_Click()
Winsock1.Connect "127.0.0.1", 6000
End Sub
Private Sub Command3_Click()
NF = FreeFile
Open CommonDialog1.FileTitle For Binary Access Read Lock Read As #NF
Do While (Loc(NF) < LOF(NF))
DoEvents
file = Input(4096, NF)
If Winsock1.State = sckConnected Then
Winsock1.SendData file
End If
Loop
Close #NF
End Sub
'Server Code
Dim WF As Integer
Dim dat As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
If received = 1 Then
WF = FreeFile
Open WF_filepath For Binary As #WF 'How do you get the file path from the server?
Put #WF, received, dat
received = received + Len(dat)
If received >= LOF(dat) Then 'How do you get to file size from the server?
Close #WF
End If
End If
End Sub
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 29th, 2003, 09:35 AM
#4
Lively Member
hi!
1# when receiving a file you should only open it one time
2# g_PutByte = g_PutByte + bytesTotal so that you start writing at the end of the new file.
3# when all of the bytes is reveived then close the file.
to know the size of the file you must get the file size on the client
side and send it to the server ( this is how I did it, maybe there is a better way )
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
If received = 1 Then
WF = FreeFile
Open WF_filepath For Binary As #WF 'How do you get the file path from the server?
Put #WF, received, dat
received = received + Len(dat)
If received >= LOF(dat) Then 'How do you get to file size from the server?
Close #WF
End If
End If
End Sub
in your example you only open the file and put tada in it if received = 1 you must put the data in the new file on every
DataArrival.
ex:
1# Put #WF, 1, dat
2# Put #WF, 4097, dat
3# Put #wf, 8193, dat
untill the whole file is received.
[vbcode]
SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
[/vbcode]
-
Mar 29th, 2003, 08:22 PM
#5
VB Code:
'Server
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
WF = FreeFile
received = 1
Do Until Len(dat)
Open App.Path & "\test.jpg" For Binary As #WF
Put #WF, received, dat
received = received + Len(dat)
Loop
If received >= Len(dat) Then
Close #WF
End If
End Sub
It doesn't send all the data before closing the file. I put a loop so it would keep writing the data to the file until the file is the correct size.
Last edited by Nightwalker83; Mar 30th, 2003 at 06:21 PM.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 30th, 2003, 08:41 AM
#6
Lively Member
try to send pakets of 4096 bytes
VB Code:
Dim filenum As Integer
Dim var_data As String
filenum = FreeFile
Open g_FilePath For Binary Access Read Lock Read As #filenum
Do While (Loc(filenum) < LOF(filenum))
DoEvents
var_data = Input(4096, #filenum)
If Winsock1.State = 7 Then
Winsock1.SendData var_data
End If
Loop
Close #filenum
you should not have to use the loop in the dataarrival sub..
And you cant open the file on each dataArrival but only close
it once..!
And if you shoose to open the file on each DataArrival it will slow
down the transfer..
[vbcode]
SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
[/vbcode]
-
Mar 30th, 2003, 07:56 PM
#7
VB Code:
'Client Code
Dim NF As Integer
Dim file As String
Private Sub Command1_Click()
CommonDialog1.ShowOpen
Text1.Text = CommonDialog1.FileName
End Sub
Private Sub Command2_Click()
Winsock1.Connect "127.0.0.1", 6000
End Sub
Private Sub Command3_Click()
NF = FreeFile
Open CommonDialog1.FileTitle For Binary Access Read Lock Read As #NF
Do While (Loc(NF) < LOF(NF))
DoEvents
file = Input(4096, NF)
If Winsock1.State = sckConnected Then
Winsock1.SendData file
End If
Loop
Close #NF
End Sub
'Server Code
Dim WF As Integer
Dim dat As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
If received = 1 Then
WF = FreeFile
Open App.Path & "\Test.jpg" For Binary As #WF
End If
Put #WF, received, dat
received = received + Len(dat)
If received >= Len(dat) Then
Close #WF
End If
End Sub
How can I be sure it is sending data all the time? I tried the above code and I get a message on the server saying:
I tried to fix the problem by putting:
Before the first if statement in the data arrival event but when I send files the output size on the server end is alway 4096bytes.
That is for every file not just the files that are suppose to be that size.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 31st, 2003, 03:20 AM
#8
Lively Member
hi!
received needs to be a global variable
and it sould be set to 1 before the start of
receiving a file...
try this..
VB Code:
'Server Code
Option Explicit ' new line
Private received As long ' new line
Dim WF As Integer
Dim dat As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
received =1 ' new line
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
If received = 1 Then
WF = FreeFile
Open App.Path & "\Test.jpg" For Binary As #WF
End If
Put #WF, received, dat
received = received + Len(dat)
If received >= Len(dat) Then
Close #WF
End If
End Sub
[vbcode]
SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
[/vbcode]
-
Mar 31st, 2003, 04:23 AM
#9
Originally posted by meteor
hi!
received needs to be a global variable
and it sould be set to 1 before the start of
receiving a file...
try this..
VB Code:
'Server Code
Option Explicit ' new line
Private received As long ' new line
Dim WF As Integer
Dim dat As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
received =1 ' new line
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
If received = 1 Then
WF = FreeFile
Open App.Path & "\Test.jpg" For Binary As #WF
End If
[color=red]Put #WF, received, dat[/color]
received = received + Len(dat)
If received >= Len(dat) Then
Close #WF
End If
End Sub
I still get this error on the highlighted line.
Sorry if I am annoying you with this but I have no experience with the data transfer client/server application.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Mar 31st, 2003, 05:15 AM
#10
Lively Member
hi!
well!!! it might me because you only open the file one (correct)
but you close the file on each dataarrival..
If received >= inFileSize Then
Close #WF
End If
here is the full working source code.. try it and compare it with
your own...
VB Code:
'Client Code
'APIs to get the file size..
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Const OF_READ = &H0&
Private Sub Command2_Click()
Winsock1.Connect "xxx.xxx.xxx.xxx", 6000
End Sub
Private Sub Command3_Click()
Dim strFileName As String
Dim strFilePath As String
strFilePath = "c:\tmp\anything.exe"
strFileName = "anything.exe"
Dim lFileSize As Long
lFileSize = FileSize(strFilePath)
' Send the size of the file to the server.
' Send the file name to the server.
If Winsock1.State = sckConnected Then
Winsock1.SendData "FILESIZE=" & lFileSize
DoEvents
Winsock1.SendData "FILENAME=" & strFileName
DoEvents
End If
NF = FreeFile
Open strFilePath For Binary Access Read Lock Read As #NF
Do While (Loc(NF) < LOF(NF))
DoEvents
file = Input(4096, NF)
If Winsock1.State = sckConnected Then
Winsock1.SendData file
End If
Loop
Close #NF
End Sub
Private Function FileSize(ByVal strSrc As String) As Long
Dim Pointer As Long, sizeofthefile As Long, lpFSHigh
Pointer = lOpen(strSrc, OF_READ)
'size of the file
sizeofthefile = GetFileSize(Pointer, lpFSHigh)
lclose Pointer
FileSize = sizeofthefile
End Function
Private Sub Timer1_Timer()
Label1.Caption = Winsock1.State
End Sub
'Server Code
Private WF As Integer
Private dat As String
Private received As Long
Private g_strFileName As String
Private g_lFileSize As Long
Private g_strInPath As String
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
g_strInPath = "c:\incoming\" ' where to store the file..
received = 1
End Sub
Private Sub Timer1_Timer()
Label1.Caption = Winsock1.State
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Winsock1.GetData dat, vbString
Dim pos As Integer
If Left(dat, 8) = "FILESIZE" Then
pos = InStr(1, dat, "=")
g_lFileSize = Right(dat, Len(dat) - pos)
Exit Sub
ElseIf Left(dat, 8) = "FILENAME" Then
pos = InStr(1, dat, "=")
g_strFileName = Right(dat, Len(dat) - pos)
Exit Sub
End If
If received = 1 Then
Debug.Print "OPEN = " & g_strInPath & g_strFileName
WF = FreeFile
Open g_strInPath & g_strFileName For Binary As #WF
End If
Put #WF, received, dat
received = received + Len(dat)
Debug.Print received
If received >= g_lFileSize Then
Debug.Print "CLOSE = " & g_strInPath & g_strFileName
Close #WF
End If
End Sub
Hope this helps!!
Last edited by meteor; Apr 7th, 2003 at 02:06 AM.
[vbcode]
SetLayeredWindowAttributes hWnd, 0, 220, LWA_ALPHA
[/vbcode]
-
Apr 20th, 2003, 03:17 AM
#11
VB Code:
'Add a reference to MicrosoftScriptingRuntimes
'Client
Option Explicit
Dim FSO As New FileSystemObject
Dim F As file
Dim nf As Integer
Dim fname As String
Dim fsize As Long
Dim file As String
Private Sub Command1_Click()
CommonDialog1.ShowOpen
End Sub
Private Sub Command2_Click()
Winsock1.Connect "127.0.0.1", 6000
End Sub
Private Sub Command3_Click()
On Error Resume Next
nf = FreeFile
Open CommonDialog1.FileName For Binary Access Read Lock Read As #nf
Set F = FSO.GetFile(CommonDialog1.FileName)
If Winsock1.State = sckConnected Then
fname = F.Name
fsize = F.Size
Winsock1.SendData "|FNAME|" & fname
DoEvents
Do While (Loc(nf) < LOF(nf))
DoEvents
file = Input(4096, nf)
Winsock1.SendData "|FILE|" & file
Loop
Set F = Nothing
Close #nf
If Winsock1.State = sckConnected Then
Winsock1.SendData "|FSIZE|" & fsize
DoEvents
End If
End If
End Sub
'Server
Option Explicit
Dim WF As Integer
Dim dat As String
Dim fname As String
Dim fsize As Long
Dim file As String
Private received As Long
Private Sub Form_Load()
Winsock1.LocalPort = 6000
Winsock1.Listen
received = 1
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
On Error Resume Next
Winsock1.GetData dat, vbString
If InStr(1, dat, "|FNAME|") <> 0 Then
fname = Mid$(dat, 8, Len(dat))
If received = 1 Then
WF = FreeFile
Open App.Path & "\" & fname For Binary As #WF
End If
End If
If InStr(1, dat, "|FILE|") <> 0 Then
file = Mid$(dat, 7, Len(dat))
Put #WF, received, file
received = received + Len(file)
End If
If InStr(1, dat, "|FSIZE|") <> 0 Then
fsize = Mid$(dat, 10, Len(dat))
If received >= fsize Then
Close #WF
received = 1 'Must be set back to one after each file transfer
End If
End If
End Sub
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
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
|