Use Pb To write Winsock TCP/IP applications
Author: Unknown Source: moonlight software station time: moonlight software Station
Http://www.it97.cn/Art/Show.asp? Id = 18705
Http://www.moon-soft.com/doc/4023.htm
The sockets in Pb are provided through the Winsock. PBL library. It encapsulates the data structures and processes used in socket programming and is functionally similar to the Winsock Control in VB.
Winsock. PBL defines two types of sockets: stream socket and datagram socket. The stream socket needs to be connected to another stream socket in the listening status for communication. It is based on connections and has high reliability. The datagram socket does not need to be connected, the packets sent from the source host are stored and forwarded to the target host in the network, which is efficient but reliable. During programming, select one of them based on the application environment and requirements. If the Communication Subnet is quite reliable, you can consider using a datagram socket.
Figure 1
The first step for compiling a Winsock TCP/IP application with Pb is to add Winsock. PBL to the application and then declare the following global variables:
Winsock WS
Boolean B-TCP-active
// Checks whether the WS Initialization is successful.
Powerobject GPO-null // global Null Object
Add the following code to the application's Open events:
Ws = create Winsock
// Initialize an instance of WinSock
Setnull (GPO-null) // empty object GPO-null is used in the WS function.
Add the following code to the close event of the application:
Figure 2
Destroy ws // destroy the WS object
After completing the above work, you can start programming. The following describes how to use socket for communication.
1. Use a datagram socket to send data to port 7 of the Local Machine
TCP and UDP Protocol specify that the port length of the transport layer is 16 bits. Therefore, TCP and UDP software can use 216 different ports for communication. However, it is best not to use the first 1024 ports during programming, because many of these ports are dedicated ports, for example, 21 is the ftp port. In this example, port 7 is very special. It returns any received data and is often used for port detection. Next we will send a datagram to port 7 of the local machine:
Dgsock = create socketdgram
// Create a datagram socket object
Uladdr = ws. iNet-ADDR ("127.0.0.1 ″)
// Convert the local IP address to a 32-bit ulong type
Buf = blob ("these data is send through datax ~ R ~ N ") // The data to be sent
Dgsock. sendto (BUF, Len (BUF), 0, uladdr, 7)
// Send a datagram to Terminal 7 of the uladdr host
Buf = blob (space (LEN (BUF )))
// Clear the Buf Buffer
Dgsock. Recv (BUF, Len (BUF), 0)
// Receives the datagram
MessageBox ('data encoded ed', string (BUF ))
// Display the received data
Dgsock. closesocket () // close the socket
Destroy dgsock
As shown in the preceding demonstration, the datagram sent to port 7 of the local machine is immediately returned.
2. Use streaming socket to develop online chat programs
A Web chat program consists of two parts: a service program and a customer program. The service program is always listening. When you hear a call from the customer program, you create a socket to respond to it. The following uses streaming socket to develop a two-node chat program:
(1) Write service programs
Service Program Interface 1. Create an instance of streaming socket in the open event in the main window:
Ssock = create sockstream // ssock is the instance variable
Add the following code to the clicked event of the "listener" button:
Uladdr = ws. iNet-ADDR ("202.140.1.20 ″)
// Convert the server address to the ulong type
Ssock. BIND (uladdr, 2000) // bind the stream socket to port 2000 of the uladdr address
Ssock. Listen (5) // listen to the preceding address and port. The parameter is the length of the Request queue and the maximum value is 5.
Uisocktype = ssock. Accept (ulclientaddr, iclientport)
// Accept the customer's request. The parameter is filled with the customer's socket address and port. The return value is the customer's socket type.
Saccept = create socket
// Create a socket to respond to customer requests
Ulparam = 1
Saccept. initsocket (uisocktype)
// Same as the client socket type
Saccept. ioctlsocket (WS. fionbio, ulparam)
// Asynchronous mode
Timer (0.5)
// Start the timer to receive data at an interval of 0.5 seconds
Add the following code to the timer event to process the incoming data:
Buf = blob (space (256) // defines the buffer size
Saccept. Recv (BUF, Len (BUF), 0)
// Received data
Mle-1.Text = mle-1.Text + trim (string (BUF ))
// Display the message
Add the following code to the clicked event of the send button:
Buf = blob (mle-2.Text + ″~ R ~ N ″)
// Put the content in the mle-2 into the sending Buffer
Saccept. Send (BUF, Len (BUF), 0)
// Send the content in the Buf to the other party
Mle-2.Text = ″″
// Clear sent content
Add the following code to the clicked event of the exit button:
Saccept closesocket () // close the socket
Destroy saccept
Ssock. closesocket ()
Destroy ssock // clear the socket
(2) Write customer programs
Design the window shown in 2. The code for the open event is:
Sclient = create socketstream
// Create a streaming socket
Ulparam = 1
// 1 indicates asynchronous mode (non-blocking mode)
Timer (0.5) // start the timer to check whether data has arrived at the interval of 0.5 seconds
Sclient. ioctlsocket (WS. fionbio, ulparam)
// Set sclient to asynchronous mode
Add the following code to the clicked event of the Connect button:
Uladdr = ws. iNet-ADDR ("202.140.1.20 ″)
// Server address
If sclient. wsconnect (uladdr, 2000) =-1 then // connect to port 2000 of the server
MessageBox ('socket ', "server connection failed ″)
End if
The code of the timer event and the clicked event of the send button is the same as that of the service program. You only need to change the socket object saccept to sclient.
Declaration: by default, the streaming socket object is created in synchronous mode and can be converted to asynchronous mode as needed. In synchronous mode, some Winsock function calls do not return control to the program before processing is completed, resulting in no response to the program. For example, before the data arrives, the Recv () call will remain in the waiting state. In the above service program, the socket used to listen to the customer's connection uses the synchronous mode, the socket responding to the customer's request uses the asynchronous mode, and the socket in the customer program also uses the asynchronous mode.
Run the service program, click "listener" to enter the waiting state, run the customer program, and click "Connect" to call. After the connection is established, you can chat. Enter a message in the mle-2, click "send" to send to the other party, the message sent by the other party is displayed in the mle-1