服务器端:
- Private Sub Form_Load()
- tcpServer.LocalPort = 1001
- tcpServer.Listen
- frmClient.Show
- End Sub
- Private Sub sSendBtn_Click()
- tcpServer.SendData txtSendData.Text
- txtOutput.Text = txtOutput.Text + vbCrLf + " " + Str(Time()) + vbCrLf + " " + txtSendData.Text
- Dim strData1 As String
- txtSendData.Text = ""
- End Sub
- Private Sub tcpServer_DataArrival _
- (ByVal bytesTotal As Long)
- Dim strData As String
- tcpServer.GetData strData
- txtOutput.Text = txtOutput.Text + vbCrLf + " " + Str(Time()) + vbCrLf + " " + strData
- End Sub
- Private Sub tcpServer_ConnectionRequest _
- (ByVal requestID As Long)
- If tcpServer.State <> sckClosed Then _
- tcpServer.Close
- tcpServer.Accept requestID
- End Sub
客户端:
- Private Sub cmdConnect_Click()
- tcpClient.Connect
- End Sub
- Private Sub cSendBtn_Click()
- tcpClient.SendData txtSendData.Text
- txtOutput.Text = txtOutput.Text + vbCrLf + " " + Str(Time()) + vbCrLf + " " + txtSendData.Text
- txtSendData.Text = ""
- End Sub
- Private Sub Form_Load()
- tcpClient.RemoteHost = Ipaddr.Text
- tcpClient.RemotePort = 1001
- End Sub
- Private Sub tcpClient_DataArrival _
- (ByVal bytesTotal As Long)
- Dim strData As String
- tcpClient.GetData strData
- txtOutput.Text = txtOutput.Text + vbCrLf + " " + Str(Time()) + vbCrLf + " " + strData
- End Sub
其中,设置换行需要在方法中设置 。切记,切记!
参考资料:
VB使用Winsock控件.rar
项目文件:
net_shiyan.rar
备注:完成于2011-12-04,计算机网络实验。
阅读(1165) | 评论(0) | 转发(0) |