The socket in the PB is provided through the Winsock.pbl library, which encapsulates the data structure and procedures used in the socket programming, which is functionally similar to the Winsock control in VB. Two types of Socket: streaming socket and datagram sockets are defined in Winsock.pbl. Flow Socket needs to be connected to another stream socket that can communicate after the stream socket is based on the connection, its reliability is high; the data report socket does not need to establish a connection, and the message sent by the source host is stored in the network. After reaching the host, the efficiency is high but the reliability is low. When programming, select one of the applications, if one of the applications, if the communication subnet is quite reliable, it is possible to consider using the data reported SOCKET. Figure 1 Write the first step of Winsock TCP / IP application with PB to add Winsock.pbl to the application, then declare the following global variables: Winsock WS Boolean B-TCP-ACTIVE // Used to test whether WS is initialized successfully PowerObject GPO-NULL // Global Empty Object Adds the following code: WS = CREATE WINSOCK // Initialize Winsock's instance setnull (gpo-null) // WS function in the empty object GPO-NULL in the application The CLOSE event of the program adds the following code: Figure 2 DESTROY WS // Destroy the WS object After completing the above work, you can start programming, which describes how to communicate with Socket. 1. Data reported socket Send data TCP and UDP protocols to this 7th port 7 port specified that the length of the transport layer port is 16 bits, so TCP and UDP software can communicate with 216 different ports. Despite this, it is best not to use the previous 1024 ports when programming, because there are many dedicated ports in this range, such as the FTP port. The number 7 used in this example is very special, and it is exposed to any data received and is often used for port detection. Let's send a datagram to the 7th port of this machine: DGSock = CREATE SOCKETDGRAM // Create a Data Patement Socket Object ULADDR = WS.Inet-Addr ("127.0.0.1") // Transfer the native IP address to 32-bit Ulong Type BUF = Blob ("Tse Data IS Send Through DataGram ~ R ~ N") // Data To send DGSock.sendto (BUF, LEN (BUF), 0, ULADDR, 7) / / 7 to ULADDR host 7 Number Send Dativity BUF = Blob (Space (BUF))) // Empty BUF Buffer Dgsock.Recv (BUF, LEN (BUF), 0) // Receive Dativity NewsMeSageBox ('Data Received', String) BUF)) // Display the received data DGSock.closeSSOCKET () // Close Socket Destroy DGSock From the above demo, it can be seen that the data report sent to the 7th port is immediately rebounded back. 2. Developing Network Chat Program Network chat programs using flow Socket usually contains two parts: Services and client programs. The service program has been in the listening state. When you hear the customer's call, create a socket respond to it. The following is developed with streaming Socket: (1) Writing Service Server Sergerts As shown in Figure 1.
A instance of a streaming socket in the OPEN event in the main window: SSOCK = CREATE SOCKSTREAM // SSOCK Adds the following code to instance variables in the Clicked event of the "listening" button: uLaddr = ws.inet-addr ("202.140.1.20 ") // Transform the server address to ulong type sock.bind (uLaddr, 2000) // Bind the stream socket to the 2000 port of the ULADDR address, SSOCK.LISTEN (5) // listen to the above address and port, parameters For the request queue length, the maximum value is 5 uisocktype = sock.Accept (ulclientaddr, iClientPort) // accepts the customer request, the parameters fill in the address and port of the customer socket, return the value to the customer socket type snaccept = create socket // creation one Socket Response Customer request ULPARAM = 1 SACCEPT.INITSOCKET (UisockType) / / The same SACCEPT.IOCTLSOCKET (WS.FIONBIE, ULPARAM) "// asynchronous mode Timer (0.5) // is received at intervals of 0.5 seconds Data Adds the following code to the TIMER event to process the data of the arrival: buf = blob (space (256)) // Defines the buffer size Saccept.Recv (buf, len (buf), 0) // receives the data MLE- 1.Text = MLE-1.Text Trim (String (BUF)) // Display message Add the following code in the Clicked event of the Send button: BUF = blob (MLE-2.Text "~ R ~ N") ///// Put the contents of MLE-2 in the send buffer SACCEPT.SEND (BUF, LEN (BUF), 0) // Send the content in the BUF to the other MLE-2.Text = "// Clear the sent content Add the following code in the Clicked event of the "Exit" button: Saccept CloseSocket () // Close Socket Destroy Saccept Ssock.closeSocket () Destroy SSOCKET (/ Clear Socket (2) Write a client The window is designed as shown in Figure 2, the code of its OPEN event is: SCLIENT = CREATE SOCKETSTREAM // Create streaming socket ulparam = 1 // 1 represents asynchronous mode (ie Non-blocking mode) Timer (0.5) // Start Timer Check if there is a 0.5 second interval to reach SCLIENT.IOCTLSOCKET (WS.FIONBIO, ULPARAM) // Set the SCLInt Set to Asynchronous Mode Add the following code: uLaddr = ws.inet-addr "202.140.1.20") // Server address if Sclient.wsconnect (uladdr, 2000) = - 1 THEN / / Connect to the server 2000 port MessageBox ('socket', "Connection Server Failure") End If Timer Events and " The code of the Clicked event of the button is the same as the service program, just change the socket object SACCEPT to SCLIENT. Disclaimer: The stream Socket object created by default uses synchronous mode, which can be converted into asynchronous mode as needed.