Use VB to implement chat and discussion rooms and point-to-point sessions

Source: Internet
Author: User

Tens or tens of thousands of computers are interconnected within a single organization or in an industry that is interconnected through a wide area protocol (such as X.25). Although chatting rooms can be established through an intranet, however, it is difficult to implement point-to-point real-time conversations. I used WinSock and VB to create a chat room and DIALOG System for reference by our colleagues.

I. Main Properties, events, and methods of WinSock

Winsock is an invisible control. The control file name is mswinsck. ocx, which is called mcirosoft Winsock control. You need to call this control into the toolbox.

1. properties: ① protocol = 0 // use the TCP protocol;

② Remotehost // prepare the IP address to connect to the remote machine

③ Remoteport // The IP port number connecting to the remote machine (between 1024-65535)

④ Localport // The IP Port Number of the Local Machine listening must be the same as that of the host machine

2. Method: ① connect // apply to connect to the remote machine

② Listen // set the listener

③ Accept // establish the actual connection

④ Senddata // send data

⑤ Getdata // receives data

⑥ Close // close the connection

3. Event: ① connectionrequest // the other party generates a connection request

② Connect // when one party accepts the connection, the other party generates

③ Close // when one party closes the connection, the other party generates

④ Dataarrival // One Party sends data generated by the other party

⑤ Error // generated when the request connection fails

Ii. Production Method

(1) Add two forms, form1 (simulated client) and form2 (simulated server), to the project ).

Add the control to form1:

Control name
Main attributes
Chinamoocs

VB. Form form1
Caption = "Lei Meng chat room"

Controlbox = 0'false
Simulate client form

VB. textbox text1
Multiline =-1 'true'

Scrollbars = 3' Bath
Used to enter information sent to the chat room

VB. textbox text2
Locked =-1 'true'

Multiline =-1 'true'

Scrollbars = 3' Bath
Displays information sent from a chat room

VB. ComboBox combo1
TEXT = "10.84.234.11"
Add common addresses

VB. commandbutton comm1
Caption = "quit"
Minimize form1

VB. commandbutton comm2
Caption = "connection"
Connection between the request and the input address

VB. commandbutton send
Caption = "send"
Send content in text1

VB. Label label1
Caption = "Enter the posted information here"
Text1

VB. Label label2
Caption = "chat room or other party's information"
Text2

VB. Label label3
Caption = "waiting for connection"
Display connection status information

VB. Label label4
Caption = "chat room or recipient's address"
Used to indicate combo1

VB. Label label5
Caption = "Operation: select an address to connect. After the connection succeeds, you can see the content of the chat room and then send the information"
Operation instructions

VB. Timer timer1
Interval = 6000; Enabled = false
Prevent connection timeout

Mswinsocklib. Winsock

For Data Transmission

Add the control to form2:

Control name
Main attributes
Chinamoocs

VB. Form form2
Caption = "Receive information"

Controlbox = 0'false
Simulate client form

VB. commandbutton command1
Caption = "return"
Implicit form2 window

VB. commandbutton command2
Caption = "dialog"
Use this command to directly start form1 during point-to-point sessions

VB. textbox text1
Locked =-1 'true'

Multiline =-1 'true'

Scrollbars = 3' Bath
Store chat or conversation content

VB. Label label1
Caption = "received information"
Text1

Mswinsocklib. Winsock

Used for listening

Mswinsocklib. Winsock B

Used to send chat Information

(2) Add the following code to the control events of form1:

Dim flag as Boolean Note: connection status variable

Private sub a_connect ()

Flag = true

End sub

Private sub a_dataarrival (byval bytestotal as long)

Dim I as string

A. getdata I

Label3.caption = "connection successful! "

Comm2.mousepointer = 0

Form1.mousepointer = 0

Timer1.enabled = false

If I = CHR (0) then

Text2.text = "you are the first customer to enter this chat room today. "+ 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 = "Disconnect"

Text1.setfocus

End 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 "network connection failed! "

Label3.caption = "waiting for connection"

Combo1.enabled = true

Combo1.setfocus

A. Close

Comm2.caption = "connection"
 

Private sub comm1_click ()

A. Close Note: Close the connection

Form1.windowstate = 1

End sub

Private sub comm2_click ()

If comm2.caption = "Disconnect" then

A. Close

Comm2.caption = "connection"

Label3.caption = "waiting for connection"

Combo1.enabled = true

Timer1.enabled = false

Comm2.mousepointer = 0

Form1.mousepointer = 0

Else

Text2.text = ""

Label3.caption = "Connecting .."

Comm2.mousepointer = 11

Form1.mousepointer = 11

Timer1.enabled = true

Flag = false

A. Protocol = scktcpprotocol

A. remotehost = combo1.text

A. remoteport = 3000

A. Connect

End if

End sub

Private sub form_dblclick ()

If msgbox ("close this chat room! Are you sure you want? ", 36," exit system ") = 6 then

End

Else

Form1.windowstate = 1

End if

End sub

Private sub form_load ()

If app. previnstance then

Msgbox "this system has been loaded. Please refer to the task blocking! ", 48," prompt"

End

End if

Flag = false

Load form2' reads form2 into the listener

End sub

Private sub send_click ()

Dim s as string

On Error goto ffff' prevents link interruption

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 sub

FFFF:

Msgbox "Connection interrupted! ", 48," prompt"

A. Close

Send. mousepointer = 0

Comm2.caption = "connection"

Label3.caption = "waiting for connection"

Combo1.enabled = true

Comm2.mousepointer = 0

Form1.mousepointer = 0

Exit sub

End sub

Private sub timer1_timer ()

Flag = false

Timer1.enabled = false

Comm2.mousepointer = 0

Form1.mousepointer = 0

Msgbox "network connection failed (timeout )! "

Label3.caption = "waiting for connection"

Combo1.enabled = true

Combo1.setfocus

A. Close

Comm2.caption = "connection"

End sub

(3) Add the following code to the control events of form2:

Const maxn = 100' maximum number of customers connecting to the local machine at the same time

Dim user (maxn) as Boolean

Private sub commandementclick ()

Form2.hide

End sub

Private sub command2_click ()

Load form1

Form1.show

End sub

Private sub form_load ()

Dim str1 as string

Form2.caption = "Lei Meng communication software"

Note: Winsock control a is used as a server program listener

A. localport = 3000

A. Listen

End sub

Private sub a_connectionrequest (byval requestid as long)

Dim I as long

For I = 1 to maxn' when a customer requests a Winsock Control flag number

If not user (I) then

User (I) = true

Exit

End if

Next I

If I> maxn then

Exit sub

End if

Load B (I) 'Start a Winsock Control when a customer requests

B (I). Accept requestid note: the actual connection is established.

If text1.text = "" Then Note: send data

B (I). senddata CHR (0)

Else

B (I). senddata text1.text

End if

Form2.show

End sub

Private sub s_close (index as integer)

B (INDEX). Close Note: Close the connection

Unload B (INDEX) Note: Uninstall a Winsock Control

User (INDEX) = false

End 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 I

End sub

Iii. Run

This program is compiled in VB6.0 and minimized to the taskbar after running. You can also use the shell_policyicon function of the API to add the resident memory in the indicator bar in the lower right corner. You can use a fixed machine address in the network as the chat Discussion Room. Other users choose the address to connect to the room for chat or discussion. Each user can also choose their own familiar address to connect to the conversation, double-click form1 blank space from the memory to withdraw from the system. You can create an email system based on the same principle.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.