open yourfilename for binary as #fileNum i=1 while not Eof(0) if i<255 then read #filenum,i,value '//你查一下这个指令对不对。 buffer(i-1)=value i=i+1 endif if i=255 then Winsoct1.senddate buffer'//这一句要验正,很久了,我拿不定是传 的buffer(0)个还是整个buffer wend close #filename
这个问题最容易的解答就是使用 TransmitFile winsock API.
read #filenum,i,value ====>Get #filenum,i,value
我用Winsock控件一对多传输文件,Client向Server申请连接成功,但文件怎么传不过去,请各位高手指教我的代码哪错了。 Server端: Private Sub Form_Load() intMax = 0 SckServer(0).LocalPort = 9456 SckServer(0).Listen End SubPrivate Sub SckServer_Connect(Index As Integer) Dim g As Integer Dim value As Byte Dim buffer() As Byte Open App.Path & "\db1.mdb" For Binary As #1 g = 1 While Not EOF(0) If g < 255 Then Get #1, g, value buffer(g - 1) = value g = g + 1 End If If g = 255 Then SckServer(intMax).SendData buffer End If DoEvents Wend Close #1 End SubPrivate Sub SckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long) For winsock_count = 1 To intMax If SckServer(intMax).State <> sckClosed Then SckServer(intMax).Close End If Next winsock_count If Index = 0 Then intMax = intMax + 1 Load SckServer(intMax) SckServer(intMax).LocalPort = 0 SckServer(intMax).Accept requestID End If End SubClient端: Dim beconnected As BooleanPrivate Sub SckClient_Connect() beconnected = True MsgBox "已经连接到服务器", vbOKOnly End SubPrivate Sub SckClient_DataArrival(ByVal bytesTotal As Long) Dim buffer() As Byte Dim value As Byte Dim g As Integer If beconnected = True Then Open App.Path & "\db1.mdb" For Binary As #1 g = 1 While Not EOF(0) ReDim buffer(bytesTotal - 1) SckClient.GetData buffer, vbByte + vbArray DoEvents If g < 255 Then value = buffer(g - 1) Put #1, g, value g = g + 1 End If Wend Close #1 End If End SubPrivate Sub SckClient_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) MsgBox "连接失败,请重试", vbOKOnly End End Sub
CLIENT端。
其中,Senddata和Getdata文件我不会。
希望大哥帮帮忙,如分不够,我将另开论坛加分,拜托!
我的E-mail是[email protected]或[email protected]
晚上5:00以后请发[email protected]
1。建一缓冲[255],文件用二进制打开
2。用一循环如:While not Eof(0)
.....'//读文件入缓冲
Winsoct1.send buffer‘//注意 :二进制的发送和字符的发送有所不同
(字符是以ASCII形式发送)buffer是String
你要用二进制发送,要变一下,这里我记不起了
你可自己试一下
doevents
wend
i=1
while not Eof(0)
if i<255 then
read #filenum,i,value '//你查一下这个指令对不对。
buffer(i-1)=value
i=i+1
endif
if i=255 then Winsoct1.senddate buffer'//这一句要验正,很久了,我拿不定是传
的buffer(0)个还是整个buffer
wend
close #filename
Server端:
Private Sub Form_Load()
intMax = 0
SckServer(0).LocalPort = 9456
SckServer(0).Listen
End SubPrivate Sub SckServer_Connect(Index As Integer)
Dim g As Integer
Dim value As Byte
Dim buffer() As Byte
Open App.Path & "\db1.mdb" For Binary As #1
g = 1
While Not EOF(0)
If g < 255 Then
Get #1, g, value
buffer(g - 1) = value
g = g + 1
End If
If g = 255 Then
SckServer(intMax).SendData buffer
End If
DoEvents
Wend
Close #1
End SubPrivate Sub SckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
For winsock_count = 1 To intMax
If SckServer(intMax).State <> sckClosed Then
SckServer(intMax).Close
End If
Next winsock_count
If Index = 0 Then
intMax = intMax + 1
Load SckServer(intMax)
SckServer(intMax).LocalPort = 0
SckServer(intMax).Accept requestID
End If
End SubClient端:
Dim beconnected As BooleanPrivate Sub SckClient_Connect()
beconnected = True
MsgBox "已经连接到服务器", vbOKOnly
End SubPrivate Sub SckClient_DataArrival(ByVal bytesTotal As Long)
Dim buffer() As Byte
Dim value As Byte
Dim g As Integer
If beconnected = True Then
Open App.Path & "\db1.mdb" For Binary As #1
g = 1
While Not EOF(0)
ReDim buffer(bytesTotal - 1)
SckClient.GetData buffer, vbByte + vbArray
DoEvents
If g < 255 Then
value = buffer(g - 1)
Put #1, g, value
g = g + 1
End If
Wend
Close #1
End If
End SubPrivate Sub SckClient_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)
MsgBox "连接失败,请重试", vbOKOnly
End
End Sub