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)
}
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
void __fastcall TForm1::ServerSocketAccept(TObject *Sender,TCustomWinSocket *Socket)
ResumeThread(Thread);
}
void __fastcall TForm1::ServerSocketClientRead(TObject *Sender,TCustomWinSocket *Socket)
}
void __fastcall TForm1::ServerSocketClientError(TObject *Sender,TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
/*SocketServidor.h*/
#ifndef SocketServidorH
#define SocketServidorH
#include
#include
#include
#include
#include
class TForm1 : public TForm
;
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)
void __fastcall TForm1::Button2Click(TObject *Sender)
void __fastcall TForm1::ClientSocketConnect(TObject *Sender,TCustomWinSocket *Socket)
void __fastcall TForm1::ClientSocketRead(TObject *Sender,TCustomWinSocket *Socket)
/*SocketCliente.h*/
#ifndef SocketClienteH
#define SocketClienteH
#include
#include
#include
#include
#include
class TForm1 : public TForm
;
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!!)