User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Programación en C++ Builder

 Arrays (arrays)
 Pointers
3. Example of class in c + +
8. AnsiString class methods
 C + + projects
 Packages, distribute an application without installation
 Exchange or bubble sorting
 String string.h functions

12.1. Example TCP / IP chat

Chat Form Socket example TCP / IP

Considerations:

Establish a communications port valid for ServerSocket ClientSocket and Properties -> Port, such as 1024.

Add a Panel control StatusBar1 Properties -> Panels -> Add New.

main.cpp

 //------------------------------------------------ ---------------------------
# Include 
# Pragma hdrstop

# Include "main.h"
//------------------------------------------------ ---------------------------
# Pragma link "ScktComp"
# Pragma resource "*. dfm"
TChatForm * ChatForm;
//------------------------------------------------ ---------------------------
TChatForm __fastcall:: TChatForm (TComponent * Owner): TForm (Owner)
{
}
//------------------------------------------------ ---------------------------
/ / Enable Socket Server and specify the listening mode
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: CheckBoxListenClick (TObject * Sender)
{
  / / CheckBoxList-> Checked =! CheckBoxList-> Checked;
  if (CheckBoxList-> Checked) {
     ClientSocket-> Active = false;
     ServerSocket-> Active = true;
     StatusBar1-> Panels-> Items [0] -> Text = "Listening ...";
  Else {}
     if (ServerSocket-> Active) {
        ServerSocket-> Active = false;
     }
     StatusBar1-> Panels-> Items [0] -> Text = "";
  }
}
//------------------------------------------------ ---------------------------
/ / Connect to the specified host
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ConnectClick (TObject * Sender)
{
  / / If a connection is active, it has to close before establishing a new
  if (ClientSocket-> Active) {
      ClientSocket-> Active = false;
  }
  / / Connect to Server
  if (InputQuery ("Computer to connect to", "Address Name:", server)) {/ / Ask the server IP
     if (Server.Length ()> 0) {
        ClientSocket-> Host = Server; / / Specify the Host IP
        ClientSocket-> Active = true, / / ​​Connect to Server
        / / CheckBoxList-> Checked = false; / / Close the server and leave the listening mode
        / / If you stop the server, you can not make a connection to localhost
     }
  }
}
//------------------------------------------------ ---------------------------
/ / Close and exit communications
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ExitClick (TObject * Sender)
{
  ServerSocket-> Close (); / / Close the Socket Server
  ClientSocket-> Close (); / / Close the Socket Client
  Close (); / / Exit Program
}
//------------------------------------------------ ---------------------------
/ / Transmit Data
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: Memo1KeyDown (TObject * Sender, WORD & Key, TShiftState Shift)
{
  if (Key == VK_RETURN) {/ / If pressed the carriage return
     / / If we act as a server, a client has connected to us
     if (IsServer) {
         / / Send the current line
         ServerSocket-> Socket-> Connections [0] -> sendtext (Memo1-> Lines-> Strings [Memo1-> Lines-> Count - 1]);
     }
     / / If we act as a client, you connect to a server
     else {
         / / Send the current line
         ClientSocket-> Socket-> sendtext (Memo1-> Lines-> Strings [Memo1-> Lines-> Count -1]);
     }
  }
}
//------------------------------------------------ ---------------------------
/ / When starting the program
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: FormCreate (TObject * Sender)
{
  CheckBoxList-> Checked = true, / / ​​Enable Socket and enter listening mode
}
//------------------------------------------------ ---------------------------
/ / When connecting
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ClientSocketConnect (TObject * Sender, TCustomWinSocket * Socket)
{
  StatusBar1-> Panels-> Items [0] -> Text = "Connect to:" + Socket-> RemoteHost;
} 

 //------------------------------------------------ ---------------------------
