代码如下: Const HostIP = "200.121.2.110" Const HostPort = 788 Const LocalPort = 788 Const GetCommand = "Get Share Path" Dim sLocalPC As StringPrivate Sub cmdSend_Click() If txtCmd <> "" Then Call SendCmd(txtCommand.Text) End SubPrivate Sub Command1_Click() On Error GoTo errConnWith Winsock1 If .State <> sckClosed Then .Close If txtServer <> "" Then .RemoteHost = txtServer .Connect End With Exit Sub errConn: MsgBox Err.Description End SubPrivate Sub Form_Load() Debug.Print Debug.Print "-----------VOD Client-------------"With Winsock1 .Protocol = sckTCPProtocol .RemoteHost = HostIP '缺省 .RemotePort = HostPort '.LocalPort = LocalPort sLocalPC = .LocalHostName End With txtServer = HostIP lblPort = Str(HostPort) StatusBar1.Style = sbrSimple cmdSend.Enabled = False End SubPrivate Sub Form_Unload(Cancel As Integer) 'Debug.Print "Unload frmVodClient" If Winsock1.State <> sckClosed Then Winsock1.Close Debug.Print "Close socket " + Str(HostPort) End If End SubPrivate Sub txtCommand_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then Call SendCmd(txtCommand.Text) End If End SubPrivate Sub Winsock1_Connect() StatusBar1.SimpleText = "Connected" 'With Winsock1 '.PeekData .LocalHostName + " Joined" 'txtShow.Text = txtShow + sLocalPC + ":Joined" + vbCrLf '.SendData sLocalPC + ":" + GetCommand 'txtShow.Text = txtShow + sLocalPC + ":" + GetCommand + vbCrLf 'End With cmdSend.Enabled = True End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long) 'Dim i As Long Dim sRecv As StringDebug.Print "Data Arrival"StatusBar1.SimpleText = "Received Data"With Winsock1 .GetData sRecv, vbString txtShow.Text = "Received" End WithEnd SubPrivate 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) StatusBar1.SimpleText = Str(Number) + Description Winsock1.Close End SubPrivate Sub Winsock1_SendComplete() StatusBar1.SimpleText = "Send Complete" End Sub Sub SendCmd(txtCmd As String)'Dim Larrfile() As ByteOn Error GoTo errSend'ReDim Larrfile(FileLen(txtCmd)) As Byte'Larrfile = txtCmdWinsock1.SendData txtCmdtxtShow.Text = txtShow + txtCmd + vbCrLfDebug.Print "SendCmd:" + txtCmdExit SuberrSend: MsgBox Err.Description End Sub
是不是代码太长了? 其实只要拷贝下来,放到form中看就很清楚了。
定主为BYVAL就成二进制了, 不过每次最多只能传8049 个字节
dim Byt() as byte redim byt(totalSize) winsock.getdata byt,vbbyte
port应该怎么设?对方Listen的是88
对方Client的是788。
如果对方listen是88,那么
winsock1.connect 要联的IP地址,88
Const HostIP = "200.121.2.110"
Const HostPort = 788
Const LocalPort = 788
Const GetCommand = "Get Share Path"
Dim sLocalPC As StringPrivate Sub cmdSend_Click()
If txtCmd <> "" Then Call SendCmd(txtCommand.Text)
End SubPrivate Sub Command1_Click()
On Error GoTo errConnWith Winsock1
If .State <> sckClosed Then .Close
If txtServer <> "" Then .RemoteHost = txtServer
.Connect
End With
Exit Sub
errConn:
MsgBox Err.Description
End SubPrivate Sub Form_Load()
Debug.Print
Debug.Print "-----------VOD Client-------------"With Winsock1
.Protocol = sckTCPProtocol
.RemoteHost = HostIP '缺省
.RemotePort = HostPort
'.LocalPort = LocalPort
sLocalPC = .LocalHostName
End With
txtServer = HostIP
lblPort = Str(HostPort)
StatusBar1.Style = sbrSimple
cmdSend.Enabled = False
End SubPrivate Sub Form_Unload(Cancel As Integer)
'Debug.Print "Unload frmVodClient"
If Winsock1.State <> sckClosed Then
Winsock1.Close
Debug.Print "Close socket " + Str(HostPort)
End If
End SubPrivate Sub txtCommand_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Call SendCmd(txtCommand.Text)
End If
End SubPrivate Sub Winsock1_Connect()
StatusBar1.SimpleText = "Connected"
'With Winsock1
'.PeekData .LocalHostName + " Joined"
'txtShow.Text = txtShow + sLocalPC + ":Joined" + vbCrLf
'.SendData sLocalPC + ":" + GetCommand
'txtShow.Text = txtShow + sLocalPC + ":" + GetCommand + vbCrLf
'End With
cmdSend.Enabled = True
End SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
'Dim i As Long
Dim sRecv As StringDebug.Print "Data Arrival"StatusBar1.SimpleText = "Received Data"With Winsock1
.GetData sRecv, vbString
txtShow.Text = "Received"
End WithEnd SubPrivate 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)
StatusBar1.SimpleText = Str(Number) + Description
Winsock1.Close
End SubPrivate Sub Winsock1_SendComplete()
StatusBar1.SimpleText = "Send Complete"
End Sub
Sub SendCmd(txtCmd As String)'Dim Larrfile() As ByteOn Error GoTo errSend'ReDim Larrfile(FileLen(txtCmd)) As Byte'Larrfile = txtCmdWinsock1.SendData txtCmdtxtShow.Text = txtShow + txtCmd + vbCrLfDebug.Print "SendCmd:" + txtCmdExit SuberrSend:
MsgBox Err.Description
End Sub
其实只要拷贝下来,放到form中看就很清楚了。
不过每次最多只能传8049 个字节
redim byt(totalSize)
winsock.getdata byt,vbbyte
真可笑。
改用SOAP了。