Ver Mensaje Individual
  #2  
Antiguo 19-03-2005
Avatar de OSKR
OSKR OSKR is offline
Miembro
 
Registrado: nov 2004
Ubicación: San Cristóbal/Táchira/Venezuela
Posts: 389
Reputación: 20
OSKR Va por buen camino
Thumbs up Un ejemplo con hilos de la API de GUINDOWS

Te muestro el ejemplo en Builder, los componentes usados los ha de tener también el Delphi, corres el servidor una vez y el cliente cuantas veces quieras, en el primer Edit das el ip de donde esta el servidor y luego clickeas en conectar y mas nada, ahi van el .cpp, .h, .dfm
Código Delphi [-]
/*SocketServidor.cpp*/
#include 
#pragma hdrstop
#include "SocketServidor.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int Last=0;
//------------------------------------------------------------------------------
DWORD WINAPI ThreadFunc(LPVOID Param)
{ TCustomWinSocket *Socket=(TCustomWinSocket *)Param;
  int i=-1;
  while(true)
  { AnsiString Cad=AnsiString(++i);
    if(Socket->Connected)
      Socket->SendBuf(Cad.c_str(),Cad.Length()+1);
    else
      return 0;
    Sleep(990);
  }
}
//------------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{ ServerSocket->Active=true;
}
void __fastcall TForm1::ServerSocketAccept(TObject *Sender,TCustomWinSocket *Socket)
{ ShowMessage("Conexión: "+IntToStr(++Last));
  DWORD Id;
  HANDLE Thread;
  Thread = CreateThread(0, 0, ThreadFunc,Socket, CREATE_SUSPENDED, &Id);
  if(!Thread)
  { ShowMessage("Error! Cannot create thread.");
    Application->Terminate();
  }
  ResumeThread(Thread);
}
void __fastcall TForm1::ServerSocketClientRead(TObject *Sender,TCustomWinSocket *Socket)
{ int lngtd=Socket->ReceiveLength();
  char *Buffer=new char[lngtd+1];
  AnsiString AS;
  Socket->ReceiveBuf(Buffer,lngtd);
  AS=AnsiString(Buffer);
  for(int i=0;iSocket->Connections[i]->SendBuf(Buffer,lngtd);
       ShowMessage("Puerto"+IntToStr(ServerSocket->Socket->Connections[i]->RemotePort));
     }
}
//------------------------------------------------------------------------------
void __fastcall TForm1::ServerSocketClientError(TObject *Sender,TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{ Socket->Close();
  ErrorCode=0;
}
//------------------------------------------------------------------------
 
/*SocketServidor.h*/
#ifndef SocketServidorH
#define SocketServidorH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TServerSocket *ServerSocket;
        void __fastcall ServerSocketAccept(TObject *Sender,
          TCustomWinSocket *Socket);
        void __fastcall ServerSocketClientRead(TObject *Sender,
          TCustomWinSocket *Socket);
        void __fastcall ServerSocketClientError(TObject *Sender,
          TCustomWinSocket *Socket, TErrorEvent ErrorEvent,
          int &ErrorCode);
private: // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
 
/*SocketServidor.dfm*/
object Form1: TForm1
Left = 0
Top = 978
Width = 344
Height = 192
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Position = poDesktopCenter
PixelsPerInch = 96
TextHeight = 13
 
object ServerSocket: TServerSocket
 Active = False
 Port = 3000
 ServerType = stNonBlocking
 OnAccept = ServerSocketAccept
 OnClientRead = ServerSocketClientRead
 OnClientError = ServerSocketClientError
 Left = 8
 Top = 8
end
end

/*SocketCliente.cpp*/
#include 
#pragma hdrstop
#include "SocketCliente.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//------------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
}
//------------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{ ClientSocket->Address=Edit1->Text;
  ClientSocket->Active=true;
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{ ClientSocket->Socket->SendBuf(Edit1->Text.c_str(),Edit1->Text.Length()+1);
}
//------------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketConnect(TObject *Sender,TCustomWinSocket *Socket)
{ ShowMessage("Conectado");
  //ClientSocket->P
}
//------------------------------------------------------------------------------
void __fastcall TForm1::ClientSocketRead(TObject *Sender,TCustomWinSocket *Socket)
{ int lngtd=ClientSocket->Socket->ReceiveLength();
  char *Buffer=new char[lngtd+1];
  AnsiString AS;
  ClientSocket->Socket->ReceiveBuf(Buffer,lngtd);
  AS=AnsiString(Buffer);
  Edit2->Text=AS;
  delete Buffer;
}
/*SocketCliente.h*/
#ifndef SocketClienteH
#define SocketClienteH
//---------------------------------------------------------------------------
#include 
#include 
#include 
#include 
#include 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
        TEdit *Edit1;
        TButton *Button1;
        TClientSocket *ClientSocket;
        TButton *Button2;
        TEdit *Edit2;
        void __fastcall Button1Click(TObject *Sender);
        void __fastcall Button2Click(TObject *Sender);
        void __fastcall ClientSocketConnect(TObject *Sender,
          TCustomWinSocket *Socket);
        void __fastcall ClientSocketRead(TObject *Sender,
          TCustomWinSocket *Socket);
private: // User declarations
public:  // User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
/*SocketCliente.dfm*/
object Form1: TForm1
  Left = 100
  Top = 100
  Width = 438
  Height = 151
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 168
    Top = 16
    Width = 121
    Height = 21
    TabOrder = 0
  end
  object Button1: TButton
    Left = 40
    Top = 16
    Width = 121
    Height = 25
    Caption = 'Conectar'
    TabOrder = 1
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 296
    Top = 16
    Width = 121
    Height = 25
    Caption = 'Enviar'
    TabOrder = 2
    OnClick = Button2Click
  end
  object Edit2: TEdit
    Left = 168
    Top = 48
    Width = 121
    Height = 21
    TabOrder = 3
  end
  object ClientSocket: TClientSocket
    Active = False
    ClientType = ctNonBlocking
    Port = 3000
    OnConnect = ClientSocketConnect
    OnRead = ClientSocketRead
    Top = 8
  end
end
/*Listo*/
El SendBuf y el ReceiveBuf transfieren (por decirlo así) bloques de bytes del tamaño dado en su segundo parámetro, el buffer o bloque es una referencia cualquiera (excepto punteros a funciones y a archivos), en C el puntero genérico es void *, en Pascal no recuerdo ahorita si es Pointer o algo así, simplemente mandas la dirección del bloque q has de enviar y cuanto has de enviar, si trabajas con envio de archivos consulta por NMStrmServ y NMStrm, ellos envían flujos de tamaño considerable y la velocidad es bestial (de hecho...vuelan!!)

Última edición por vtdeleon fecha: 15-03-2007 a las 05:48:55.
Responder Con Cita