/ / Stop or disconnect the connection
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: DisconnectClick (TObject * Sender)
{
  ClientSocket-> Active = false; / / Close the connection to the server
  ServerSocket-> Active = true, / / ​​Enable and enter ServerSocket listening mode
  StatusBar1-> Panels-> Items [0] -> Text = "Listening ...";
}
//------------------------------------------------ ---------------------------
/ / Occurs when a client socket should read the socket connection.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ClientSocketRead (TObject * Sender, TCustomWinSocket * Socket)
{
  / / When the server sends this data to the coefficient and the process has
  Memo2-> Lines-> Add (Socket-> ReceiveText ()) / / Add data to the memory reception
}
//------------------------------------------------ ---------------------------
/ / Occurs when the server socket should read the information from a client socket.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ServerSocketClientRead (TObject * Sender, TCustomWinSocket * Socket)
{
  / / When the client sends data to the server and that the process has
  Memo2-> Lines-> Add (Socket-> ReceiveText ()) / / Add data to the memory reception
}
//------------------------------------------------ ---------------------------
/ / It occurs on a socket server immediately after accepting the connection to a client socket.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ServerSocketAccept (TObject * Sender, TCustomWinSocket * Socket)
{
  IsServer = true, / / ​​act as server
  StatusBar1-> Panels-> Items [0] -> Text = "Connect to:" + Socket-> RemoteAddress;
}
//------------------------------------------------ ---------------------------
/ / Occurs when a full client socket connection accepted by the server socket.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ServerSocketClientConnect (TObject * Sender, TCustomWinSocket * Socket)
{
  Memo2-> Lines-> Clear (); / / Clean the data memo
}
//------------------------------------------------ ---------------------------
/ / It occurs just before a closed client socket connection to a server.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ClientSocketDisconnect (TObject * Sender, TCustomWinSocket * Socket)
{
  / / CheckBoxList-> Checked = true, / / ​​Enable Socket and enter listening mode
  / / Only if it is for the server when acting as a client
}
//------------------------------------------------ ---------------------------
/ / When the socket connection fails, use, or close a connection.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ClientSocketError (TObject * Sender, TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode)
{
  Memo2-> Lines-> Add ("Error connecting to:" + Server);
  ErrorCode = 0;
}
//------------------------------------------------ ---------------------------
/ / Occurs when a connection to a client socket is closed.
//------------------------------------------------ ---------------------------
void __fastcall TChatForm: ServerSocketClientDisconnect (TObject * Sender, TCustomWinSocket * Socket)
{
  StatusBar1-> Panels-> Items [0] -> Text = "Listening ...";
} 

main.h

 //------------------------------------------------ --------------------------- 
# Ifndef mainha
# Define mainha
//------------------------------------------------ ---------------------------
# Include <Classes.hpp>
# Include <Controls.hpp>
# Include <StdCtrls.hpp>
# Include <Forms.hpp>
# Include "ScktComp.hpp"
# Include <Buttons.hpp>
# Include <ComCtrls.hpp>
# Include <ExtCtrls.hpp>
# Include <Menus.hpp>
//------------------------------------------------ ---------------------------
TChatForm class: public TForm
{
__published: / / IDE-managed Components
TStatusBar * StatusBar1;
TPanel * Panel1;
TMemo * Memo1;
TMemo * Memo2;
TServerSocket * ServerSocket;
TClientSocket * ClientSocket;
TButton * Connect;
TCheckBox * CheckBoxList;
TButton * Disconnect;
TButton * Exit;
void __fastcall Memo1KeyDown (TObject * Sender, WORD & Key,
TShiftState Shift);
void __fastcall FormCreate (TObject * Sender);
void __fastcall ClientSocketConnect (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ClientSocketDisconnect (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ClientSocketRead (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ServerSocketClientRead (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ServerSocketAccept (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ServerSocketClientConnect (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall ClientSocketError (TObject * Sender,
TCustomWinSocket * Socket, TErrorEvent ErrorEvent, int & ErrorCode);
void __fastcall ServerSocketClientDisconnect (TObject * Sender,
TCustomWinSocket * Socket);
void __fastcall CheckBoxListenClick (TObject * Sender);
void __fastcall ConnectClick (TObject * Sender);
void __fastcall DisconnectClick (TObject * Sender);
void __fastcall ExitClick (TObject * Sender);
private: / / User declarations
public: / / User declarations
bool IsServer / / act as server
Server String / / IP to connect to server
__fastcall TChatForm (TComponent * Owner);
};

//------------------------------------------------ ---------------------------
extern TChatForm * ChatForm;
//------------------------------------------------ ---------------------------
# Endif

Loading
copyright © 2007-2014  www.alciro.org  All rights reserved.         
Share |