The network is an exciting field, write the application on the network is the plot that many programmers aspire to, but write the network program need to master a lot of network transmission protocol, programming interface and WinSock32 API function, because of this, The transition from a Windows programmer to a Web programmer is not an easy task. Recently, I succeeded in using C + + Builder 5.0 Enterprise version of the online chat program, introduced the following:
First, the principle: the network chat tool needs to pass the TCP/IP protocol, therefore may divide the network chat program into the server end and the client end two parts, in which the server side uses to convert the program to a fictitious TCP/IP server. and establishes a connection with the specified client, sends and receives data to the specified client when the connection succeeds, converts the program to a virtual TCP/IP client, and sends a connection signal to the specified server, sending and receiving data to the specified server after the connection succeeds. In the C + + Builder 5.0 environment, server-side and client-side functions are implemented by Tserversocket controls and Tclientsocket controls respectively. Where the TCP/IP protocol is contained in the properties of the control, so long as the attribute is correctly filled in, the network communication can be achieved.
Second, the concrete realization: first ' in C + + Builder 5.0 environment to establish a application, and save for Project1. Add the following controls on the Form1 and set the related properties, where Memo2 displays information from each other, where Memo1 is used to display information to the other person:
Control Name property value
Tbevel Caption Bevel1
Tspeedbutton Caption SpeedButton1
TMemo Caption Memo1
TMemo Caption Memo2
Tmainmenu Caption MainMenu1
Tserversocket Caption ServerSocket1
Tclientsocket Caption ClientSocket1
Adds three menu items to the MainMenu1 control, with the properties
Name Caption
Connect Connect
Disconncet Disconnect
Listin Monitor
Add in Unit1 's private:
BOOL Isserver;
String Server;
Add the following code to the click Handle in Connect:
void __fastcall Tform1::connectclick (tobject *sender)
{
if (clientsocket1->active) {clientsocke1t->active = false;}//To determine if a connection is to be made, such as a connection disconnect//
if (Inputquery ("Computer to connect to", "Add to the IP address of the machine to be connected:", Server)
{
if (server1.length () > 0)//To determine whether the user has been added//
{
Clientsocket-1>host = server;//Set the IP address value to be connected to the user//
Clientsocket1->active = true;//for connection//
}
}
}
Add the following code to the listen click handle:
void __fastcall Form1:: Listenclick (TObject *sender)
{
listen->checked =! listen->checked;
if (listen->checked)
serversocket1->active = true;//Server side listens for signals from the client
Else
Serversocket1->active = false;
}