Click here to Skip to main content
15,887,214 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionIMPLEMENT_RUNTIMECLASS_T Pin
john563218-May-13 21:38
john563218-May-13 21:38 
GeneralRe: IMPLEMENT_RUNTIMECLASS_T Pin
Richard MacCutchan19-May-13 1:19
mveRichard MacCutchan19-May-13 1:19 
QuestionButton Ownerdraw - Transparency Issue Pin
Jijo.Raj18-May-13 17:16
Jijo.Raj18-May-13 17:16 
Questiongenerate password using brute algo? Pin
Le@rner17-May-13 20:34
Le@rner17-May-13 20:34 
AnswerRe: generate password using brute algo? Pin
MicroVirus18-May-13 12:58
MicroVirus18-May-13 12:58 
Questionglut.h header package doesnt work Pin
Yashwanth. C.b17-May-13 19:01
Yashwanth. C.b17-May-13 19:01 
AnswerRe: glut.h header package doesnt work Pin
Newbie0017-May-13 22:28
Newbie0017-May-13 22:28 
QuestionHow to Conect two computers in different networks Pin
AntonioJesus17-May-13 0:48
professionalAntonioJesus17-May-13 0:48 
Hello,

I've just started to programm in C++, using Builder C++. I need to stablish comunication via internet between two computers. I've succed using the ClientSocket Component and the ClientServer but only if the computers are in a local network but not if the computers are in different networks. Here is my code for the server:

C++
// BCB VCL TCP Message Server
// Chin-Shiuh Shieh
// 2002-04-04
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

//--------------------Abrir el Servidor---------------------------------------//

void __fastcall TForm1::BAbrirClick(TObject *Sender)
{
//Configurar el número de puerto
        ServerSocket1->Port=StrToInt(Npuerto->Text);

//Abrir el servidor
        ServerSocket1->Open();

//Actualizar estado de los botones
        BAbrir->Enabled=false;
        BCerrar->Enabled=true;

//Informar de que se ha abierto el servidor
        BEstado->SimpleText="Servidor Conectado!";

//Indicar el número de conexionoes activas
        NOnline->Text=IntToStr(ServerSocket1->Socket->ActiveConnections);

//Activar el Boton de enviar mensaje
        BEnviar->Enabled = true;
}

//----------------------------------------------------------------------------//

//---------------------Cerrar el Servidor-------------------------------------//

void __fastcall TForm1::BCerrarClick(TObject *Sender)
{
//Cerrar el Servicio
        ServerSocket1->Close();

//Actualizar estado de los botones
        BAbrir->Enabled=true;
        BCerrar->Enabled=false;

//Informar del cierre del servidor
        BEstado->SimpleText="Servidor Cerrado!";

//Actualizar el campo de nº de conectados
        NOnline->Text=IntToStr(ServerSocket1->Socket->ActiveConnections);

//Desactivar el Boton de enviar mensaje
        BEnviar->Enabled = false;
}

//----------------------------------------------------------------------------//

//---------------------Cuando se Conecte un Cliente---------------------------//

void __fastcall TForm1::ServerSocket1ClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
//Mostrar aviso en la barra de estado
        BEstado->SimpleText="Conectado desde  "+Socket->RemoteAddress;

//Actualizar el número de conectados
        NOnline->Text=IntToStr(ServerSocket1->Socket->ActiveConnections);
}


//----------------------------------------------------------------------------//

//---------------------Al Desconectarse un Cliente----------------------------//

void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
//Actualizar el número de conectados
        NOnline->Text=IntToStr(ServerSocket1->Socket->ActiveConnections-1);

//Informar de la desconexión
        BEstado->SimpleText="Desconectado de "+Socket->RemoteAddress;
}
//----------------------------------------------------------------------------//

//---------------------Al recibir un Mensaje----------------------------------//

void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
//Espacio para recibir el mensaje
char * buffer;
        int len;
        AnsiString Mensaje;
        int i;

// Recibir mensaje
int *tam;
tam = new int;
*tam = Socket->ReceiveLength();

len=Socket->ReceiveBuf(buffer,*tam);
buffer[len]=0;

//Estructura para mostrar el mensaje correctamente
TTime hora = TTime::CurrentTime();
AnsiString MensajeIn = Socket->RemoteAddress;
        MensajeIn += " A las " + TimeToStr(hora) + " Dice" "----->";

ChatBox->Lines->Add(MensajeIn +StrPas(buffer));
BEstado->SimpleText=IntToStr(len)+"Nuevo mensaje entrante!";

// Repetir el mensaje a los demás
Mensaje = StrPas(buffer);
strcpy(buffer,Mensaje.c_str());
for(i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
    ServerSocket1->Socket->Connections[i]->SendBuf(buffer,strlen(buffer));


delete[] buffer;
}

