首先在部件中添加Microsoft winsock control 6.0。因为我只有VB精简版,没有此控件,也就直接写吧,可能哪里写错了,自己改改吧
(1)编写一个服务器应用程序:
窗体中加入一个winsock 控件。
'服务器程序代码
private sub command_click()
winsock1.senddate text2.text
text1.text = text1.text + chr(13)+chr(10)+text2.text
end sub
private sub form_load()
winsock1.localport =1002 '设定服务器端口
winsock1.listen '侦听是否有链接请求
end sub
private sub winsock1_connectionRequest(ByVal requestID as long)
winsock1.close '关闭侦听端口
winsock1.Accept requestID '接受请求
end sub
private sub winsock1_DateArrival(ByVal bytesTotal as long)
dim chart as string
winsock1.getDate chart '调用getDate 方法接收数据
text1.text = text1.text+chr(13)+chr(10)+chart
end sub
(2)上面创建了一个简单的服务器程序,还要创建个客户端应用程序,客户端窗口添加一个winsock .代码如下:
private sub command1_click()
winsock1.sendDate text2.text '发送数据
text1.text = text1.text +chr(13)+chr(10)+text2.text
end sub
private sub command2_click()
winsock1.connect '发送请求连接
end sub
private sub form_load() ’设置服务器主机名和端口,主机名可通过控制面板/网络/标识中查到
winsock1.remoteHost = “smx” '注意:请查阅你的主机名,自己改下
winsock1.remotePort = 1002
end sub
private sub winsock1_DateArrival(ByVal bytesTotal As long)
dim chart as string
winsock1.GetDate chart
text1.text = text1.text +chr(13)+chr(10)+chart
end sub