正文

聊天室和对话系统2005-10-28 20:13:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/stef/6400.html

分享到:

在一个单位内部或通过广域协议(如X.25)互联的行业内部都有几十或上万台计算机互联,用Intranet虽然可以建立聊天室,但实现点对点实时对话却比较困难。本人用Winsock和VB自制了一套聊天室和对话系统,特拿来供同行们参考。     一·Winsock的主要属性、事件和方法     Winsock是不可见控件,控件文件名是MSWINSCK.OCX,全称为Mcirosoft winsock control,使用时要将此控件调入工具箱。    1·属性:①Protocol=0       //使用TCP协议;              ②RemoteHost     //准备连接远程机的IP地址              ③RemotePort     //连接远程机的IP端口号 (1024—65535之间)              ④LocalPort       //本地机监听IP端口号必须与呼叫机端口号相同2·方法:①connect         //申请连接远程机          ②listen           //设置监听          ③accept          //建立实际连接          ④senddata        //发送数据          ⑤getdata          //接收数据          ⑥close            //关闭连接3·事件:①connectionrequest    //一方请求连接时另一方产生          ②connect            //一方机接受连接时另一方产生          ③close              //一方机关闭连接时另一方产生          ④dataArrival         //一方发送数据另一方产生          ⑤error              //请求连接失败时产生二·制作方法⑴ 在一工程中添加两个表单form1(模拟客户端)、form2(模拟服务器端)。form1中装入控件:    控件名          主要属性         用      途VB.Form form1caption=”雷萌聊天室”controlbox=0 ‘False模拟客户机表单VB.Textbox text1multiline=-1    ‘Truescrollbars=3    ‘Bath用于输入发往聊天室的信息VB.Textbox text2locked=-1     ‘Truemultiline=-1    ‘Truescrollbars=3    ‘Bath显示从聊天室发来的信息VB.Combobox combo1text=”10.84.234.11”  ‘任定默认地址放入常用的地址VB.Commandbutton comm1                caption=”退出”最小化form1VB.Commandbutton comm2                    caption=”连接”请求与输入的地址连接VB.Commandbutton send                    caption=”发送”发送Text1中的内容VB.Label label1caption=“请在此输入发表的信息”Text1的框标VB.Label label2caption=“聊天室或对方的信息”Text2的框标VB.Label label3caption=”等待连接”显示连接状态信息VB.Label label4caption=”聊天室或对方地址”用于指示Combo1VB.Label label5caption=”操作:选地址连接,连接成功看到聊天室内容后再输信息发送”操作说明VB.Timer timer1interval=6000;   enabled=false防止连接超时MSWinsocklib.winsock a   用于数据传输    form2中装入控件:    控件名          主要属性         用      途VB.Form form2caption=”接收信息”controlbox=0 ‘False模拟客户机表单VB.Commandbutton                     command1caption=”返回”隐含Form2窗口VB.Commandbutton                     command2caption=”对话”点对点会话时用此直接启动Form1VB.Textbox text1locked=-1     ‘Truemultiline=-1    ‘Truescrollbars=3    ‘Bath存放聊天或对话内容VB.Label label1caption=”接收的信息”Text1的框标MSWinsocklib.Winsock a   用于监听MSWinsocklib.Winsock b   用于传送聊天信息  ⑵ 在Form1的各控件事件中加入如下代码:Dim flag As Boolean     注释:连接状态变量  Private Sub a_Connect()    flag = TrueEnd Sub  Private Sub a_DataArrival(ByVal bytesTotal As Long)    Dim i As String    a.GetData i    Label3.Caption = "连接成功!"    Comm2.MousePointer = 0    Form1.MousePointer = 0    Timer1.Enabled = False    If i = Chr(0) Then      Text2.Text = "你是今天第一个进入本聊天室的客户。" + Chr(13) + Chr(10)    Else      Text2.Text = Text2.Text + i    End If    Text2.SelStart = Len(Text2.Text)    Send.MousePointer = 0    Combo1.Enabled = False    Comm2.Caption = "断开连接"    Text1.SetFocusEnd Sub  Private Sub a_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)    flag = False    Timer1.Enabled = False    Comm2.MousePointer = 0    Form1.MousePointer = 0    MsgBox "网络连接失败 !"    Label3.Caption = "等待连接"    Combo1.Enabled = True    Combo1.SetFocus    a.Close    Comm2.Caption = "连接"End Sub  Private Sub Comm1_Click()    a.Close       注释:关闭连接    Form1.WindowState = 1End Sub  Private Sub Comm2_Click()If Comm2.Caption = "断开连接" Then   a.Close   Comm2.Caption = "连接"   Label3.Caption = "等待连接"   Combo1.Enabled = True   Timer1.Enabled = False   Comm2.MousePointer = 0   Form1.MousePointer = 0Else   Text2.Text = ""   Label3.Caption = "正在连接.."   Comm2.MousePointer = 11   Form1.MousePointer = 11   Timer1.Enabled = True   flag = False   a.Protocol = sckTCPProtocol   a.RemoteHost = Combo1.Text   a.RemotePort = 3000   a.ConnectEnd IfEnd Sub  Private Sub Form_DblClick()If MsgBox("关闭本聊天室! 确认吗?", 36, "退出系统") = 6 ThenEndElseForm1.WindowState = 1End IfEnd Sub  Private Sub Form_Load()If App.PrevInstance ThenMsgBox "本系统已经加载,请看任务拦!", 48, "提示"EndEnd Ifflag = FalseLoad Form2    ‘读入form2进入监听End Sub  Private Sub Send_Click()    Dim S As String    On Error GoTo ffff     ‘防止链路中断    Send.MousePointer = 11    If Right(Text1.Text, 1) <> Chr(10) Then        S = Text1.Text + Chr(13) + Chr(10)    Else        S = Text1.Text    End If    If flag Then        a.SendData S    End If    Exit Subffff:MsgBox "连接中断!", 48, "提示"a.CloseSend.MousePointer = 0Comm2.Caption = "连接"Label3.Caption = "等待连接"Combo1.Enabled = TrueComm2.MousePointer = 0Form1.MousePointer = 0Exit SubEnd Sub  Private Sub Timer1_Timer()  flag = False  Timer1.Enabled = False  Comm2.MousePointer = 0  Form1.MousePointer = 0  MsgBox "网络连接失败(超时) !"  Label3.Caption = "等待连接"  Combo1.Enabled = True  Combo1.SetFocus  a.Close  Comm2.Caption = "连接"End Sub  ⑶ 在Form2的各控件事件中加入如下代码:Const maxn = 200   ‘最大同时连接本机的客户数Dim user(maxn) As Boolean  Private Sub Command1_Click()  Form2.HideEnd Sub  Private Sub Command2_Click()Load Form1Form1.ShowEnd Sub  Private Sub Form_Load()       Dim str1 As String    Form2.Caption = "雷萌通信软件"    注释:winsock控件 a 作为服务器程序监听    a.LocalPort = 3000    a.ListenEnd Sub  Private Sub a_ConnectionRequest(ByVal requestID As Long)    Dim i As Long      For i = 1 To maxn  ‘当一客户请求时给启动一Winsock控件标志号        If Not user(i) Then            user(i) = True            Exit For        End If      Next i    If i > maxn Then        Exit Sub    End If   Load b(i)            ‘当一客户请求时启动一Winsock控件   b(i).Accept requestID   注释:实际建立连接   If Text1.Text = "" Then   注释:发送数据      b(i).SendData Chr(0)   Else      b(i).SendData Text1.Text   End If   Form2.ShowEnd Sub  Private Sub s_Close(Index As Integer)   b(Index).Close      注释:关闭连接   Unload b(Index)     注释:卸载 一个WinSock 控件   user(Index) = FalseEnd Sub  Private Sub b_DataArrival(Index As Integer, ByVal bytesTotal As Long)   Dim str As String   Dim i As Long    b(Index).GetData str    Text1.Text = Text1.Text + str   For i = 1 To maxn    If user(i) Then    b(i).SendData str    End If   Next iEnd Sub三·运行本程序在VB6.0中编译通过,运行后最小化到任务栏上,也可以用API的Shell_Notifyicon 函数做入右下角的指示器栏中常驻内存。你可以在网络中用一个固定的机器地址作为聊天讨论室,其他用户都选该机地址连接进入该室聊天或讨论。各用户也可选各自熟悉的地址进行连接对话,双击form1空白处从内存中撤出系统。根据同样的原理可以制作电子邮件系统。  

阅读(2569) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册