⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmserver.frm

📁 简单的vb聊天软件
💻 FRM
字号:
VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmServer 
   Caption         =   "TCP Server => Download More : www.vivekpatel.cjb.net"
   ClientHeight    =   3465
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6000
   LinkTopic       =   "Form1"
   ScaleHeight     =   3465
   ScaleWidth      =   6000
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdSend 
      Caption         =   "Send"
      Default         =   -1  'True
      Height          =   495
      Left            =   4800
      TabIndex        =   2
      Top             =   2880
      Width           =   1095
   End
   Begin VB.TextBox txtmsg 
      Height          =   495
      Left            =   120
      TabIndex        =   1
      Top             =   2880
      Width           =   4575
   End
   Begin VB.ListBox lstMsg 
      Height          =   2595
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   5775
   End
   Begin MSWinsockLib.Winsock tcpServer 
      Left            =   240
      Top             =   2640
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
   End
End
Attribute VB_Name = "frmServer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'****************************************************
'Description : This is a server side chat application
'               which will accept request from ONE
'               client, and establish connection with
'               it to allow message passing (chating).
' Author     : Vivek Patel.
' Contact    : www.vivekpatel.cjb.net
'              vivek_patel9@rediffmail.com
'****************************************************


'****************************************
' CONTROLs Used in Chat System are
' 1) lstMsg (ListBox)=> To display easily chat messages.
' 2) txtMsg (TextBox)=> To type data to be send to client.
' 3) cmdSend (Command Btn) => Btn., which when press sends data to clients computer.
' 4) tcpServer (Winsock control) => (ActiveX control added from components) which is used to comunicate between computers on network.
'----------------------------------------
' Note : I have used same controls in both client and server and also used same name except.
'        tcpServer for Server winsock control and tcpClient for client winsock control.
'****************************************


Option Explicit


'It makes server ready to listen client request to
'Establish connection.
Private Sub Form_Load()
    Dim s As String
    'Enter in inputBox the service no. it can be any valid string.
    'eg : 1234
    s = InputBox("Enter the port to use (eg: 1234)")
    tcpServer.LocalPort = s  'This service no. is assigned to local port
    tcpServer.Listen         'Listen method makes server ready to listen clients request
End Sub

'It is used to set default focus to textbox.
'As unless form is visible it cannot set focus to any of its control
'and so i have used Activate event to set textbox focus
Private Sub Form_Activate()
    'to set default focus to textbox
    txtmsg.SetFocus
End Sub

'Event handles request made by client, that is each time client made
'request this event occurs.
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
    'check for tcpserver state if it is NOT CLOSED then accept request
    If tcpServer.State <> sckClosed Then tcpServer.Close
    
    'Accepts request made by client.
    tcpServer.Accept requestID
    lstMsg.AddItem "Client Connected" 'for displaying connected message...
End Sub

'Whenever data is recieved this event occurs.
'Thus we can retrieve data just by invoking "GetData" method within it.
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
    Dim strdata As String
    tcpServer.GetData strdata 'storing recieved data in strdata
    lstMsg.AddItem strdata 'displaying this data by adding it into listbox
End Sub


'command btn when pressed will send text message to client
'Its default property is set to True (which means its code
'will executed on pressing enter that is here will send data
'on just pressing enter.)
Private Sub cmdSend_Click()
    Dim strdata As String
    strdata = "Server : " & txtmsg.Text
    tcpServer.SendData strdata 'SendData method is used to send data.
    lstMsg.AddItem strdata 'adding send data into listbox to display it.
    txtmsg.Text = "" 'Making textbox clear
End Sub


'Event occur when trying to close form
Private Sub Form_Unload(Cancel As Integer)
    tcpServer.Close
    
    MsgBox "Programmed By : Vivek Patel" & vbCrLf & _
            "Website : www.vivekpatel.cjb.net" & vbCrLf & _
            "Email : vivek_patel9@rediffmail.com", , "Simple Chat System"
End Sub





'****************************************************
'Description : This is a server side chat application
'               which will accept request from ONE
'               client, and establish connection with
'               it.
' Author     : Vivek Patel.
' Contact    : www.vivekpatel.cjb.net
'              vivek_patel9@rediffmail.com
'****************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -