您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> VB >> 辅导 >> 正文    
  用VisualBasic6.0编写客户服务器程序 【注册男护士专用博客】          

用VisualBasic6.0编写客户服务器程序

www.nanhushi.com     佚名   不详 

  Visual Basic6.0提供了Winsock控件,支持网络上两台计算机之间的通信。利用它,编程人员可以毫不费力地编写出TCP/IP客户/服务器程序。以下是用Visual Basic6.0的Winsock控件编写的网络聊天程序的实例。

一.客户程序的实现方法:

1.客户程序设置RemoteHost(远程主机)属性,指定运行服务器的主机名。

2.通过设置RemotePort属性,指定服务器程序的侦听端口。

3.客户程序使用Connect方法向服务器发送连接请求。

4.服务器若空闲,则接受客户的请求,客户程序发生Connect事件,然后可以用SendData方法发送数据。

5.客户程序收到数据时,产生DataArrival事件,在该事件中可以用GetData方法接收数据。

6.如果客户收到Close事件,用Close方法关闭连接。

以下是客户程序的编写过程:

1. 新建一个工程文件,在窗体中加上一个文本框Name为txtRecive,设置 MultiLine属性为True。设置ScrollBars属性为3-Both。

2. 加一标签,Caption为“您的大名:”,后面加一文本框Name为ClientName。

3. 加一标签,Caption为“您的性别:”,后面接一组合框Name为“xingbie“。组合框中加两个OptionButton,分别为Option1,Caption为“男”,Value为True,Option2,Caption为“女”。

4. 加文本框txtSent,设置MultiLine属性为True。

5. 加命令按钮cmdSent,Caption为“发送”, 再加一命令按钮cmdConnect,Caption为“连接”。

6. 加Winsock控件,Name为sckClient。

下面是客户程序的源代码:

Dim messIndex As Integer

Dim firsttime As Boolean

Dim recNumber As Integer

Dim strData As String

Private Sub cmdConnect_Click()

On Error GoTo MyError

sckClient.Connect

Exit Sub

MyError:

MsgBox "连接服务器出错!", vbOKOnly, "系统提示"

Exit Sub

End Sub

Private Sub cmdSent_Click()

Dim name As String

If txtSent.Text = "" Then

MsgBox "您想要说什么?", vbOKOnly, "系统提示"

Exit Sub

End If

If Option1.Value = True Then

name = clientname.Text + "先生: "

Else

name = clientname.Text + "小姐: "


End If

sckClient.SendData name + txtSent.Text

End Sub

Private Sub Form_Load()

sckClient.RemoteHost = "fdd" 注释:可以更改为你运行服务器的主机名

sckClient.RemotePort = 8888

cmdSent.Enabled = False

messIndex = -1

firsttime = True

recNumber = 0

End Sub

Private Sub sckClient_Close()

MsgBox "您使用的名字已经注册或服务器已关闭,请重新连接。"

End

End Sub

Private Sub sckClient_Connect()

Dim name As String

MsgBox "连接服务器成功!", vbOKOnly, "系统提示"

cmdConnect.Enabled = False

cmdSent.Enabled = True

If firsttime = True Then

If Option1.Value = True Then

name = clientname.Text + "先生"

Else

name = clientname.Text + "小姐"

End If

sckClient.SendData name

firsttime = False

End If

clientname.Enabled = False

If Option1.Value = True Then

Option2.Enabled = False

Option1.Enabled = False

Else

Option1.Enabled = False

Option2.Enabled = False

End If

End Sub

Private Sub sckClient_DataArrival(ByVal bytesTotal As Long)

sckClient.GetData strData

txtRecive.Text = txtRecive.Text & strData & vbCrLf

txtRecive.SelStart = Len(txtRecive.Text)

End Sub

Private Sub sckClient_Error(ByVal Number As Integer, _

Des cription As String, ByVal Scode As Long, _

ByVal Source As String, ByVal HelpFile As String, _

ByVal HelpContext As Long, CancelDisplay As Boolean)

sckClient.Close

cmdConnect.Enabled = True

cmdSent.Enabled = False

End Sub

二.服务器程序的实现方法:

1. 设置LocalPort属性,作为侦听端口。该值是一个整型值,但必须是其他TCP/IP程序没有使用过的值。