//----------------------------------------------------------------------------//

//--------------------Enviar un Mensaje---------------------------------------//
void __fastcall TForm1::BEnviarClick(TObject *Sender)
{
//Espacio para meter el mensaje
 char buffer[256];
 int i;

 //Recoger el Mensaje del campo de entrada
AnsiString Mensaje = CampoMensaje->Text;
strcpy(buffer,Mensaje.c_str());

//Enviarlo a los conectados
for(i=0;i<ServerSocket1->Socket->ActiveConnections;i++)
    ServerSocket1->Socket->Connections[i]->SendBuf(buffer,strlen(buffer));

//Añadirlo al Chatbox
TTime hora = TTime::CurrentTime();
        ChatBox->Lines->Add("Servidor a las " +TimeToStr(hora)
        + " dice----->" + Mensaje);

}
//----------------------------------------------------------------------------//

//---------------------Borrar la Memo-----------------------------------------//
void __fastcall TForm1::LimpiarClick(TObject *Sender)
{
//Borrar
        ChatBox->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::CampoMensajeKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if (Key == 13)
            TForm1::BEnviarClick(CampoMensaje);        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::NpuertoKeyUp(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
        if (Key == 13)
            TForm1::BAbrirClick(Npuerto);         
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TcpServer1Accept(TObject *Sender,
      TCustomIpClient *ClientSocket)
{
//Configurar el número de puerto
        TcpServer1->RemotePort = StrToInt(Npuerto->Text);

//Abrir el servidor
        TcpServer1->Open();

//Actualizar estado de los botones
        BAbrir->Enabled=false;
        BCerrar->Enabled=true;

//Informar de que se ha abierto el servidor
        BEstado->SimpleText="Servidor Conectado!";

//Indicar el número de conexionoes activas
        NOnline->Text=IntToStr(ServerSocket1->Socket->ActiveConnections);

//Activar el Boton de enviar mensaje
        BEnviar->Enabled = true;
        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TcpServer1CreateHandle(TObject *Sender)
{
//Actualizar el número de conectados
        NOnline->Text=IntToStr(TcpServer1->Active-1);

//Informar de la desconexión
        BEstado->SimpleText="Desconectado de "+TcpServer1->LocalHostName();
}
//---------------------------------------------------------------------------


How can I implement comunication (a simple chat is enough) between the two computers?
AnswerRe: How to Conect two computers in different networks Pin
Richard MacCutchan17-May-13 1:01
mveRichard MacCutchan17-May-13 1:01 
AnswerRe: How to Conect two computers in different networks Pin
Garth J Lancaster17-May-13 1:03
professionalGarth J Lancaster17-May-13 1:03 
GeneralRe: How to Conect two computers in different networks Pin
AntonioJesus17-May-13 1:18
professionalAntonioJesus17-May-13 1:18 
AnswerRe: How to Conect two computers in different networks Pin
Jochen Arndt17-May-13 2:24
professionalJochen Arndt17-May-13 2:24 
AnswerRe: How to Conect two computers in different networks Pin
David Crow17-May-13 5:06
David Crow17-May-13 5:06 
QuestionOOP Project Ideas Pin
dirtyfishtank16-May-13 19:30
dirtyfishtank16-May-13 19:30 
AnswerRe: OOP Project Ideas Pin
MicroVirus17-May-13 1:14
MicroVirus17-May-13 1:14 
GeneralRe: OOP Project Ideas Pin
dirtyfishtank23-May-13 12:45
dirtyfishtank23-May-13 12:45 
AnswerRe: OOP Project Ideas Pin
Erudite_Eric19-May-13 22:43
Erudite_Eric19-May-13 22:43 
QuestionWhy does this compile on Linux with gcc and not on WIndows with VC? Pin
Erudite_Eric16-May-13 7:45
Erudite_Eric16-May-13 7:45 
AnswerRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Chris Losinger16-May-13 7:56
professionalChris Losinger16-May-13 7:56 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Erudite_Eric16-May-13 8:06
Erudite_Eric16-May-13 8:06 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Erudite_Eric16-May-13 8:08
Erudite_Eric16-May-13 8:08 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Erudite_Eric16-May-13 8:16
Erudite_Eric16-May-13 8:16 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Chris Losinger16-May-13 8:51
professionalChris Losinger16-May-13 8:51 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
CPallini16-May-13 9:36
mveCPallini16-May-13 9:36 
GeneralRe: Why does this compile on Linux with gcc and not on WIndows with VC? Pin
Erudite_Eric17-May-13 4:50
Erudite_Eric17-May-13 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.