Using VB to create B/s program

Source: Internet
Author: User

With the popularity of the Chinese version of Windows 95 and the Chinese version of Windows NT Server 4.0, Microsoft has launched development software on the corresponding platform: Visual Basic 5.0 Chinese Enterprise Edition. It provides a powerful tool for network development in the Windows environment, one of which is the Winsock control.

The Winsock control is based on the TCP and UDP protocols and completes communication with the remote computer. Even users who are unfamiliar with TCP/IP can create a simple client/server program in more than 10 minutes using the control. The following is a detailed description of the events, methods, and properties of the Winsock control in the order in which it appears in the program to better understand the source code of the program.

The server program implementation process is:

(1) The server program must set the LocalPort property as the listening port, which is an integer (as long as it is a value that is not used by another TCP/IP application).

(2) Use the Listen method to enter the listening state, waiting for the client program's connection request.

(3) The client program issues the connection request, causes the server program to produce the Connectionrequest event, this event obtains a parameter RequestID.

(4) The server program accepts the RequestID request of the client program with the Accept method. This allows the server program to send data using the SendData method. The Accept method must use the RequestID obtained in the previous step as its parameter.

(5) When the server program receives the data, the DataArrival event is generated, and the parameter bytestotal contains the number of bytes of data received. In this event, you can receive data by using the GetData method.

(6) If you accept the Close event, shut down the TCP/IP connection with the closed method.

The client program implementation process is:

(1) The client program sets the RemoteHost property to specify the host name of the running server program, which can be found in the Control Panel | network | id | computer name.

(2) Set the RemotePort property to specify the listening port for the server program.

(3) Using the Connect method, make a connection request to the server.

(4) The server accepts the client program request, the client program produces connect event, can use the SendData method to send the data.

(5) When the client program receives the data, the DataArrival event is generated, and the parameter bytestotal contains the number of bytes of data received. In this event, you can receive data by using the GetData method.

(6) If the Close event is accepted, the connection is closed with the closing method.

The Winsock control also has two important properties, namely, Protocol and State. Protocol whether the protocol used is TCP or UDP: The value Scktcpprotocol represents TCP, and the value Sckudpprotocol represents UDP. Because the default setting for the Winsock control is Scktcpprotocol, the Protocol property is not used in the program. The State property reflects the connection status of the current TCP/IP, as shown in table 1.

Table 1 The State property of the Winsock control and its description

Constant numerical description

sckclosed 0 Default value, off.

Sckopen 1 Open.

Scklistening 2 Listening

Sckconnectionpending 3 Connection hangs

Sckresolvinghost 4 identifies the host.

Sckhostresolved 5 Recognized hosts

sckconnecting 6 is connecting.

Sckconnected 7 is connected.

Sckclosing 8 siblings are shutting down the connection.

Sckerror 9 error.

Run the server program on a single computer, and there is only one Exit button on the window. Run the client program on another computer, enter the server's host name in the text box to the right of the connection button, and then click the Connect button. If the connection succeeds, two text boxes appear in both the Server and client program windows. At this point, you can enter text in the above text box at both ends, which appears immediately in the text box below.

The controls used by the server program are as follows:

(1) Command1: Exit button;

(2) Textsend: Send the data text box;

(3) Winsockserver: Server Winsock;

(4) Textget: Receive data text box.

The interface of the server program is shown in the figure.

The source code for the server program is as follows:

Private Sub Command1_Click()
End
End Sub

Private Sub Form_Load()
textsend.Visible = False
textget.Visible = False
Winsockserver.LocalPort = 1001
Winsockserver.Listen
End Sub

Private Sub textsend_Change()
Winsockserver.SendData textsend.Text
End Sub

Private Sub Winsockserver_Close()
Winsockserver.Close
End
End Sub

Private Sub Winsockserver_ConnectionRequest(ByVal requestID As Long)
textsend.Visible = True
textget.Visible = True
If Winsockserver.State <> sckClosed Then Winsockserver.Close
Winsockserver.Accept requestID
End Sub

Private Sub Winsockserver_DataArrival(ByVal bytesTotal As Long)
Dim tmpstr As String
Winsockserver.GetData tmpstr
textget.Text = tmpstr
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.