Methods for using the Winsock control in PB

Source: Internet
Author: User
Tags bind port number
With the need of resource sharing and real-time communication, many computer application programs have already been turned away from individual combat mode and transferred into joint action.

Network in the computer World, more and more play a pivotal role. In a Windows application, the most common use of real-time communication is the Winsock control provided by Microsoft Company. Many of the data are detailed Winsock in the use of VB, even Windows itself provides the HLP file is also written for VB. The author has figured out the application method of Winsock control in PB because of the need of practical application. The good thing dare not enjoy alone, take out to share with everybody.

The following is an example of a simple program that illustrates the use of the Winsock control in PB:

First, add the Winsock Control to the window:

Open a new window in the application, click the Controls-->ole menu item in the window artboard, eject the Insert Object window, click the Insert Control tab, double-click the Microsoft Winsock control from the list box, Attach the Winsock icon to the window.

In the program, the control name is Winsock_a (party a) and Winsock_b (party B).

Second, set the information input and output text box:

Add a button to the window cb_1, two single-line text box sle_1,sle_2, used to enter the string to send and accept each other's sent string.

Third, set up communication protocol:

The Winsock control allows users to communicate in either UDP or TCP two protocols.

1.UDP protocol settings: UDP protocol is a connectionless communication protocol, before communication, you need to bind RemoteHost and RemotePort properties, and if you need two-way communication, set the LocalPort property.

Add the following statement to the Open event in party A (at the local address: 134.1.1.1) window:

Winsock_a.object.protocol=1
Winsock communication protocol set to UDP protocol
Winsock_a.object.remotehost= "134.1.1.2"
Each other's IP address
winsock_a.object.remoteport=6000
The Winsock communication port number of the other side
winsock_a.object.localport=6001
Winsock communication port number for this machine
Winsock_a.object.bind
Binding Communication Protocol

Add the following statement to the Open event in party B (local address: 134.1.1.2) window:

Winsock_b.object.protocol=1
Winsock communication protocol set to UDP protocol
Winsock_b.object.remotehost= "134.1.1.1"
Each other's IP address
winsock_b.object.remoteport=6001
The Winsock communication port number of the other side
winsock_b.object.localport=6000
Winsock communication port number for this machine
Winsock_b.object.bin
Binding Communication Protocol

2.TCP protocol settings: The TCP protocol needs to be connected before communication.

Add the following statement to the Open event in party a (as server-side) window:

Winsock_a.object.protocol=0
Winsock communication protocol set as TCP protocol
winsock_a.object.localport=6001
Winsock communication port number for this machine
Winsock_a.listen ()
Start listening

Add the following statement to the Connectionrequest event of party WINSOCK_A control:

After accepting a connection request from the other side
If winsock_a.object.state< >0 Then
Winsock_a.close ()
End If
Winsock_a.accept (RequestID)
Establish a direct connection
RequestID is the Connectionrequest event's own argument

Add the following statement to the Open event in party B (as the client) window:

Winsock_b.object.protocol=0
Winsock communication protocol set as TCP protocol
Winsock_b.object.remotehost= "134.1.1.2"
Each other's IP address
winsock_b.object.remoteport=6000
The Winsock communication port number of the other side
Winsock_b.connect ()//Issue connection request

3. Regardless of the protocol used, add the following statement to the Close event in the window:

If winsock_a/* or winsock_b*/.object.state< >0 then
Winsock_a.close ()
End If

Otherwise, an exception problem may occur when you use the second time

Iv. beginning of communications

Add the following statement to the Click event of the button Cb_1 (Caption property set 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 data from each other
String DATASTR1
winsock_a/* or Winsock_b*/.object.getdata (def datastr1)
SLE_2.TEXT=DATASTR1//Display the data string in a text box

The above program actually embodies the bottom working principle of the chat, and can be made into a good chat software with a slight modification and expansion.

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.