Click here to Skip to main content
15,888,170 members
Home / Discussions / C#
   

C#

 
AnswerRe: Extract XML values using XpathNavigator Pin
PIEBALDconsult17-May-13 4:39
mvePIEBALDconsult17-May-13 4:39 
GeneralRe: Extract XML values using XpathNavigator Pin
baranils17-May-13 4:56
baranils17-May-13 4:56 
AnswerRe: Extract XML values using XpathNavigator Pin
Abhinav S17-May-13 19:08
Abhinav S17-May-13 19:08 
AnswerRe: Extract XML values using XpathNavigator Pin
Alan Balkany20-May-13 11:32
Alan Balkany20-May-13 11:32 
QuestionA question on WCF Pin
Dewald17-May-13 0:56
Dewald17-May-13 0:56 
AnswerRe: A question on WCF Pin
Richard MacCutchan17-May-13 1:06
mveRichard MacCutchan17-May-13 1:06 
GeneralRe: A question on WCF Pin
Dewald17-May-13 2:35
Dewald17-May-13 2:35 
QuestionHow to Conect two computers in different networks Pin
AntonioJesus17-May-13 0:35
professionalAntonioJesus17-May-13 0:35 
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
Pete O'Hanlon17-May-13 0:37
mvePete O'Hanlon17-May-13 0:37 
GeneralRe: How to Conect two computers in different networks PinPopular
AntonioJesus17-May-13 0:46
professionalAntonioJesus17-May-13 0:46 
GeneralRe: How to Conect two computers in different networks Pin
Keith Barrow17-May-13 3:03
professionalKeith Barrow17-May-13 3:03 
QuestionC# Excel- Paste HTML table in excel sheet Pin
raesark17-May-13 0:35
raesark17-May-13 0:35 
AnswerRe: C# Excel- Paste HTML table in excel sheet Pin
Richard MacCutchan17-May-13 1:04
mveRichard MacCutchan17-May-13 1:04 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
raesark17-May-13 1:11
raesark17-May-13 1:11 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
Richard MacCutchan17-May-13 1:37
mveRichard MacCutchan17-May-13 1:37 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
raesark17-May-13 1:46
raesark17-May-13 1:46 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
Eduardo Antonio Cecilio Fernandes17-May-13 2:00
Eduardo Antonio Cecilio Fernandes17-May-13 2:00 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
Richard MacCutchan17-May-13 2:20
mveRichard MacCutchan17-May-13 2:20 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
raesark17-May-13 2:29
raesark17-May-13 2:29 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
Richard MacCutchan17-May-13 3:00
mveRichard MacCutchan17-May-13 3:00 
GeneralRe: C# Excel- Paste HTML table in excel sheet Pin
raesark17-May-13 3:10
raesark17-May-13 3:10 
Questionreplace a Text in winword within a range Pin
Cyrus-IRA16-May-13 20:38
Cyrus-IRA16-May-13 20:38 
AnswerRe: replace a Text in winword within a range Pin
BillWoodruff16-May-13 21:38
professionalBillWoodruff16-May-13 21:38 
QuestionHow to verify the signature signed by Java? Pin
Jun Du16-May-13 11:16
Jun Du16-May-13 11:16 
AnswerRe: How to verify the signature signed by Java? Pin
Abhinav S16-May-13 16:50
Abhinav S16-May-13 16: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.