TCP/IP transmission is a point-to-point transmission mode!
1. Create an application.
Select the "Application" option under the "NEW" menu to create a common Application.
2. Create the required controls.
First, select the ImageList control and CoolBar control on the Win32 page of the control bar, and then select the ToolBar control from the Win32 bar to place it in the CoolBar.
Control. Right-click the "ImageList" control and select the "ImageList Editer..." option in the pop-up menu.
In the "ImageList Editer" dialog box, click the "Add..." button and select five bitmaps. In the Object manager Object Inspector
Set the Image attribute of the ToolBar Control to "ImageList1", right-click the "ToolBar" control, and select the "New Button" option,
Five toolbuttons are created in total. In the object manager, set the ImageIndex attribute of the five toolbuttons to, and 4 respectively.
In this case, the five bitmaps in the ImageList control are displayed on the Toolbutton, And the ShowHint attributes of the five toolbuttons are all set.
Set to "ture" and set their Hint attributes to "listener", "connection", "disconnected", "Change your nickname", and
"Exit the chat program ".
Then, place an Edit control, Memo control, StatusBar control, and a Label control in the form. Set the Caption attribute of the Label Control
Set to "input box ".
Finally, it is the most critical. On the Internet page of the control bar, select the SeverSocket control and ClientSocket control and place it in the form.
Set the Port attribute of the SeverSocket control and ClientSocket control to "1100 ". The SeverSocket control is transmitted over TCP/IP protocol.
Server Side control, which is mainly used to listen to other TCP/IP-based transmission computer connection requests, and when receiving the connection request
Establish a connection for data transmission. The ClientSocket control is a control of the client based on TCP/IP transmission. Its main function is to listen
The TCP/IP Transmission server sends a connection request. After receiving a response from the server that allows the connection, the server establishes a connection and transmits data.
Create both the ServerSocket and ClientSocket controls in the form because the application can be used as both a server and a client.
3. connections between Serversocket and ClientSocket
First, set two global variables:
NickName: string;
B _Client: boolean;
Here, NickName is used to display the name of the chatbot, and B _Client is used to indicate whether the application is used as a client for data transmission.
Initialize the variable in the Oncreate event of form Form1. The Code is as follows: Procedure TForm1.FormCreate (Sender: TObject ); Begin NickName: + = "My NickName "; B _Client: = ture; End; Double-click ToolButton1 and write the server listening Code as follows: Procedure TForm1.Toolbutton1Click (Sender: TObject ); Begin ClientSocket1.close; ServerSocket1.open; StatusBar1.SimpleText: = 'start listening '; End; Double-click ToolButton2 to write the client application connection. The Code is as follows: Procedure TForm1.ToolButton2Click (Sender: TObject ); Var s: string; Begin If Clientsocket1.Active then ClientSocket1.close; If InputQuery ('connect to a computer ', 'name or IP address of the computer to be connected:', s) then If Length (s)> 0 then With ClientSocket1 do Begin Host: = s; Open; End; End; In the object manager, double-click the oning ing event on the ClientSocket event page to write and process the client waiting for connection requests. As follows: Procedure TForm1.ClientSocket1Connecting (Sender: TObject; Socket: TCustomWinSocket ); Begin StatusBar1.SimpleText: = 'Wait for the connection from '+ Socket. RemoteAddress +' to allow the response ...'; End; In Object Manager, double-click the OnAccept event on the SeverSocket event page to process the server's response to the connection event. The Code is as follows: Procedure TForm1.SeverSocket1Accept (Sender: TObject; Socket: TCustomWinSocket ); Begin B _Client: = false; StatusBar1.SimpleText: = 'connection' + Socket. RemoteAddress; End; In Object Manager, double-click the OnConnect event on the ClientSocket event page. The OnConnect event is called when the connection is successful. The Code is as follows: Procedure TForm1.ClientSocket1Connect (Sender: TObject; Socket: TCustomWinSocket ); Begin B _Client: = ture; StatusBar1.SimpleText: = 'Connection successful '; End; |
4. data transmission between ServerSocket and ClientSocket
The chat content is input through the Edit Control and displayed in the Memo control after you press the Enter key, and then transmitted to the connected computer.
The OnKeyDown Event code of Edit is as follows: Procedure TForm1.Edit1KeyDown (Sender: TObject; var Key: Word; Shift: TShiftState ); Begin If Key = VK_Return then Begin Memo1.Lines. Add (NickName + ':' + Edit1.Text0; If B _Client then ClientSocket1.Socket. SendText (Memo1.Lines [Memo1.lines. Count-1]) Else ServerSocket1.Socket. Connections [0]. SendText (Memo1.Lines [Memo1.lines. Count-1]); End; End; Write the action after the server receives data in the onread event of the ServerSocket control. The Code is as follows: Procedure TForm1.ServerSocket1ClientRead (Sender: TObject; Socket: TCustomWinSocket ); Begin Memo1.lines. Add (Socket. ReceiveText ); End; Write the action after the client receives the data in the onread event of the ClientSocket control. The Code is as follows: Procedure TForm1.ClientSocket1Read (Sender: TObject; Socket: TCustomWinSocket ); Begin Memo1.lines. Add (Socket. ReceiveText ); End; |
5. Disconnect Serversocket from ClientSocket
Double-click ToolButton3 to write the process of client disconnection. The Code is as follows: Procedure TForm1.ToolButton3Click (Sender: TObject ); Begin ClientSocket1.close; StatusBar1.SimpleText: = 'disconnected '; End; Compile the process for the server to respond to client disconnection. The Code is as follows: Procedure TForm1.ServerSocket1ClientDisconnect (Sender: TObject; Socket: TCustomWinSocket );
Begin SeverSocket1.close; StatusBar1.SimpleText: = 'disconnected '; End; |
6. Change the nickname of the chatbot.
Double-click Toolbutton4 and write the code to change the nickname as follows: Procedure TForm1.ToolButton4Click (sender: TObject ); Var S: string; Begin If InputQuery ('change nickname ', 'your new nickname', s) then If Length (s)> 0 then NickName: = s; End; |
7. Exit the application
Double-click Toolbutton5 and write the code to exit the application as follows: Procedure TForm1.ToolButton5Click (sender: TObject ); ClientSocket1.close; ServerSocket1.close; Form1.close; End; |
8. Save and run the application
It is best to run the program on the Internet. If the program is not connected to the Internet, but your computer supports the TCP/IP protocol, you can install the TCP/IP protocol through the network neighbors ),
You can run two instances of the application from my computer on your computer. After running, use a chat program as the Server Monitor
Listen, another chat program serves as a client to connect to and chat with the server. The LAN can also run !!!
Related Articles]
- Analysis and Summary of TCP/IP attack principles
- Detailed description of TCP/IP protocol datagram Structure
- Interpreting TCP/IP protocol by connecting to an instance