2. 使用Listen方法进入侦听状态,等待客户程序发出连接请求。

3. 收到客户的连接请求,服务器发生ConnectRequest事件,得到RequestID。

4. 服务器程序用Accept方法接受客户的连接请求。然后可以用SendData方法发送数据。

5. 服务器接受到数据时,发生DataArrival事件,在该事件中可以用GetData的方法接收数据。

6. 如果接收到Close事件,则用Close方法关闭TCP/IP连接。

以下是服务器程序的编写过程:

1. 新建一个工程文件,在窗体中加上一个文本框Name为txtRecive,设置 MultiLine属性为True。设置ScrollBars属性为3-Both。

2. 加一标签,Caption为“连接数”,后面接一个标签,Name为number,Caption为0,设置Appearance为1-3D,BorderStyle为1-Fixed Single。

3. 加一命令按钮,Name为clearmess,Caption为“清除消息”。

4. 加三个WinSock控件,Name分别为sckServer,sckBusy,sckListen。

下面是服务器程序的源代码:

Private MaxNumber As Integer

Dim curnumber As Integer

Dim chatname(50) As String

Dim firstmess(50) As Boolean

Private Sub clearmess_Click()

txtRecive.Text = ""

End Sub

Private Sub Form_Load()

MaxNumber = 50

curnumber = 0

For l = 0 To MaxNumber

firstmess(l) = True

Next l

For i = 1 To MaxNumber - 1

Load sckServer(i)

Next i

sckListen.LocalPort = 8888

sckListen.Listen

End Sub

Private Sub sckBusy_Close()

sckBusy.Close

End Sub

Private Sub sckBusy_DataArrival(ByVal bytesTotal As Long)

sckBusy.SendData "服务器忙,请稍后再连接!"

DoEvents

End Sub

Private Sub sckListen_ConnectionRequest(ByVal requestID As Long)

Dim i As Integer

For i = 0 To MaxNumber - 1

If sckServer(i).State = 0 Then

Exit For

End If

Next i

If sckServer(i).State = 0 Then

sckServer(i).Accept requestID

sckServer(i).SendData "欢迎您参加入网络聊天!"

curnumber = curnumber + 1

number.Caption = curnumber

Exit Sub


End If

sckBusy.Close

sckBusy.Accept requestID

End Sub

Private Sub sckListen_Error(ByVal number As Integer, _

Des cription As String, ByVal Scode As Long, _

ByVal Source As String, ByVal HelpFile As String, _

ByVal HelpContext As Long, CancelDisplay As Boolean)

sckListen.Close

sckListen.LocalPort = 8888

sckListen.Listen

End Sub

Private Sub sckServer_Close(Index As Integer)

Dim j As Integer

sckServer(Index).Close

firstmess(Index) = True

For j = 0 To MaxNumber - 1

If sckServer(j).State = 7 Then

sckServer(j).SendData chatname(Index) + "退出网络聊天"

DoEvents

End If

Next j

curnumber = curnumber - 1

number.Caption = curnumber

End Sub

Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)

Dim strData As String

Dim i As Integer

sckServer(Index).GetData strData

If firstmess(Index) = True Then

chatname(Index) = strData

firstmess(Index) = False

For m = 0 To 49

If (Index <> m) And (chatname(m) = strData) Then

sckServer(Index).Close

firstmess(Index) = True

curnumber = curnumber - 1

number.Caption = curnumber

Exit Sub

End If

Next m

strData = strData + "加入网络聊天"

End If

For i = 0 To MaxNumber - 1

If sckServer(i).State = 7 Then

sckServer(i).SendData strData

DoEvents

End If

Next i

txtRecive.Text = txtRecive.Text & "Index " & Index & " " & strData + vbCrLf

txtRecive.SelStart = Len(txtRecive.Text)

End Sub

Private Sub sckServer_Error(Index As Integer, ByVal number As Integer, _

Des cription As String, ByVal Scode As Long, ByVal Source As String, _

ByVal HelpFile As String, ByVal HelpContext As Long, _

CancelDisplay As Boolean)

sckServer(Index).Close

End Sub

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目

      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)                            【进男护士社区逛逛】
    姓 名:
    * 游客填写  ·注册用户 ·忘记密码
    主 页:

    评 分:
    1分 2分 3分 4分 5分
    评论内容:
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。