To create an internet-based application, you may think of complex Winsock programming. However, C + + Builder3 provides a new webbroker Internet suite, where the tclientsocket and Tserversocket components encapsulate Windows related APIs, greatly simplifying Winsock programming. To transfer data over the Internet, you need at least a pair of sockets, one socket on the client, and the other socket on the server side. In fact, the Tclientsocket, Tserversocket component is not a socket object, its properties socket will return the respective socket object. Tclientsocket is used to handle the socket connection between the client and the server, Tserversocket is used to handle the socket connection sent by the client, and the client and server can communicate with each other once the socket is connected to the client and server side.
Build a new project to create the user interface for your application:
1. Switch the component page to an Internet page, place a tserversocket component and a Tclientsocket component on the form so that the application can be either a TCP/IP server or a TCP/IP client. Set the Port property to the same value (such as 1000) to determine that the connection type between the sockets is nonblocking (non-blocking).
2. Place two TMemo components on the form to display the conversation separately and set the Memo2 ReadOnly property to True.
3. Place a panel component on top of the form with three buttons on it: Listener (Btnlisten), Connection (Btnconnect), disconnect (Btndisconnect), to initiate the appropriate action.
4. Place a StatusBar component at the bottom of the form, set its Simplepanel property to true, and change the status bar information in the appropriate event handler to let the user know the connection status at any time.
Open the header file, adding two private members to the private segment of the form class: BOOL Isserver; String Server. Both sides of the communication need to run the chat program, isserver to determine which chat program is on the server side, server used to host the server hostname. The constructor that establishes the form class is as follows:
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
IsServer=false;
Server="localhost";
}
Here the server is set by default to localhost, so that the program can be debugged on a stand-alone computer that is not connected to the Internet. In the Windows subdirectory you can find the Hosts.sam file in which the native IP address 127.0.0.1 has been defined by the host name: localhost.
void __fastcall TForm1::FormCreate(TObject *Sender)
{
btndisconnect- >Enabled=false;
}
When the program is running, if the user presses the "Listen" button, the program is set to the server side, the Tserversocket active property should be set to true to allow the server to automatically enter the listening state.
void __fastcall TForm1::btnlistenClick(TObject *Sender)
{
ClientSocket1- >Active=false;
ServerSocket1- >Active=true;
StatusBar1- >SimpleText="正在监听...";
btnlisten- >Enabled=false;
btnconnect- >Enabled=false;
}
When the user presses the "Connect" button, the program pops up a query box asking the user to enter the hostname of the server to which you want to connect, and then establish a connection.
void __fastcall TForm1::btnconnectClick(TObject *Sender)
{
if(InputQuery("连接到服务器","输入服务器地址:",Server)){
if(Server.Length() >0){
ClientSocket1- >Host=Server;
ClientSocket1- >Active=true;
btnlisten- >Enabled=false;
btnconnect- >Enabled=false;
btndisconnect- >Enabled=true;
}
}
}
When a user makes a connection request, the client triggers the OnCreate event, the program first displays the connection information in the status bar, and then the Memo2 that displays the conversation is emptied and ready to start the conversation.
void __fastcall TForm1::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
StatusBar1- >SimpleText="连接到:"+Server;
Memo2- >Lines- >Clear();
}