|
-
Dec 20th, 2006, 10:16 AM
#8
Re: Sending Images With Winsock
There are 2 problems:
First, I don't know why you have an Index for the socket when for the client you should have only one connection. If this is for the server (instead of client) then the code should be diferent. If it's the server then you have to give each file a unique name because otherwise you will write data from all clients into one file, and I'm sure you don't want that...
Second: The next code you should use only for transfering the image file, if you transfer data other than the file, then you have to change the code for the other types of data. Since I don't know what else you transfer (how and what format), I could not write the code to support that also.
OK, here is the code, I did not test it, but it should work:
VB Code:
Private Sub WS_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim sData As String, Pos As Long
' make the variales static so the data gets preserved between calls
' you can dim next variables in the
' General Declarations section also (but remove next line if you do so)
Static DataLength As Long, FileNum As Integer
'"image":length:data...
WS(Index).GetData sData, vbString
' first time, read header and open file
If DataLength = 0 And LCase(Left(sData, 6)) = "image:" Then
' get the total data length
DataLength = Val(Split(sData, ":")(1))
' find the second ":"
Pos = InStr(1, sData, ":")
Pos = InStr(Pos + 1, sData, ":")
' get only the bitmap data (stip off the header)
sData = Mid$(sData, Pos + 1)
FileNum = FreeFile
Open "C:\Draw.bmp" For Binary Lock Write As FileNum
End If
' save data into file
If DataLength > 0 And FileNum <> 0 Then
Put FileNum, LOF(FileNum) + 1, sData
' we received all the data that we are supposed to receive
If LOF(FileNum) >= DataLength Then
' close the file, and reset variables
Close FileNum
DataLength = 0
FileNum = 0
' load picture
picMain.Picture = LoadPicture("C:\Draw.bmp")
' delete temporary picture file
Kill "C:\Draw.bmp"
Else
End If
End Sub
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
|