Create TCPIP Client Server Programs with VB5 Winsock Control

zhaozj2021-02-17  50

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

WINSOCK control is based on TCP, UDP protocol, complete communication with the remote computer. Even users who are not familiar with TCP / IP, using this control can also create a simple client / server program in ten minutes. Below we are described in detail in the order of Winsock control, in order to better understand the program source code.

The implementation process of the server program is:

(1) The server program must set the LocalPort property as a listener port, which is an integer (as long as it is not used by other TCP / IP applications).

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

(3) The client program issues a connection request to cause the server program to generate the ConnectionRequest event, which get a parameter requestID.

(4) The server program uses the Accept method to accept the RequestID request for the client program. In this way, the server program can send data with the SendData method. The Accept method must use the RequestID obtained by the previous step as its parameters.

(5) When the server program receives the data, generates the DataArrival event, the parameter Bytestotal contains the number of received data bytes. In this event, you can receive data with a getData method.

(6) If you accept a Close event, close the TCP / IP connection with the Close method.

The implementation process of the client program is:

(1) The client program sets the RemoteHost property to specify the host name of the running server program, which can be checked in the Control Panel | Network | Identification | Computer Name.

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

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

(4) The server accepts the request by the client program. The client program generates the Connect event, you can send data with the SendData method.

(5) When the client program receives the data, generates a DataArrival event, and the parameter Bytestotal contains the number of received data bytes. In this event, you can receive data with a getData method.

(6) If the Close event is accepted, the connection is turned off with a Close method.

The Winsock control has two important properties, which is Protocol and State. Protocol Setting Used Protocol is TCP or UDP: Value SCKTCPPROTOCOL indicates TCP, and the value SCKUDPPROTOCOL indicates UDP. Because the default setting of the Winsock control is SCKTCPPROTOCOL, there is no Protocol property in the program. The State property reflects the current TCP / IP connection status, and the value is shown in Table 1.

Table 1 State property and description of the Winsock control

The constant value describes SCKCLOSED 0 default, close. Sckopen1 opens. Scklistening2 listening SCKNECTIONPENDING3 connections hangs SckresolvingHost4 identifies the host. SckhostResolved5 recognizes host Sckconnecting6 is being connected. SckConnected7 is connected. Sckclosing8 Single staff is closing the connection. SCKERROR 9 error. First run the server program on a computer, there is only one "exit" button on the window. Run the client program on another computer, and enter the server's host name in the text box on the "Connection" button. Click the "Connection" button. If the connection is successful, both the server and the client program window will appear. At this time, both ends can be entered in the above text box, which will appear immediately in the text box below.

The controls used by the server program are as follows:

(1) Command1: Exit button;

(2) Textsend: Send a data text box;

(3) WinsockServer: server Winsock;

(4) Textget: Receive the Data Text Box.

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

The source code of the server program is as follows:

Private submmand1_click ()

End

End Sub

Private sub flow_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

The controls used by the client program are as follows:

(1) Command1: Exit button;

(2) Command2: Connection button;

(3) WinsockClient: customer Winsock;

(4) Text1: Hostname text box;

(5) Textsend: Send a data text box;

(6) Textget: Receive the data text box; the interface of the client program is shown in the figure.

The source code of the client program is as follows:

PRIVATE SUB Command1_click () end

End Sub

Private sub fascist2_click ()

WinsockClient.Connect

End Sub

Private sub flow_load ()

Textsend.visible = false

Textget.visible = false

WinsockClient.remotEport = 1001

WinsockClient.Remotehost = "sccdsz"

End Sub

Private sub text1_change ()

WinsockClient.Remotehost = text1.text

End Sub

Private sub textsend_change ()

WinsockClient.senddata textsend.text

End Sub

Private sub winsockclient_close ()

WinsockClient.Close

End

End Sub

Private sub winsockclient_connect ()

Textsend.visible = TRUE

TEXTGET.Visible = TRUE

Command2.visible = false

End Sub

Private Sub WinsockClient_DataArrival (Byval Bytestotal As Long)

DIM TMPSTR AS STRING

WinsockClient.getdata Tmpstr

Textget.text = Tmpstr

End Sub

(China Bank Chengdu Branch Computer Song Song Weihua 610061)

转载请注明原文地址:https://www.9cbs.com/read-28966.html

New Post(0)