作者:枫雪 文章来源:原创 更新时间:2008-3-25
今晚加的功能是多个TCP板与电脑通信,由于手上只有一块TCP板,所以我用VB做了两个客户端代替TCP板,现在任务主要是电脑上的VB服务端程序,所以用VB写的客户端代替TCP板也没关系,上图,图1是没连接的服务器界面,图2、图3是客户端的初始界面,代表TCP板1和TCP板2
图1
图2
图3
接着是服务端接收到两个客户端的连接和把01发到客户端1把02发到客户端2
下图是客户端接收到数据界面,如果客户端没连接上服务器,是显示连接,如果连上就显示发送,代表可以发数据到服务器
服务端代码,基本没变,就是加了个发送功能
Dim color(2) As Long
Dim cur As Integer
Public tmin As Integer
Public tmax As Integer
Public repet As Integer
Dim remain As Integer
Dim SoundFile As String
Dim Result As Long
Private Sub Command1_Click()
sock(1).SendData Text1.Text
End Sub
Private Sub Command2_Click()
sock(2).SendData Text2.Text
End Sub
Private Sub Form_Load()
Num = 50
locPort = 31660
tmin = 12
tmax = 26
repet = 5
Call init
End Sub
Sub init()
remain = 0
color(0) = &HFF00& '&H0000FF00&
color(1) = &HFF& '&H000000FF&
color(2) = &HFFFF& '&H0000FFFF&
sock(0).LocalPort = locPort
sock(0).Listen
For i = 1 To Num
Load sock(i)
sock(i).LocalPort = locPort + i
sock(i).Listen
Next
Label2.Caption = "本地IP: " & sock(0).LocalIP
Label3.Caption = "信息: 已连接" & cur & "/" & Num
End Sub
Private Sub sock_Close(Index As Integer)
sock(Index).Close
sock(Index).Listen
'Shape1.FillColor = color(2)
cur = cur - 1
'Labtem1 = ""
End Sub
Private Sub sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
sock(Index).Close
sock(Index).Accept requestID
cur = cur + 1
Label3.Caption = "信息: 已连接" & cur & "/" & Num
End Sub
Private Sub sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim s As String
sock(Index).GetData s ' DataFormat XX0YYY
Call reflec(s)
End Sub
Sub reflec(data As String)
Labtem1.Caption = data
End Sub
客户端代码,两个客户端基本一样,只是端口不一样
Private Sub Command1_Click()
If Command1.Caption = "连接" Then
Winsock1.Close
Winsock1.Connect
Else
Winsock1.SendData Text1.Text
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "连接"
rom = "127.0.0.1"
Port = 31661
Winsock1.RemoteHost = rom
Winsock1.RemotePort = Port
End Sub
Private Sub Winsock1_Close()
Command1.Caption = "连接"
End Sub
Private Sub Winsock1_Connect()
Command1.Caption = "发送"
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim s As String
Winsock1.GetData s ' DataFormat XX0YYY
Text2.Text = s
End Sub
文章评论(0条评论)
登录后参与讨论