找了一个例子给你看看客户端程序: '客户端程序,by hahahawk Option Explicit Dim buffer(8191) As Byte Dim flen As Long '文件长度 Dim flag As String '标志:要传输的是文件的长度还是文件本身 Dim trans_num As Integer Dim fileNum As Integer Private Sub cmdstart_Click() Winsock1.Connect End Sub Private Sub Form_Load() Dim i As Integer Dim xFileName As String xFileName = App.Path & "\..\filecopy\a1.exe" fileNum = FreeFile Open xFileName For Binary As #fileNum flen = LOF(fileNum)
Label1.Caption = Label1.Caption & vbCrLf & "要传输的文件长度为" & Str(flen) Winsock1.RemoteHost = "wqnotebook" Winsock1.RemotePort = 2002 flag = "head" trans_num = 0 End Sub Private Sub Form_Unload(Cancel As Integer) Close #fileNum End Sub Private Sub Winsock1_Connect() cmdstart.Enabled = False Label1.Caption = Label1.Caption & vbCrLf & "已经连接上,开始发送" If flag = "head" Then trans_num = readblock(fileNum) Winsock1.SendData Str(trans_num)
End If End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim s1 As String Winsock1.GetData s1, vbString Debug.Print s1 If s1 = "ok" Then If flag = "file" Then If trans_num > 0 Then Winsock1.SendData buffer
Else
If trans_num = 1024 * 8 Then trans_num = readblock(fileNum) If trans_num > 0 Then Winsock1.SendData Str(trans_num) Else Label1.Caption = Label1.Caption & vbCrLf & "整个文件传送完毕" End If Else Label1.Caption = Label1.Caption & vbCrLf & "整个文件传送完毕" End If End If Else If Winsock1.State <> sckClosed Then Winsock1.Close Label1.Caption = Label1.Caption & vbCrLf & "服务器无法接受" cmdstart.Enabled = True End If
End Sub Private Sub Winsock1_SendComplete() If flag = "file" Then Label1.Caption = Label1.Caption & vbCrLf & "发送块完毕" flag = "head" Else Label1.Caption = Label1.Caption & vbCrLf & "交换块大小" & trans_num flag = "file" End If End Sub Public Function readblock(fileid As Integer) Dim i As Integer i = 0 Do While Not EOF(fileid) If i = 0 Then Debug.Print i If i = 4095 Then Debug.Print i If i = 4096 Then Debug.Print i If i = 4097 Then Debug.Print i Get #fileid, , buffer(i) i = i + 1 If i = 1024 * 8 Then Exit Do Loop readblock = i End Function 服务端程序: '服务端程序,by hahahawk Option Explicit Dim flag As String Dim flen As Long Dim trans_num As Integer Dim fileNum As Integer Private Sub Form_Load() Winsock1.LocalPort = 2002 Winsock1.Listen flag = "head" Dim xFileName As String fileNum = FreeFile() xFileName = App.Path & "\..\filecopy\a2.exe" Open xFileName For Binary Access Write Lock Write As #fileNum End Sub Private Sub Form_Unload(Cancel As Integer) If Winsock1.State <> sckClosed Then Winsock1.Close Close #fileNum 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) Dim buffer() As Byte, s1 As String If flag = "head" Then Winsock1.GetData s1, vbString Label1.Caption = Label1.Caption & vbCrLf & "客户方准备发送长度为" & s1 & "的块" trans_num = Val(s1) flag = "file" Winsock1.SendData "ok"
Else ReDim buffer(trans_num) Winsock1.GetData buffer, vbArray + vbByte flag = "head" Label1.Caption = Label1.Caption & vbCrLf & "长度为" & Str(trans_num) & "的块接受完毕" Dim i As Integer i = 0 For i = 0 To trans_num - 1 Put #fileNum, , buffer(i) Next Winsock1.SendData "ok" End If End Sub Private Sub Winsock1_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 Description End Sub
'最简单的一个例子,建两个工程,一个先启动server. Private Sub Form_Load() Winsock1.LocalPort = 6001 Winsock1.Listen End SubPrivate 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) Dim str As String Winsock1.GetData str Debug.Print str'这里就是接收到的信息了 End Sub'再启动客户端. Private Sub Command1_Click() Winsock1.SendData "F4 44 04 F9"'发送数据 End SubPrivate Sub Form_Load() Winsock1.RemoteHost = "192.168.0.1" Winsock1.RemotePort = 6001 Winsock1.Connect End Sub'按客户的发送按钮,server端就可收到信息了
MSDN上有用法
Option Explicit
Dim buffer(8191) As Byte
Dim flen As Long '文件长度
Dim flag As String '标志:要传输的是文件的长度还是文件本身
Dim trans_num As Integer
Dim fileNum As Integer Private Sub cmdstart_Click() Winsock1.Connect
End Sub Private Sub Form_Load()
Dim i As Integer
Dim xFileName As String
xFileName = App.Path & "\..\filecopy\a1.exe"
fileNum = FreeFile
Open xFileName For Binary As #fileNum
flen = LOF(fileNum)
Label1.Caption = Label1.Caption & vbCrLf & "要传输的文件长度为" & Str(flen)
Winsock1.RemoteHost = "wqnotebook"
Winsock1.RemotePort = 2002
flag = "head"
trans_num = 0
End Sub Private Sub Form_Unload(Cancel As Integer)
Close #fileNum
End Sub Private Sub Winsock1_Connect()
cmdstart.Enabled = False
Label1.Caption = Label1.Caption & vbCrLf & "已经连接上,开始发送"
If flag = "head" Then
trans_num = readblock(fileNum)
Winsock1.SendData Str(trans_num)
End If
End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s1 As String
Winsock1.GetData s1, vbString
Debug.Print s1
If s1 = "ok" Then
If flag = "file" Then
If trans_num > 0 Then Winsock1.SendData buffer
Else
If trans_num = 1024 * 8 Then
trans_num = readblock(fileNum)
If trans_num > 0 Then
Winsock1.SendData Str(trans_num)
Else
Label1.Caption = Label1.Caption & vbCrLf & "整个文件传送完毕"
End If
Else
Label1.Caption = Label1.Caption & vbCrLf & "整个文件传送完毕"
End If
End If
Else
If Winsock1.State <> sckClosed Then Winsock1.Close
Label1.Caption = Label1.Caption & vbCrLf & "服务器无法接受"
cmdstart.Enabled = True
End If
End Sub Private Sub Winsock1_SendComplete()
If flag = "file" Then
Label1.Caption = Label1.Caption & vbCrLf & "发送块完毕"
flag = "head"
Else
Label1.Caption = Label1.Caption & vbCrLf & "交换块大小" & trans_num
flag = "file"
End If
End Sub
Public Function readblock(fileid As Integer)
Dim i As Integer
i = 0
Do While Not EOF(fileid)
If i = 0 Then Debug.Print i
If i = 4095 Then Debug.Print i
If i = 4096 Then Debug.Print i
If i = 4097 Then Debug.Print i
Get #fileid, , buffer(i)
i = i + 1
If i = 1024 * 8 Then Exit Do
Loop
readblock = i
End Function
服务端程序: '服务端程序,by hahahawk
Option Explicit
Dim flag As String
Dim flen As Long
Dim trans_num As Integer
Dim fileNum As Integer Private Sub Form_Load()
Winsock1.LocalPort = 2002
Winsock1.Listen
flag = "head"
Dim xFileName As String
fileNum = FreeFile()
xFileName = App.Path & "\..\filecopy\a2.exe"
Open xFileName For Binary Access Write Lock Write As #fileNum
End Sub Private Sub Form_Unload(Cancel As Integer)
If Winsock1.State <> sckClosed Then Winsock1.Close
Close #fileNum
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)
Dim buffer() As Byte, s1 As String
If flag = "head" Then
Winsock1.GetData s1, vbString
Label1.Caption = Label1.Caption & vbCrLf & "客户方准备发送长度为" & s1 & "的块"
trans_num = Val(s1)
flag = "file"
Winsock1.SendData "ok"
Else
ReDim buffer(trans_num)
Winsock1.GetData buffer, vbArray + vbByte
flag = "head"
Label1.Caption = Label1.Caption & vbCrLf & "长度为" & Str(trans_num) & "的块接受完毕"
Dim i As Integer
i = 0
For i = 0 To trans_num - 1
Put #fileNum, , buffer(i)
Next Winsock1.SendData "ok"
End If
End Sub Private Sub Winsock1_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 Description
End Sub
服务器端——侦听(listen函数)——收到客户端发来信息——返回确认信息
客户端——向服务器发送信息(senddata函数)——收到服务器返回的确认——发送数据
看看帮助自己试试,很简单的。
Winsock控件
服务器端——侦听(listen函数)—连接请求(ConnectionRequest事件)(Winsock1.Accept requestID连接)—收到客户端发来信息(DataArrival事件)(getdata收数据)——返回确认信息(senddata)
-客户端断开连接(close事件)(winsock1.close)--重新侦听(listen)客户端—连接(winsock.connect)—向服务器发送信息(senddata函数)——收到服务器返回的确认(DataArrival事件)(getdata收数据)——发送数据(senddat)--断开连接(winsock1.close)
Private Sub Form_Load()
Winsock1.LocalPort = 6001
Winsock1.Listen
End SubPrivate 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)
Dim str As String
Winsock1.GetData str
Debug.Print str'这里就是接收到的信息了
End Sub'再启动客户端.
Private Sub Command1_Click()
Winsock1.SendData "F4 44 04 F9"'发送数据
End SubPrivate Sub Form_Load()
Winsock1.RemoteHost = "192.168.0.1"
Winsock1.RemotePort = 6001
Winsock1.Connect
End Sub'按客户的发送按钮,server端就可收到信息了
注明:网络编程