|
According to the following example, I can't send text!
First, add the WINSOCK control in the window:
新 Open a new window in the application, click the controls-> OLE menu item in the window artboard, and the Insert object window pops up,
Click the Insert control tab, double-click the selected Microsoft Winsock control from the list box, and paste the winsock icon on the window.
控件 The names of the controls in the program are winsock_a (Party A) and winsock_b (Party B).
Second, set the information input and output text boxes:
增加 Add a button cb_1, two single-line text boxes sle_1, sle_2 in the window, which are used to enter the string to be sent and accept the string sent by the other party.
3. Set communication protocol: WINSOCK control allows users to communicate using either UDP or TCP protocol.
1. UDP protocol setting: UDP protocol is a connectionless communication protocol. Before communication, you need to bind the remotehost and remoteport attributes. If you need two-way communication, you must also set the localport attribute.
加入 Add the following statement to the Open event of the Party A (local address: 134.1.1.1) window:
Winsock_a.object.protocol = 1 // winsock communication protocol is set to UDP protocol
Winsock_a.object.remotehost = "134.1.1.2" // IP address of the other party
Winsock_a.object.remoteport = 6000 // Winsock communication port number of the other party
Winsock_a.object.localport = 6001 // winsock communication port number of this machine
Winsock_a.object.bind // Binding communication protocol
加入 Add the following statement to the Open event of Party B's (local address: 134.1.1.2) window:
Winsock_b.object.protocol = 1 // winsock communication protocol is set to UDP protocol
Winsock_b.object.remotehost = "134.1.1.1" // The IP address of the other party
Winsock_b.object.remoteport = 6001 // Winsock communication port number of the other party
Winsock_b.object.localport = 6000 // winsock communication port number of this machine
Winsock_b.object.bin // Binding communication protocol
2. TCP protocol setting: TCP protocol needs to be connected before communication.
加入 Add the following statement to the Open event of the Party A (as the server) window:
Winsock_a.object.protocol = 0 // Winsock communication protocol is set to TCP protocol
Winsock_a.object.localport = 6001 // winsock communication port number of this machine
Winsock_a.listen () // Start listening
加入 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 own parameter of the Connectionrequest event
加入 Add the following statement to the Open event of the Party B (as client) window:
Winsock_b.object.protocol = 0 // Winsock communication protocol is set to TCP protocol
Winsock_b.object.remotehost = "134.1.1.2" // The IP address of the other party
Winsock_b.object.remoteport = 6000 // Winsock communication port number of the other party
Winsock_b.connect () // Make a connection request
3, no matter which protocol is used, the following statement must be added to the Close event of the window:
If winsock_a / * or winsock_b * /. Object.state <> 0 then
Winsock_a.close ()
End if
Otherwise, abnormal problems may occur during the second use.
四 、 Start communication
加入 Add the following statement to the click event of button cb_1 (caption property is set to 'send'):
Winsock_a / * or winsock_b * /. Object.send (sle_1.text)
加入 Add the following statement to the dataarrival event of winsock_a / * or winsock_b * / control:
// After receiving the other party's data
String datastr1 winsock_a / * or winsock_b * /. Object.getdata (ref datastr1) sle_2.text = datastr1 // Display the data string in the text box.
Always prompt "error calling extenal object function senddata ..."
I have tried both send and senddata, and send indicates that there is no such function. senddata prompts the above error, as if the senddata function was not declared! I have tried tcp / ip, UDP mode, no! Please hero guidance! |
|