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