Use of Winsock Controlled UDP Protocol in VB

zhaozj2021-02-08  226

Use of Winsock Controlled UDP Protocol in VB

UDP protocol foundation:

UDP (User DataGram Protocol) is a connectionless protocol. Unlike TCP operations, the computer does not need to establish a connection, and a UDP application can simultaneously act as a customer or server side.

Since the UDP protocol does not need to create a clear connection, establish a UDP application is much easier than the establishment of TCP. In TCP applications, a Winsock control must be explicitly set to "listening", and other Winsock control must use the Connect method to initially connect.

Using the UDP protocol, the data is transmitted between the two Winsock controls, and the following three steps must be completed at both ends of the connection:

1. Set the name of the RemoteHost property for other computers;

2. Set the RemotePort property as the value of the LocalPort property of the second Winsock control;

3. Apply for a BIND method.

By using method BIND, the Winsock control can be bundled to a local port so that the Winsock control uses the port to perform a "listening" function similar to TCP and prevent other applications from using the port.

Use this protocol to transfer data, first set the localPort property of the client computer. As the server's computer only needs to set the RemoteHost property to be the IP address or domain name of the client computer, and set its RemotePort property to the localport attribute on the client computer, then you can start the information, the customer can be sent. Computers can use methods getData in its DataArrial event to get information.

The next example demonstrates a "conversation" application to allow real-time conversations to each other.

UDP application one:

Create a new standard EXE project file, drag and drop a Winsock control to your form, add two text boxes to your form, and make the settings of the following properties:

Form: Name = "frmpera" caption = "UDP Application (1)"

Winsock Control: Name = "udppera" protocol = "SCKUDPPPROTOCOL"

Text Box 1 (TextBox): Name "txtsend"

Text Box 2 (TCXTBOX): name = "txtoutput" multiline-true scrollbars = 2

Then open the code window, enter the following code in the corresponding event:

Private sub flow_load ()

With udppeera

.Remotehost = "197.1.1.2" 'To connect the computer name

.Remoteport = 1010 'To connect the port number

.Localport = 1011 'The Winsock controls the local port number to be used, which is convenient for other ends with the Winsock communication

.Bind 1011 'Binds the Winsock Control to the local port

ENDWITH

End Sub

Private sub txtsend_change ()

UDPPEERA.SENDDATA TXTSEND.TEXT 'Send text

End Sub

Private sub udppera_dataarrival (byval bytestotal as long)

DIM STRDATA AS STRING

Udppeera.Getdata strdata, vbstring

TXTOUTPUT.TEXT = STRDATA

End Sub

UDP application 2:

Similar to establishing a UDP Server, add a WINSOCK control and two text boxes on the form, then make the settings for the following properties: FORM: Name = "frmpeerb" caption = "UDP Application (2)"

Winsock Control: Name = "udppeerb" protocol = "Sckudpprotoclool"

Text Box 1 (TextBox): Name = "txtsend"

Text Box 2 (TextBox): name = "txtoutput" multiline = true scrollbars = 2

Then enter the following code:

Private sub flow_load ()

With udppeerb

.Remotehost = "197.1.1.2" 'To connect to the IP address of the computer

.Remoteport = 1011 'To connect the port number

.Localport = 1010 'This Winsock control will use the local port number to make it easy for other parties and communication

.Bind 1010 'Binds the Winsock Control to the local port

End with

End Sub

Private sub txtsend_change ()

Udppeerb.senddata txtsend.text 'Send text

End Sub

Private sub udppeerb_dataaarrival (byval Bytestotal As Long)

DIM STRDATA AS STRING

UDPPEERB.GETDATA STRDATA, VBSTRING

TXTOUTPUT.TEXT = STRDATA

End Sub

To run the instance, open two Visual Basic's examples, then run these two project files separately. To run this two cases on a different machine, you only need to change RemoteHost in the two project files into the IP address or domain name of the corresponding computer (the above example is passed on Visual Basic 5.0 and PWIN97).

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

New Post(0)