| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖, 1 人收藏
标题:一个用winsock实现的TCP会话程序
只看楼主 加入收藏
chenwind17
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-25
收藏(1)
 问题点数:0 回复次数:0 
一个用winsock实现的TCP会话程序

以下为一个用winsock实现的TCP会话程序,与大家分享
'winsock 可以在部件中找到
Option Explicit
'Client Program
'text2 : send textbox
'text3 : receive textbox
'text4 : remote host port number
'text1 : remote host name
'tcpstate : show current state of winsock
'label1 : "Send data"
'label2 : "Receive data"
'command1 : "Exit"
'command2 : "Connect remote host"
'command3 : "Send"

Private Sub ShowState(ByVal i As Integer)
Select Case i
Case 0
TcpState = "sckClosed"
Case 1
TcpState = "sckOpen"
Case 2
TcpState = "sckListening"
Case 3
TcpState = "sckConnectionPending"
Case 4
TcpState = "sckResolvingHost"
Case 5
TcpState = "sckConnection"
Case 6
TcpState = "sckConnected"
Case 7
TcpState = "sckClosing"
Case 8
TcpState = "sckError"
Case Else

End Select
End Sub
Private Sub Command1_Click()
End
End Sub

Private Sub TcpState_Click()
ShowState (Winsock1.State)
End Sub

Private Sub text1_change()
If Text1 = "" Then
Else
Winsock1.RemoteHost = Text1
End If
End Sub

Private Sub form_load()
Text2.Visible = False
Text3.Visible = False
Label1.Visible = False
Label2.Visible = False
Command3.Visible = False
Winsock1.RemoteHost = "servername"
Text4.MaxLength = 5
End Sub

Private Sub command2_click()
'If Winsock1.State <> sckClosed Then 'thest three sentences can be replaced by "on error resume next"
'Winsock1.Close
'End If
On Error Resume Next 'Without it the program is not so stable! So it is crutial! Experience yourself!
If Text1 = "" Then
MsgBox "Please input remote host name of IP address first!", , "RemoteHost"
Text1.SetFocus
ElseIf Text4 = "" Then
MsgBox "Please input remote host port first!", , "RemotePort"
Text4.SetFocus
Else
Winsock1.Connect
End If
End Sub

Private Sub winsock1_close()
Winsock1.Close
End
End Sub

Private Sub winsock1_connect()
Text2.Visible = True
Text3.Visible = True
Label1.Visible = True
Label2.Visible = True
Command2.Enabled = False
Command3.Visible = True
Text3.Enabled = False
End Sub

Private Sub command3_click()
Winsock1.SendData Text2
Text2 = ""
Text2.SetFocus
End Sub

Private Sub winsock1_dataarrival(ByVal DataBytes As Long)
Dim str1$
Winsock1.GetData str1
Text3 = str1
End Sub

Private Sub text4_change()
If Val(Text4) > 65535 Then
MsgBox Port number must less than 65536!", , "Remoteport"
Text4 = ""
Text4.SetFocus
ElseIf Val(Text4) = 0 Then

Else
Winsock1.RemotePort = Val(Text4)
End If
End Sub

Private 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)
MsgBox "Error Number:" & Number & ", " & Description
End Sub

以下为服务器端程序
'command1 结束按钮
'command2 发送按钮
'text1 发送textbox 输入信息按发送键发送给客户端
'text2 接受textbox 接受客户端信息
'winsock1 is the name of 控件

Option Explicit
'Server Program
Private Sub Command1_Click()
End
End Sub

Private Sub form_load()
Text1.Visible = False
Text2.Visible = False
Label1.Visible = False
Label2.Visible = False
Command2.Enabled = False
Winsock1.LocalPort = 12345
Winsock1.Listen
End Sub

Private Sub winsock1_connectionrequest(ByVal id As Long)
Text1.Visible = True
Text2.Visible = True
Label1.Visible = True 'label1.caption 为发送
Label2.Visible = True 'label2.caption 为接受
Command2.Enabled = True
Text2.Enabled = False
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept id
End Sub

Private Sub winsock1_close()
Winsock1.Close
End
End Sub

Private Sub winsock1_dataarrival(ByVal DataBytes As Long)
Dim str1$
Winsock1.GetData str1
Text2 = str1
End Sub

Private Sub Command2_click()
Winsock1.SendData Text1
Text1 = ""
Text1.SetFocus
End Sub

'以下为出错处理程序:

Private 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)
CancelDisplay = False
MsgBox "Error Number:" & Number & ",Error Details:" & Description & ",source:" & Source
End Sub


搜索更多相关主题的帖子: TCP winsock 会话 
2006-09-28 20:00
快速回复:一个用winsock实现的TCP会话程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.092401 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved