一个服务端和一个客户端
服务端传送1个文件给客户端
当传送文件完成后,服务端再发送一个消息给客户端
客户端更具服务器发送的消息内容做不同的事情可是客户端接收文件的时候 接收到的数据类型是byte,而接收消息时的数据类型是string这2个怎么判断,高手指导下,听说可以用数组,可从来没用过数组,希望高人解密传送文件代码如下:
Server:
Option ExplicitPrivate Sub Command1_Click()
Dim BytDate() As Byte
Dim FileName As String
Dim lngFile As Long
Dim i As Long
lngFile = FileLen(Text1.Text) \ 1024
Me.ProgressBar1.Min = 0
Me.ProgressBar1.Max = lngFile + 1
ProgressBar1.Value = 0
ReDim myfile(1023) As Byte
For i = 0 To lngFile
On Error GoTo abc
If i = lngFile Then ReDim myfile(FileLen(Text1.Text) - 2048 * i - 1) As Byte
abc:
Resume Next
Open Text1.Text For Binary As #1
Get #1, i * 1024 + 1, myfile
Close #1
Winsock1.SendData myfile
DoEvents
ProgressBar1.Value = ProgressBar1.Value + 1
Next i
If ProgressBar1.Value = ProgressBar1.Max Then MsgBox "发送完成"
End SubPrivate Sub Command2_Click()
Winsock1.SendData "OK" '像客户端发送1个消息
End SubPrivate Sub Form_Load()
Text1.Text = "D:\123.exe"
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = 2001
Winsock1.Listen
End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> 0 Then Winsock1.Close
Winsock1.Accept requestID
End Sub
============================================================================
Client:Option ExplicitPrivate Sub Form_Load()
With Winsock1
.Protocol = sckTCPProtocol
.RemoteHost = "127.0.0.1"
.RemotePort = 2001
.Connect
End With
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = "C:\abc.exe"
ReDim myFile(bytesTotal - 1)
Winsock1.GetData myFile
Open myPath For Binary As #1
myLong = FileLen(myPath)
Put #1, myLong + 1, myFile
Close #1
'接收服务器发的消息("OK")怎么弄,高手帮忙
'这里就不会写了
End Sub
===============================================================
服务端传送1个文件给客户端
当传送文件完成后,服务端再发送一个消息给客户端
客户端更具服务器发送的消息内容做不同的事情可是客户端接收文件的时候 接收到的数据类型是byte,而接收消息时的数据类型是string这2个怎么判断,高手指导下,听说可以用数组,可从来没用过数组,希望高人解密传送文件代码如下:
Server:
Option ExplicitPrivate Sub Command1_Click()
Dim BytDate() As Byte
Dim FileName As String
Dim lngFile As Long
Dim i As Long
lngFile = FileLen(Text1.Text) \ 1024
Me.ProgressBar1.Min = 0
Me.ProgressBar1.Max = lngFile + 1
ProgressBar1.Value = 0
ReDim myfile(1023) As Byte
For i = 0 To lngFile
On Error GoTo abc
If i = lngFile Then ReDim myfile(FileLen(Text1.Text) - 2048 * i - 1) As Byte
abc:
Resume Next
Open Text1.Text For Binary As #1
Get #1, i * 1024 + 1, myfile
Close #1
Winsock1.SendData myfile
DoEvents
ProgressBar1.Value = ProgressBar1.Value + 1
Next i
If ProgressBar1.Value = ProgressBar1.Max Then MsgBox "发送完成"
End SubPrivate Sub Command2_Click()
Winsock1.SendData "OK" '像客户端发送1个消息
End SubPrivate Sub Form_Load()
Text1.Text = "D:\123.exe"
Winsock1.Protocol = sckTCPProtocol
Winsock1.LocalPort = 2001
Winsock1.Listen
End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> 0 Then Winsock1.Close
Winsock1.Accept requestID
End Sub
============================================================================
Client:Option ExplicitPrivate Sub Form_Load()
With Winsock1
.Protocol = sckTCPProtocol
.RemoteHost = "127.0.0.1"
.RemotePort = 2001
.Connect
End With
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Static i As Long
Dim myFile() As Byte
Dim myLong As Double
Dim myPath As String
myPath = "C:\abc.exe"
ReDim myFile(bytesTotal - 1)
Winsock1.GetData myFile
Open myPath For Binary As #1
myLong = FileLen(myPath)
Put #1, myLong + 1, myFile
Close #1
'接收服务器发的消息("OK")怎么弄,高手帮忙
'这里就不会写了
End Sub
===============================================================
MessageId as long
BufferSize as long
Buffer() as byte
end type这时MessageId表示传送过来的是什么东西,而Buffer表示传送过来的数据,BufferSize表示数据的长度
如果用你这个
Private Type SendInfo
MessageId as long
BufferSize as long
Buffer() as byte
end type
这个应该怎么用啊,怎么加到我上面那程序里?写一个例子就行,服务器传送完成后发个OK的消息
客户端在接完后收到OK的消息就MSGBOX一下
你如果都这样那你是做什么的??真正变成coder抄写代码的机器???