home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 May / Chip_2002-05_cd1.bin / chplus / cpp / 5 / chat.exe / main.cpp < prev    next >
C/C++ Source or Header  |  1998-03-23  |  5KB  |  110 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "main.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TChatForm *ChatForm;
  10. //---------------------------------------------------------------------------
  11. __fastcall TChatForm::TChatForm(TComponent* Owner)
  12.     : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. __fastcall TChatForm::~TChatForm(){
  17. }
  18. //---------------------------------------------------------------------------
  19. void __fastcall TChatForm::FileListenItemClick(TObject* Sender){
  20.     FileListenItem->Checked = !FileListenItem->Checked;
  21.     if (FileListenItem->Checked){
  22.         ClientSocket->Active = false;
  23.         ServerSocket->Active = true;
  24.         StatusBar1->Panels->Items[0]->Text = "Listening...";
  25.     } else {
  26.         if (ServerSocket->Active)
  27.             ServerSocket->Active = false;
  28.         StatusBar1->Panels->Items[0]->Text = "";
  29.     }
  30. }
  31. //---------------------------------------------------------------------------
  32. void __fastcall TChatForm::FileConnectItemClick(TObject* Sender){
  33.     if (ClientSocket->Active) ClientSocket->Active = false;
  34.  
  35.     if (InputQuery("Computer to connect to", "Address Name:", Server)){
  36.         if (Server.Length() > 0){
  37.             ClientSocket->Host = Server;
  38.             ClientSocket->Active = true;
  39.         }
  40.     }
  41. }
  42. //---------------------------------------------------------------------------
  43. void __fastcall TChatForm::Exit1Click(TObject* Sender){
  44.     ServerSocket->Close();
  45.     ClientSocket->Close();
  46.     Close();
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TChatForm::Memo1KeyDown(TObject* Sender, Word &Key, TShiftState Shift){
  50.     if (Key == VK_RETURN){
  51.         if (IsServer)
  52.             ServerSocket->Socket->Connections[0]->SendText(Memo1->Lines->Strings[Memo1->Lines->Count - 1]);
  53.         else
  54.             ClientSocket->Socket->SendText(Memo1->Lines->Strings[Memo1->Lines->Count - 1]);
  55.     }
  56. }
  57. //---------------------------------------------------------------------------
  58. void __fastcall TChatForm::FormCreate(TObject* Sender){
  59.     FileListenItemClick(NULL);
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TChatForm::ServerSocketError(TObject* Sender, short Number, AnsiString &Description, int Scode
  63.         , const AnsiString Source, const AnsiString HelpFile, int HelpContext, Word &CancelDisplay){
  64.     ShowMessage(Description);
  65. }
  66. //---------------------------------------------------------------------------
  67. void __fastcall TChatForm::Disconnect1Click(TObject* Sender){
  68.     ClientSocket->Close();
  69.     FileListenItemClick(NULL);
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TChatForm::ClientSocketConnect(TObject* Sender, TCustomWinSocket* Socket){
  73.     StatusBar1->Panels->Items[0]->Text = "Connected to: " + Socket->RemoteHost;
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TChatForm::ClientSocketRead(TObject* Sender, TCustomWinSocket* Socket){
  77.     Memo2->Lines->Add(Socket->ReceiveText());
  78. }
  79. //---------------------------------------------------------------------------
  80. void __fastcall TChatForm::ServerSocketClientRead(TObject* Sender, TCustomWinSocket* Socket){
  81.     Memo2->Lines->Add(Socket->ReceiveText());
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TChatForm::ServerSocketAccept(TObject* Sender, TCustomWinSocket* Socket){
  85.     IsServer = true;
  86.     StatusBar1->Panels->Items[0]->Text = "Connected to: " + Socket->RemoteAddress;
  87. }
  88. //---------------------------------------------------------------------------
  89. void __fastcall TChatForm::ServerSocketClientConnect(TObject* Sender, TCustomWinSocket* Socket){
  90.     Memo2->Lines->Clear();
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TChatForm::ClientSocketDisconnect(TObject* Sender, TCustomWinSocket* Socket){
  94.     FileListenItemClick(NULL);
  95. }
  96. //---------------------------------------------------------------------------
  97. void __fastcall TChatForm::ClientSocketError(TObject* Sender, TCustomWinSocket* Socket, TErrorEvent ErrorEvent
  98.     , int &ErrorCode){
  99.     Memo2->Lines->Add("Error connecting to : " + Server);
  100.     ErrorCode = 0;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TChatForm::ServerSocketClientDisconnect(TObject* Sender, TCustomWinSocket* Socket){
  104.     ServerSocket->Active = false;
  105.     FileListenItem->Checked = !FileListenItem->Checked;
  106.     FileListenItemClick(NULL);
  107. }
  108. //---------------------------------------------------------------------------
  109.  
  110.