You are on page 1of 4

Chat Room Socket (Delphi)

Contents

[hide]

 1 Description

 2 Code

 3 Uses

o 3.1 See Also

Description

This example builds a Chat Room using sockets components. To build the example, follow the instructions:

1. Install the socket components.

2. Create a VCL Forms Application and save it as Server.

3. In the Project Manager, right-click ProjectGroup1 > Add New Project and add a new VCL Forms Application. Save it as Client.

4. Add two buttons (Send and Start/Stop), a memo box, an edit box, and a TServerSocket from the Tool Palette to the Server form.

5. Add two buttons (Send and Connect/Disconnect), a memo box, an edit box, and a TClientSocket from the Tool Palette to the Client form.

Code

6. In Server.pas

1. Add to the TForm1 class a private variable, Str, of type String, in which to save the messages received or sent by the server.

2. Add the code below to the OnClick event of the Start button:

3. procedure TForm1.Button2Click(Sender: TObject);


4. begin
5. if(ServerSocket1.Active = False)//The button caption is ‘Start’
6. then
7. begin
8. ServerSocket1.Active := True;//Activates the server socket
9. Memo1.Text:=Memo1.Text+'Server Started'+#13#10;
10. Button2.Caption:='Stop';//Set the button caption
11. end
12. else//The button caption is ‘Stop’
13. begin
14. ServerSocket1.Active := False;//Stops the server socket
15. Memo1.Text:=Memo1.Text+'Server Stopped'+#13#10;
16. Button2.Caption:='Start';
17. //If the server is closed, then it cannot send any messages
18. Button1.Enabled:=false;//Disables the “Send” button
19. Edit1.Enabled:=false;//Disables the edit box
20. end;
end;
21. Add the code below to the OnClick event of the Send button:

22. procedure TForm2.Button1Click(Sender: TObject);


23. var
24. i: integer;
25. begin
26. Str:=Edit1.Text;//Take the string (message) sent by the server
27. Memo1.Text:=Memo1.Text+'me: '+Str+#13#10;//Adds the message to the memo box
28. Edit1.Text:='';//Clears the edit box
29. //Sends the messages to all clients connected to the server
30. for i:=0 to ServerSocket1.Socket.ActiveConnections-1 do
31. ServerSocket1.Socket.Connections[i].SendText(str);//Sent
end;

32. Add the code below to the OnClientConnect event of TServerSocket:

33. procedure TForm1.ServerSocket1ClientConnect(Sender: TObject; Socket:


TCustomWinSocket);
34. begin
35. Socket.SendText('Connected');//Sends a message to the client
36. //If at least a client is connected to the server, then the server can
communicate
37. //Enables the Send button and the edit box
38. Button1.Enabled:=true;
39. Edit1.Enabled:=true;
end;

40. Add the code below to the OnClientDisconnect event of TServerSocket:

41. procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;


42. Socket: TCustomWinSocket);
43. Begin
44. //The server cannot send messages if there is no client connected to it
45. if ServerSocket1.Socket.ActiveConnections-1=0 then
46. begin
47. Button1.Enabled:=false;
48. Edit1.Enabled:=false;
49. end;
end;

50. Add the code below to the OnClientRead event of TServerSocket:

51. procedure TForm1.ServerSocket1ClientRead(Sender: TObject;


52. Socket: TCustomWinSocket);
53. Begin
54. //Read the message received from the client and add it to the memo text
55. // The client identifier appears in front of the message
56.
Memo1.Text:=Memo1.Text+'Client'+IntToStr(Socket.SocketHandle)+' :'+Socket.Receive
Text+#13#10;
end;

7. In Client.pas

1. Add to the TForm2 class a private variable, Str, of type String, in which to save the messages received or sent by the client.

2. Add the code below to the OnClick event of the Connect button:

3. procedure TForm2.Button2Click(Sender: TObject);


4. begin
5. //127.0.0.1 is the standard IP address to loop back to your own machine
6. ClientSocket1.Address:='127.0.0.1';
7. ClientSocket1.Active := True;//Activates the client
8.
9. if(ClientSocket1.Socket.Connected=True)
10. then
11. begin
12. str:='Disconnected';
13. ClientSocket1.Active := False;//Disconnects the client
14. Button2.Caption:='Connect';
15. end;
end;

16. Add the code below to the OnClick event of the Send button:

17. procedure TForm2.Button1Click(Sender: TObject);


18. begin
19. Str:=Edit1.Text;
20. Memo1.Text:=Memo1.Text+'me: '+str+#13#10;
21. Edit1.Text:='';//Clears the edit box
22. ClientSocket1.Socket.SendText(str);//Send the messages to the server
end;

23. Add the code below to the OnDisconnect event of TClientSocket:

24. procedure TForm2.ClientSocket1Disconnect(Sender: TObject; Socket:


TCustomWinSocket);
25. begin
26. Memo1.Text:=Memo1.Text+'Disconnect'+#13#10;
27. Socket.SendText(str);//Send the “Disconnected” message to the server
28. //str is set to “Disconnected” when the Disconnect button is pressed
29. //A client cannot send messages if it is not connected to a server
30. Button1.Enabled:=False;
31. Edit1.Enabled:=False;
32. Button2.Caption:='Connect';
end;

33. Add the code below to the OnError event of TClientSocket:

34. procedure TForm2.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket;


ErrorEvent: TErrorEvent; var ErrorCode: Integer);
35. begin
36. ErrorCode:=0;
37. ClientSocket1.Active := False;
38. // This can happen when no active server is started
39. Memo1.Text:=Memo1.Text+'No connection found'+#13#10;
end;

40. Add the code below to the OnRead event of TClientSocket:

41. procedure TForm1.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);


42. begin
43. //Reads and displays the message received from the server;
44. Memo1.Text:=Memo1.Text+'Server: '+Socket.ReceiveText+#13#10;
end;

To test the example:

1. In the Project Manager, right-click Server.exe and then press Run.

2. Right-click Client.exe and then Run without debugging.

3. Start the Server by pressing the Start button from the server form.

4. Connect the Client by pressing the Connect button from the client form.

5. Start sending messages from server to client, and from client to server.

Uses

 System.Win.ScktComp.TServerSocket

 System.Win.ScktComp.TClientSocket
See Also

 Installing Socket Components

 Chat Room Socket (C++)

You might also like