เป็นตัวอย่างการเขียนโปรแกรม รับส่งข้อมูลผ่าน Network โดยใช้ Winsock1 add the WINSOCK control window
---- In the application to open a new window, click the drawing board in the window controls -> OLE menu item, Insert object pop-up window, click Insert control label, selected from the list box, double-click the Microsoft Winsock control, will be winsock icon attached to the window.
---- In the name of the control procedures for winsock_a (Party A) and winsock_b (B).
---- Second, set up information input and output text box:
---- In the window add a button cb_1, two one-line text box sle_1, sle_2, respectively, for the string you want to send and receive each other to send the string.
c ---- set up communication protocols:
---- WINSOCK control allows users to both protocols UDP and TCP communication alternatively.
---- 1.UDP protocol settings: UDP protocol is a connectionless protocol, the communication is required before remotehost and remoteport binding properties of two-way communication, if necessary, but also localport attribute set.
---- In the Party (local address: 134.1.1.1) window Open event to add the following statement:
winsock_a.object.protocol = 1
/ / winsock communication protocol is set to UDP Protocol
winsock_a.object.remotehost = "134.1.1.2"
/ / Each other's ip address
winsock_a.object.remoteport = 6000
/ / The other side of the winsock communication port number
winsock_a.object.localport = 6001
/ / This machine winsock communication port number
winsock_a.object.bind
/ / Bind communication protocol
/ / winsock communication protocol is set to UDP Protocol
winsock_a.object.remotehost = "134.1.1.2"
/ / Each other's ip address
winsock_a.object.remoteport = 6000
/ / The other side of the winsock communication port number
winsock_a.object.localport = 6001
/ / This machine winsock communication port number
winsock_a.object.bind
/ / Bind communication protocol
---- In the B (local address: 134.1.1.2) window Open event to add the following statement:
winsock_b.object.protocol = 1
/ / winsock communication protocol is set to UDP Protocol
winsock_b.object.remotehost = "134.1.1.1"
/ / Each other's ip address
winsock_b.object.remoteport = 6001
/ / The other side of the winsock communication port number
winsock_b.object.localport = 6000
/ / This machine winsock communication port number
winsock_b.object.bin
/ / Bind communication protocol
/ / winsock communication protocol is set to UDP Protocol
winsock_b.object.remotehost = "134.1.1.1"
/ / Each other's ip address
winsock_b.object.remoteport = 6001
/ / The other side of the winsock communication port number
winsock_b.object.localport = 6000
/ / This machine winsock communication port number
winsock_b.object.bin
/ / Bind communication protocol
---- 2.TCP protocol settings: TCP protocol is needed to connect communications.
---- In the Party (as a server-side) window Open event to add the following statement:
winsock_a.object.protocol = 0
/ / winsock communication protocol is set to TCP protocol
winsock_a.object.localport = 6001
/ / This machine winsock communication port number
winsock_a.listen ()
/ / Start listening
/ / winsock communication protocol is set to TCP protocol
winsock_a.object.localport = 6001
/ / This machine winsock communication port number
winsock_a.listen ()
/ / Start listening
---- Winsock_a Party control in the event Connectionrequest add the following statement:
/ / Receive the other side of the connection request
if winsock_a.object.state <> 0 then
winsock_a.close ()
end if
winsock_a.accept (requestID)
/ / Establish a direct connection
/ / requestID event is its own parameters Connectionrequest
if winsock_a.object.state <> 0 then
winsock_a.close ()
end if
winsock_a.accept (requestID)
/ / Establish a direct connection
/ / requestID event is its own parameters Connectionrequest
---- In the B (as a client) Open the window to add the following event statement:
winsock_b.object.protocol = 0
/ / winsock communication protocol is set to TCP protocol
winsock_b.object.remotehost = "134.1.1.2"
/ / Each other's ip address
winsock_b.object.remoteport = 6000
/ / The other side of the winsock communication port number
winsock_b.connect () / / issue a connection request
/ / winsock communication protocol is set to TCP protocol
winsock_b.object.remotehost = "134.1.1.2"
/ / Each other's ip address
winsock_b.object.remoteport = 6000
/ / The other side of the winsock communication port number
winsock_b.connect () / / issue a connection request
---- 3. No matter what kind of agreement, both events in the window Close to add the following statement:
if winsock_a / * or winsock_b * /. object.state <> 0 then
winsock_a.close ()
end if
winsock_a.close ()
end if
---- Otherwise, they may use the second time the problem occurs when abnormal
---- Third, the beginning of communication
---- The button cb_1 (caption attribute is set to 'send') of the click event add the following statement:
winsock_a / * or winsock_b * /. object.send (sle_1.text)
---- In winsock_a / * or winsock_b * / control to add the following events dataarrival statement:
/ / Receive other data
string datastr1
winsock_a / * or winsock_b * /. object.getdata (def datastr1)
sle_2.text = datastr1 / / display the data string in the text box
string datastr1
winsock_a / * or winsock_b * /. object.getdata (def datastr1)
sle_2.text = datastr1 / / display the data string in the text box
---- In fact the procedure is reflected bottom of the chat's working principle, a little modification can be made to expand a good chat software.


