How to create a user's own online chat program using C ++ Builder 5.0 Enterprise

zhaozj2021-02-08  225

How to create a user's own online chat program using C Builder 5.0 Enterprise

The network is an exciting area. Writing applications on the network is a plot of many programmers, however writes a network program to master a large number of network transport protocols, programming interfaces, and Winsock32 API functions, because of this, to complete from Windows The transformation of programmers to web programmers is not a matter. Recently, the author successfully wrote online chat programs, special introduction to the following: 1. Principle: Network chat tool needs to pass the TCP / IP protocol, so you can divide the network chat program into server and customers. The server is two parts, where the server side is used to convert the program into a virtual TCP / IP server. And establish a connection with the specified client. After the connection is successful, send and receive data to the specified client; the client side The program is converted into a virtual TCP / IP client, and the connection signal is sent to the specified server, and after the connection is successful, send and receive data to the specified server. The server-side and client-side features are implemented by the TSERVERSOCKET controls and TclientSocket controls in the C Builder 5.0 environment. The TCP / IP protocol is included in the properties of the control, so online communication can be implemented as long as the attribute is filld properly. Second, the specific implementation: First, set an Application in the C Builder 5.0 environment, saved as Project1.

On Form1 add the following control, and set the associated attributes, wherein Memo2 other information transmitted to the display, wherein Memo1 pass each other information for display: Control Name Property Value TBevel Caption Bevel1TSpeedButton Caption SpeedButton1TMemo Caption Memo1TMemo Caption Memo2TMainMenu Caption MainMenu1TServerSocket Caption ServerSocket1TClientSocket Caption ClientSocket1 MainMenu1 control while increasing in three menu item with the attribute is connected Disconncet Name Caption connect disconnect Listin listener's Private the added Unit1: bool IsServer; String Server; connect handler add the following code to Click: void __fastcall TForm1 :: ConnectClick (TOBJECT * Sender) {if (clientsocket1-> Active) {ClientSocke1t-> Active = false;} // Determine whether it is connected, if the connection is disconnected // if ("Computer to connect to" "IP address of the machine to add to the machine:", server)) {if (server1.length ()> 0) // determines if the user has added // {ClientSocket-1> Host = Server; // setting The IP address to which you want to connect to the IP address value // clientsocket1-> active = true; // //}}}}} Add the following code in Listen: Void __fastcall form1 :: listenclick (Tobject * Sender) {listen-> checked =! Listen-> checked; if (listen-> checked) ServerSocket1-> Active = true; // server side monitoring signal from the client device // else serversocket1-> Active = false; } Add the following code in Disconnect: void __fastcall tform1 :: exitclick (TOBJECT * Sender) {serversocket1-> close (); // Close server // clientSocket1-> close (); // Close client / / Close ();} KeyDown add the following code to handle Memo1: void __fastcall TForm1 :: Memo1KeyDown (TObject * Sender, WORD & Key, TShiftState Shift) {if (Key == VK_RETURN) {if (IsServer) {ServerSocket-> Socket -> Connections [0] -> Sendtext (Memo1-> Lines-> strings [Memo1-> Lines-> count - 1]);

转载请注明原文地址:https://www.9cbs.com/read-1663.html

New Post(0)