home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD28151112000.psc / ChatServer / frmMain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2000-01-11  |  11.6 KB  |  352 lines

  1. VERSION 5.00
  2. Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "Simple VB Chat Server"
  6.    ClientHeight    =   2730
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   5685
  10.    Icon            =   "frmMain.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   2730
  15.    ScaleWidth      =   5685
  16.    Begin VB.TextBox txtIdle 
  17.       Enabled         =   0   'False
  18.       Height          =   285
  19.       Left            =   1080
  20.       MaxLength       =   2
  21.       TabIndex        =   5
  22.       Text            =   "5"
  23.       Top             =   2400
  24.       Width           =   375
  25.    End
  26.    Begin VB.CheckBox chkIdle 
  27.       BackColor       =   &H00C0C0C0&
  28.       Caption         =   "Idle Limit"
  29.       Height          =   255
  30.       Left            =   120
  31.       TabIndex        =   4
  32.       Top             =   2400
  33.       Width           =   975
  34.    End
  35.    Begin VB.CheckBox chkGuard 
  36.       BackColor       =   &H00C0C0C0&
  37.       Caption         =   "Guard Users"
  38.       Height          =   255
  39.       Left            =   120
  40.       TabIndex        =   3
  41.       ToolTipText     =   "Block user commands"
  42.       Top             =   2160
  43.       Width           =   1215
  44.    End
  45.    Begin VB.Timer tmrUpdate 
  46.       Interval        =   5000
  47.       Left            =   2280
  48.       Top             =   1080
  49.    End
  50.    Begin VB.ListBox lstUsers 
  51.       BackColor       =   &H00C0E0FF&
  52.       ForeColor       =   &H00800000&
  53.       Height          =   2010
  54.       Left            =   4320
  55.       TabIndex        =   2
  56.       ToolTipText     =   "Connected Users"
  57.       Top             =   0
  58.       Width           =   1335
  59.    End
  60.    Begin VB.CommandButton cmdListen 
  61.       Caption         =   "&Start"
  62.       CausesValidation=   0   'False
  63.       Height          =   495
  64.       Left            =   4680
  65.       TabIndex        =   1
  66.       Top             =   2160
  67.       Width           =   855
  68.    End
  69.    Begin VB.TextBox txtData 
  70.       BackColor       =   &H00C0E0FF&
  71.       ForeColor       =   &H00800000&
  72.       Height          =   2055
  73.       Left            =   0
  74.       Locked          =   -1  'True
  75.       MultiLine       =   -1  'True
  76.       ScrollBars      =   2  'Vertical
  77.       TabIndex        =   0
  78.       ToolTipText     =   "Incoming Chat data"
  79.       Top             =   0
  80.       Width           =   4335
  81.    End
  82.    Begin MSWinsockLib.Winsock ChatServer 
  83.       Left            =   0
  84.       Top             =   0
  85.       _ExtentX        =   741
  86.       _ExtentY        =   741
  87.       _Version        =   393216
  88.       Protocol        =   1
  89.       LocalPort       =   1000
  90.    End
  91.    Begin VB.Label Label1 
  92.       Caption         =   "minutes"
  93.       Height          =   255
  94.       Left            =   1560
  95.       TabIndex        =   6
  96.       Top             =   2400
  97.       Width           =   615
  98.    End
  99. Attribute VB_Name = "frmMain"
  100. Attribute VB_GlobalNameSpace = False
  101. Attribute VB_Creatable = False
  102. Attribute VB_PredeclaredId = True
  103. Attribute VB_Exposed = False
  104. Option Explicit
  105. Dim User(1 To 16) As New MyClients
  106. Private Sub ChatServer_DataArrival(ByVal bytesTotal As Long)
  107.     Dim X As Byte
  108.     Dim msg
  109.     'Retrieve sent data
  110.     ChatServer.GetData msg, vbString
  111.     'Update Idle Limit for user submitting data
  112.     For X = 1 To 15
  113.         If User(X).MyIP = ChatServer.RemoteHostIP And User(X).MyPort = ChatServer.RemotePort Then
  114.             'Update user's idle limit
  115.             User(X).Idle = Time
  116.             Exit For
  117.         End If
  118.     Next X
  119.     'Check for any user close command
  120.     If Mid(msg, 1, 4) = "Bye_" Then
  121.         For X = 1 To 15
  122.             'If they are the user on this port and have matching name
  123.             If User(X).MyPort = ChatServer.RemotePort And User(X).MyName = Mid(msg, 5) Then
  124.                 'They want to disconnect
  125.                 User(X).MyIP = ""
  126.                 User(X).MyPort = 0
  127.                 User(X).IsUsed = False
  128.                 'Let other users no he /she is gone
  129.                 msg = User(X).MyName & " has left the chat" & vbNewLine
  130.                 User(X).MyName = ""
  131.                 GoTo Broadcast
  132.             End If
  133.         Next X
  134.     End If
  135.     'Check  for new connections
  136.     If Mid(msg, 1, 8) = "Connect_" Then
  137.         'Try to find avaaiable slot
  138.         For X = 1 To 16
  139.             If X = 16 Then Exit For
  140.             If User(X).IsUsed = False Then
  141.                 'Found a slot
  142.                 User(X).IsUsed = True
  143.                 User(X).MyIP = ChatServer.RemoteHostIP
  144.                 User(X).MyPort = ChatServer.RemotePort
  145.                 User(X).Idle = Time
  146.                 User(X).MyName = Mid(msg, 9)
  147.                 msg = "Connected " & User(X).MyName & " as User#" & X & vbNewLine
  148.                 GoTo Broadcast
  149.             End If
  150.         Next X
  151.         
  152.         'User 6 is phantom.
  153.         'Is only used to let user know they
  154.         'could not connect.
  155.         User(16).IsUsed = True
  156.         User(16).MyIP = ChatServer.RemoteHost
  157.         User(16).MyPort = ChatServer.RemotePort
  158.         ChatServer.SendData "Could not allow " & _
  159.               "you to connect.  Too many users." & vbNewLine
  160.         'Close connection
  161.         ChatServer.SendData "Close_"
  162.         User(16).IsUsed = False
  163.         User(16).MyIP = ""
  164.         User(16).MyPort = 0
  165.         Exit Sub
  166.     End If
  167.     'Check for booting code
  168.     If Mid(msg, 1, 5) = "Drop_" Then
  169.             If chkGuard.Value = 1 Then
  170.                 'This should kill user boot code
  171.                 msg = "VB Chat Server Intercepted '" & msg & "' command!" & vbNewLine
  172.                 GoTo Broadcast
  173.             End If
  174.                 
  175.             'Find user to drop
  176.             For X = 1 To 15
  177.             'If they are the user on this port and have matching name
  178.             If User(X).MyName = Mid(msg, 5) Then
  179.                 ChatServer.RemotePort = User(X).MyPort
  180.                 ChatServer.SendData "Close_"
  181.                 'Disconnect them
  182.                 User(X).MyIP = ""
  183.                 User(X).MyPort = 0
  184.                 User(X).IsUsed = False
  185.                 'Let other users know he /she is gone
  186.                 msg = User(X).MyName & " has left the chat" & vbNewLine
  187.                 User(X).MyName = ""
  188.                 GoTo Broadcast
  189.             End If
  190.         Next X
  191.     End If
  192.     If Mid(msg, 1, 6) = "Color_" Then
  193.             If chkGuard.Value = 1 Then
  194.                 'This should kill the color code
  195.                 msg = "VB Chat Server Intercepted '" & msg & "' & command!" & vbNewLine
  196.                 GoTo Broadcast
  197.             End If
  198.     End If
  199.         
  200.     'Broad Cast messages to all users
  201. Broadcast:
  202.         'Don't add blank stuff to chatsever log
  203.     If Trim(msg) <> "" Then
  204.         txtData.Text = txtData.Text & msg
  205.     End If
  206.     X = 1
  207.     'Send message to known connected users only
  208.     Do
  209.         'Set to a valid chat client
  210.         If User(X).IsUsed = True Then
  211.             'Some1 connected as this user
  212.             ChatServer.RemoteHost = User(X).MyIP
  213.             ChatServer.RemotePort = User(X).MyPort
  214.             'Send the data
  215.             ChatServer.SendData msg
  216.         End If
  217.         X = X + 1
  218.     Loop Until X > 15
  219. End Sub
  220. Private Sub chkIdle_Click()
  221. 'Toggle Enabling of Idle text box
  222. txtIdle.Enabled = Not txtIdle.Enabled
  223. End Sub
  224. Private Sub cmdListen_Click()
  225.     'On port 1000
  226.     On Error Resume Next
  227.     ChatServer.Bind
  228.     cmdListen.Enabled = False
  229.     If Err <> 0 Then
  230.         'Probably already attemped bind method
  231.         'Address in use or address family not
  232.         'supported error is my guess
  233.         MsgBox "Error# " & Err.Number & vbNewLine & vbNewLine & Err.Description, vbExclamation, "Error"
  234.         MsgBox "Error. Aborting Program!", vbCritical, "Error"
  235.         Unload Me
  236.         End
  237.         Exit Sub
  238.     End If
  239.     txtData.Text = txtData.Text & vbNewLine & vbNewLine & "VB ChatServer@" & ChatServer.LocalIP & ":" & ChatServer.LocalPort
  240. 'Server works by broadcasting message
  241. 'May not be very efficient, but gets the job
  242. 'done for now
  243. End Sub
  244. Private Sub Form_Load()
  245. 'Resize chat window at runtime
  246. 'txtData.Move 0, 0, ScaleWidth, ScaleHeight - cmdListen.Height
  247. txtData.Text = "Copyright 1998-99. Kenneth Gilbert Jr." & vbNewLine & "All rights resereved." & vbNewLine & "VB Chat Server: Up to 15 users may chat at one time on this server!." & vbNewLine
  248. End Sub
  249. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  250. 'Disconnect all users conencted to VB Chat Server
  251. Dim X As Byte
  252. For X = 1 To 5
  253.     If User(X).IsUsed = True Then
  254.         'Boot the user
  255.         ChatServer.RemotePort = User(X).MyPort
  256.         ChatServer.SendData "Close_"
  257.     End If
  258.     'Kill MyClients object
  259.     Set User(X) = Nothing
  260. Next X
  261. 'Done.  Good bye!
  262. End Sub
  263. Private Sub lstUsers_DblClick()
  264.     Dim X As Byte
  265.     'Boot selected user
  266.       'Find user to drop
  267.         For X = 1 To 15
  268.             'If they are the user on this port and have matching name
  269.             If User(X).MyName = lstUsers.Text And User(X).IsUsed = True Then
  270.                 ChatServer.RemotePort = User(X).MyPort
  271.                 ChatServer.SendData "Close_"
  272.                 'They want to disconnect
  273.                 User(X).MyIP = ""
  274.                 User(X).MyPort = 0
  275.                 User(X).MyName = ""
  276.                 User(X).Idle = ""
  277.                 User(X).IsUsed = False
  278.                 Exit Sub
  279.             End If
  280.         Next X
  281.         'If we get here, we did not find the user.
  282.         txtData.Text = txtData.Text & vbNewLine & vbNewLine & "User was not found in MyClients!" & vbNewLine & vbNewLine
  283. End Sub
  284. Private Sub Text1_Change()
  285. End Sub
  286. Private Sub tmrUpdate_Timer()
  287.     Dim X As Byte
  288.     Dim strUsers As String
  289.     strUsers = "User_"
  290.     'Update user listing ever 5 seconds
  291.     'Update user listing
  292.     lstUsers.Clear
  293.     For X = 1 To 15
  294.         If User(X).IsUsed = True Then
  295.             'User connected here
  296.             lstUsers.AddItem User(X).MyName
  297.             strUsers = strUsers & User(X).MyName & ";"
  298.         End If
  299.     Next
  300.         
  301.    'Send users to all chat clients
  302.    For X = 1 To 15
  303.       If User(X).IsUsed = True Then
  304.          'User connected.
  305.          'Send a user list
  306.          ChatServer.RemoteHost = User(X).MyIP
  307.          ChatServer.RemotePort = User(X).MyPort
  308.          ChatServer.SendData strUsers
  309.       End If
  310.    Next
  311.    'Check for Idlers
  312.    If chkIdle.Value = 1 Then
  313.         For X = 1 To 15
  314.            If User(X).IsUsed = True Then
  315.              If DateDiff("s", User(X).Idle, Time) >= Int(txtIdle.Text * 60) Then
  316.                  'User connected.
  317.                  'Send a user list
  318.                  ChatServer.RemoteHost = User(X).MyIP
  319.                  ChatServer.RemotePort = User(X).MyPort
  320.                  ChatServer.SendData "Close_"
  321.              End If
  322.           End If
  323.         Next
  324.  End If
  325. End Sub
  326. Private Sub txtData_Change()
  327.     On Error Resume Next
  328.     'Move to bottom of text box
  329.     If Len(txtData.Text) > 63000 Then
  330.         'Getting to much information
  331.         '64k limit
  332.         txtData.Text = ""
  333.     End If
  334.     txtData.SelStart = Len(txtData.Text)
  335. End Sub
  336. Private Sub txtIdle_Change()
  337. 'Make sure they have entered numbers
  338. If IsNumeric(txtIdle) = False Then txtIdle = 5
  339. 'If they enter < 1 then set off
  340. If txtIdle.Text < 1 Then
  341.     chkIdle.Value = 0
  342.     txtIdle.Text = 0
  343.     txtIdle.Enabled = False
  344. End If
  345. End Sub
  346. Private Sub txtIdle_GotFocus()
  347. 'Select all text from start to end
  348. txtIdle.SelStart = 0
  349. txtIdle.SelLength = Len(txtIdle.Text)
  350. 'Done
  351. End Sub
  352.