Chat program based on VB Winsock Control

Source: Internet
Author: User

Today, we fully utilize network resources to implement chat programs in the LAN. it is actually a small control in VB-Winsock Control. Winsock uses the TCP and UDP protocols to connect the local computer to a remote computer and exchange data in real time before the connection ends. when using the TCP protocol, you must create a client application to identify the server name or IP address. the communication port of the application will monitor the messages sent by the other party at any time, which is a reliable connection guarantee for the system. once a connection occurs, either party can send and receive data through senddata and use getdata to separate its own data.

Here we need to create two applications, server applications and client applications. the server first creates a service port number (preferably greater than 2000), and the server application is in the working state. Make sure that the client application enters the listening state before establishing a connection, enter the normal communication status.
The following program mainly implements the real-time chat function:
1. Create a server and client interface.

 

 

 

 

 

 

 

 

 

 

2. This interface is successfully connected after running.

3. Implement the chat function and chat successfully.

The Code is as follows:

Server:

Private sub commandementclick ()
'Send
Winsock1.senddata text1.text
Text2.text = text2.text + CHR (13) + CHR (10) + CSTR (date) + CHR (32) + CSTR (time $ () + CHR (13) + CHR (10) + "I:" & text1.text
Text1.text = ""

End sub

Private sub form_load ()
'Port settings
Winsock1.localport = 2008
Winsock1.listen
Label1.caption = label1.caption + CSTR (winsock1.localip)
Label2.caption = label2.caption + CSTR (winsock1.localport)
End sub

Private sub text=keydown (keycode as integer, shift as integer)
'Press enter to send data
If keycode = 13 then
Text2.text = text2.text + CHR (13) + CHR (10) + CSTR (date) + CHR (32) + CSTR (time $ () + CHR (10) + "me: "& text1.text
Winsock1.senddata text1.text
Text1.text = ""
End if
End sub

Private sub winsock1_connectionrequest (byval requestid as long)
'Connection
If winsock1.state <> sckclosed then
Winsock1.close
Winsock1.accept (requestid)
End if
End sub

Private sub winsock1_dataarrival (byval bytestotal as long)

'Accept
BEEP
Dim strdata as string
Winsock1.getdata strdata
Text2.text = text2.text + CHR (13) + CHR (10) + CSTR (data) + CHR (32) + CSTR (time $ () + CHR (13) + CHR (10) + text3.text & ":" & strdata

End sub

 

Client:

 

Private sub commandementclick ()
'Connection
Winsock1.remotehost = text3.text
Winsock1.remoteport = Val (text4.text)
If winsock1.state = sckclosed then
Winsock1.connect
Label4.caption = "successful connection"
End if
End sub

Private sub command2_click ()
'Send
Winsock1.senddata text1.text
Text2.text = text2.text + CHR (13) + CHR (10) + CSTR (date) + CHR (32) + CSTR (time $ () + CHR (13) + CHR (10) + "I:" & text1.text
Text1.text = ""

End sub

Private sub command3_click ()
'Exit
End
End sub

Private sub text=keydown (keycode as integer, shift as integer)
'Press enter to send data
If keycode = 13 then
Text2.text = text2.text + CHR (13) + CHR (10) + CSTR (data) + CHR (32) + CSTR (time $ () + CHR (13) + CHR (10) + "I:" & text1.text
Winsock1.senddata text1.text
Text1.text = ""
End if
End sub

Private sub winsock1_dataarrival (byval bytestotal as long)
'Receive
BEEP
Dim strdata as string
Winsock1.getdata strdata
Text2.text = text2.text + CHR (13) + CHR (10) + text5.text & ":" & strdata
End sub

 

 

 

 

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.