DirectPlay 8 and VB6, send files.
I use DirectPlay 8 to send files over the LAN/internet in my Peer2Peer program...
These are the relevant declarations..
Code:
Dim FileBuffer() As Byte, LenOfFile As Double
Dim FileCount As Double
Dim FileID As Integer
Dim EndOfFile As Boolean
Dim SentFileName As String
This is the send filecode,
Code:
Private Sub cmdSendFile_Click()
With CommonDialog1 'let the user select file
.ShowOpen
If .FileName = "" Then Exit Sub
SentFileName = GetFileName(.FileName)
FileID = FreeFile
Open .FileName For Binary Access Read As #FileID
LenOfFile = LOF(FileID)
ReDim FileBuffer(1 To LenOfFile)
Get #FileID, 1, FileBuffer
Close #FileID
FileCount = 0
SendFilePacket 'start the transfer
End With
End Sub
Private Sub SendFilePacket()
Dim PacketSize As Long
Dim DefaultPacketSize As long
Dim lMsg As Long
Dim lOffSet As Long
Dim oBuf() As Byte
Dim Chunk() As Byte
'find out if we use a packetsize of 512 or if we need a smaller one
DefaultPacketSize = 512
PacketSize = DefaultPacketSize
If (FileCount + 1) * PacketSize > LenOfFile Then PacketSize = LenOfFile - (FileCount * PacketSize)
Debug.Print PacketSize
ReDim Chunk(1 To PacketSize)
Chunk() = MidB(FileBuffer, FileCount * DefaultPacketSize + 1, PacketSize)
'here i send the packet so the other computer
lOffSet = NewBuffer(oBuf)
lMsg = MsgSendPicture
AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffSet
AddStringToBuffer oBuf, SentFileName, lOffSet
AddDataToBuffer oBuf, PacketSize, LenB(PacketSize), lOffSet
AddDataToBuffer oBuf, Chunk(1), PacketSize, lOffSet
dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
If FileCount * DefaultPacketSize + PacketSize + 1 > LenOfFile Then
EndOfFile = True
Exit Sub
End If
FileCount = FileCount + 1
End Sub
If remove the "send code" part above and instead save the file back on the local drive using the Put command the file is correct.. But if i retrive with the sendcode (the code below) the file is not correct...
Code:
Private Sub DirectPlay8Event_Receive(dpnotify As DxVBLibA.DPNMSG_RECEIVE, fRejectMsg As Boolean)
'process what msgs we receive.
Dim lMsg As Long, lOffSet As Long
Dim dpPeer As DPN_PLAYER_INFO, sName As String
Dim oBuf() As Byte
Dim PacketSize As Long
Dim Chunk() As Byte
With dpnotify
GetDataFromBuffer .ReceivedData, lMsg, LenB(lMsg), lOffSet
Select Case lMsg
Case MsgSendPicture 'we retrive the SendPicture command from the other client
SentFileName = GetStringFromBuffer(.ReceivedData, lOffSet) 'get the filename
GetDataFromBuffer .ReceivedData, PacketSize, LenB(PacketSize), lOffSet 'get the length of the packet
Debug.Print PacketSize
ReDim Chunk(1 To PacketSize)
GetDataFromBuffer dpnotify.ReceivedData, Chunk(1), PacketSize, lOffSet 'get the packet
If FileID = 0 Then 'if fileid = 0 then no file is open, lets open one
FileID = FreeFile
Open AppPath & "\Sent_" & SentFileName For Binary Access Write As #FileID
End If
Put #FileID, , Chunk() 'save the packet
lOffSet = NewBuffer(oBuf) 'let let the sender know we have retrived the packet
lMsg = MsgRecivedFilePart
AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffSet
dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
Case MsgRecievedFilePart 'The packets have ben recieved by the reciver
If EndOfFile = False Then
SendFilePacket 'if the file isnt finished yet, lets send another 512 bytes of data
Else 'the file is ended, lets send that to the reciving client
EndOfFile = False
FileCount = 0
lOffSet = NewBuffer(oBuf)
lMsg = MsgEOF
AddDataToBuffer oBuf, lMsg, LenB(lMsg), lOffSet
dpp.SendTo DPNID_ALL_PLAYERS_GROUP, oBuf, 0, DPNSEND_NOLOOPBACK Or DPNSEND_GUARANTEED
End If
Case MsgEOF 'the recieving client get a end of file message from the sender
Close #FileID 'lets close the file.
End Select
End With
End Sub
Ok, since it works to save the file localy it must be the netcode or the reciving part that is not correct. But i cant just figure out what..
Anyone that is good on sending files/binary data over the network?
I've opened up txt files that i sent over the LAn with this code, and the bytelenth of the files seems correct, but its the first byte (and maybe also the last) of each packet that is incorrect..
sorry for this long thread, but i really dont know what todo..
Thansk in advance, Anders
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement