|
-
Mar 27th, 2004, 07:32 PM
#1
Thread Starter
Fanatic Member
Socket file corruption [RESLVD]
Allright, this is my first attempt at sending a file over winsock ... heres my code
CLIENT
Code:
Private Sub sendPicture(strFileName As String)
Dim picMain As New StdPicture
Dim fileLEN As Long
Dim byteChunk(999) As Byte
Open strFileName For Binary As #1
sckSocket.SendData "[FIS]" & LOF(1)
fileLEN = LOF(1)
While (fileLEN >= 0)
DoEvents
Get 1, , byteChunk()
fileLEN = fileLEN - 1000
If (fileLEN > 0) Then
sckSocket.SendData sndChunk()
sckSocket.SendData byteChunk()
Else
sckSocket.SendData endChunk()
sckSocket.SendData byteChunk()
End If
Wend
Close #1
MsgBox "Send complete!"
End Sub
SERVER
Code:
If (Left(Data, 5) = "[FIS]") Then
Data = cutLData(Data, 5)
getFileIndex = 0
totalSize = Data
ReDim getFile(0 To (totalSize + 2000)) 'need fix +2000, keeps giving (out of bound error)
With frmVote 'ignore me, display only
.Show
.frameVote.Visible = False
.frameGetFile.Visible = True
.lblTotalSize.Caption = "Total size: " & Val(Data)
End With
ElseIf (Left(Data, 5) = "[FIL]") Then
Data = cutLData(Data, 5)
For i = 1 To Len(Data) - 1
getFile(getFileIndex) = Asc(Mid(Data, i, 1))
getFileIndex = getFileIndex + 1
Next i
frmVote.lblGotSize = "Got: " & getFileIndex & " bytes"
ElseIf (Left(Data, 5) = "[FIE]") Then
Dim intTrim As Integer
intTrim = totalSize - getFileIndex
Data = cutLData(Data, 5)
Trim Data
For i = 1 To Len(Data) - 1 - (intTrim) '(OOB) here, ***?
getFile(getFileIndex) = Asc(Mid(Data, i, 1))
getFileIndex = getFileIndex + 1
Next i
Open "c:\test.jpg" For Binary As #2
Put #2, , getFile
Close #2
frmVote.lblGotSize = "Got: " & getFileIndex & " bytes (done!)"
End If
When i get the picture on the other side, its really corrupt... I mean the effect is cool and all but its not what I'm going for 
What am i doing wrong, AND am i doing this really inefficently?
I tried to poke around the forum for sending around files but didnt really come accross anything good so i came up with my own way of doing it...
some variables are declared globaly, so just assume they work.
Any help would be good, thanks!
Last edited by invitro; Mar 27th, 2004 at 08:56 PM.
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
-
Mar 27th, 2004, 08:08 PM
#2
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
-
Mar 27th, 2004, 08:20 PM
#3
Thread Starter
Fanatic Member
hmmm, interesting... it looks to me that you dont need to use bytes at all... just accept it as a string and write it out to a binary file. That would solve my array problem... the code seems kind of messy, to me... so im going to rewrite my code and post it on here for anyone else to use
thanks for the reply
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
-
Mar 27th, 2004, 08:37 PM
#4
Thread Starter
Fanatic Member
Ok, for much much simplified data sending... heres my code for those of you who are curious. What it does is take chunks of 1000bytes and sends em over, no biggie.... it works i just tested it.
Oh sorry cutLData is a function that cutes off 5 bytes from the left... just makes it easier oppose to using left all the time... yeah yeah im anal i know
CLIENT
Code:
Private Sub sendPicture(strFileName As String)
Dim picMain As New StdPicture
Dim fileLEN As Long
Dim byteChunk As String * 1000
Open strFileName For Binary As #1
sckSocket.SendData "[FIS]" & LOF(1)
While (Not EOF(1))
DoEvents
Get 1, , byteChunk
sckSocket.SendData "[FIL]" & byteChunk
Wend
Close 1
MsgBox "Send complete!"
End Sub
SERVER
Code:
If (Left(Data, 5) = "[FIS]") Then
Data = cutLData(Data, 5)
getFileIndex = 0
totalSize = Data
With frmVote
.Show
.frameVote.Visible = False
.frameGetFile.Visible = True
.lblTotalSize.Caption = "Total size: " & Val(Data)
End With
ElseIf (Left(Data, 5) = "[FIL]") Then
Data = cutLData(Data, 5)
getFile = getFile & Data
getFileIndex = getFileIndex + Len(Data)
frmVote.lblGotSize = "Got: " & getFileIndex & " bytes"
If (getFileIndex >= totalSize) Then
Open "C:\test.jpg" For Binary As #2
Put 2, , getFile
Close #2
End If
End If
ok, so... windows takes 1 minute to search for a file on my PC yet google.com takes 1 second to search the entire internet? 
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
|