How to Use the Winsock Control in Pb

Source: Internet
Author: User
Tags control label

 

How to Use the WINSOCK Control in PB


Original: Da weichun

With the need for resource sharing and real-time communication, many computer applications have already moved into the single-man combat mode and moved into joint operations.
Networks play an increasingly important role in the computer world. In WINDOWS applications, the WINSOCK control most commonly used for processing real-time communication is provided by MICROSOFT. Many documents detail how to use WINSOCK in VB. Even the hlp files provided by WINDOWS are written for VB. Due to the actual application needs, I figured out the application method of the WINSOCK Control in PB. Good things cannot be enjoyed by yourself and shared with everyone.

The following uses a simple program as an example to describe how to use the WINSOCK Control in PB:

1. Add the WINSOCK Control in the window:
Open a new window in the application, click controls --> OLE menu item in the window canvas, and the Insert object window is displayed,
Click the Insert control label, double-click the selected Microsoft Winsock control from the list box, and paste the winsock icon on the window.
In the program, the control name is winsock_a (Party A) and winsock_ B (Party B ).

2. Set the information input and output text box:
In the window, add a button cb_1 and two single-line text boxes sle_1 and sle_2 to enter the string to be sent and the string to be sent by the recipient respectively.

3. Set the communication protocol: The WINSOCK control allows you to use either of the UDP and TCP Protocols for communication.
1. UDP Protocol Settings: UDP is a connectionless communication protocol. Before communication, you must bind the remotehost and remoteport attributes. If two-way communication is required, you must also set the localport attribute.

Add the following statement to the Open event in the window of Party A (Local Address: 134.1.1.1:
Winsock_a.object.protocol = 1 // set the winsock communication protocol to UDP.
Winsock_a.object.remotehost = "134.1.1.2" // ip address of the other party
Winsock_a.object.remoteport = 6000 // The winsock communication port number of the other party
Winsock_a.object.localport = 6001 // The winsock Communication Port Number of the Local Machine
Winsock_a.object.bind // bind the communication protocol
Add the following statement to the Open event in the window of Party B (Local Address: 134.1.1.2:
Winsock_ B .object.protocol = 1 // set the winsock communication protocol to UDP.
Winsock_ B .object.remotehost = "134.1.1.1" // ip address of the other party
Winsock_ B .object.remoteport = 6001 // The winsock communication port number of the other party
Winsock_ B .object.localport = 6000 // The winsock Communication Port Number of the Local Machine
Winsock_ B .object.bin // bind the communication protocol
2. TCP protocol settings: TCP must be connected before communication.
Add the following statement to the Open event of Party A (as the server) window:
Winsock_a.object.protocol = 0 // set the winsock communication protocol to TCP
Winsock_a.object.localport = 6001 // The winsock Communication Port Number of the Local Machine
Winsock_a.listen () // start the listener
Add the following statement to the Connectionrequest event of Party A's winsock_a control:
// After receiving the connection request from the other party
If winsock_a.object.state <> 0 then
Winsock_a.close ()
End if
Winsock_a.accept (requestID) // establish a direct connection
// RequestID is the parameter of the Connectionrequest event.
Add the following statement to the Open event of Party B (as the client) window:
Winsock_ B .object.protocol = 0 // set the winsock communication protocol to TCP
Winsock_ B .object.remotehost = "134.1.1.2" // ip address of the other party
Winsock_ B .object.remoteport = 6000 // The winsock communication port number of the other party
Winsock_ B .connect () // sends a connection request
3. No matter which protocol is used, add the following statement to the Close event of the window:
If winsock_a/* or winsock_ B */. object. state <> 0 then
Winsock_a.close ()
End if
Otherwise, an exception may occur during the second usage.

4. Start Communication
Add the following statement to the click Event of the button cb_1 (set the caption attribute to 'send:
Winsock_a/* or winsock_ B */. object. send (sle_1.text)
Add the following statement to the dataarrival event of the winsock_a/* or winsock_ B */control:
// After receiving the Peer Data
String datastr1 winsock_a/* or winsock_ B */. object. getdata (def datastr1) sle_2.text = datastr1 // display the data string in the text box.

The above procedures actually reflect the underlying working principle of the scanner. A little modification and expansion can be made into a good chat software.

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.