用WINSOCK做多PC通信,服务器上有两个WINSOCK,一个是WINSOCK1(0),一个是WINSOCK1(1).现在就只用两台电脑连接,可就是连接不起来.生成EXE文件时,服务端说Winsock1.GetData strData错误.请各位看看这段代码错在哪里,就该怎么改.
服务端
Private Sub Form_Load()
Winsock1(0).LocalPort = 1001
Winsock1(0).Listen
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim sip As String
Dim I As Integer
sip = Winsock1(0).RemoteHostIP '获得登录者的IP地址
I = 1
Do While I <= Winsock1.ubound '检查是否已经有该地址的记录
If Winsock1(I).RemoteHostIP = sip Then '如有,不必加载新的控件
Winsock1(I).Accept requestID
Exit Sub
End If
I = I + 1
Loop
Load Winsock1(I) '否则,加载新的控件
Winsock1(I).Accept requestID
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim strData As String
Dim a(1 To 4) As String
For I = 1 To 4
Winsock1.GetData strData
a(I) = strData
Texti = a(I)
List1.AddItem = Texti.Text
Next I
End Sub
客户端
Private a(1 To 4) As String
Private Sub Command1_Click()
Dim i As Integer
For i = 1 To 4
a(i) = Text(i).Text
Winsock1.SendData a(i)
i = i + 1
Next i
End Sub
Private Sub Command2_Click()
On Error GoTo connecterror
Winsock1.Connect
If Winsock1.State = 7 Then
MsgBox ("连接成功")
End If
connecterror:
Dim a As String
a = "服务器未连接或程序未运行"
MsgBox (a)
End Sub
Private Sub Command3_Click()
Winsock1.Close
End Sub
Private Sub Form_Load()
Winsock1.RemoteHost = "192.168.1.100"
Winsock1.RemotePort = 1001
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
[此贴子已经被作者于2006-10-17 21:25:58编辑过]