Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Como mandar archivos por SOCKET en Delphi 7 (https://www.clubdelphi.com/foros/showthread.php?t=19588)

juliopag1 18-03-2005 19:48:57

Como mandar archivos por SOCKET en Delphi 7
 
Como mandar archivos por SOCKET en Delphi 7 ?,

no se utilizar el commando de SendBuf() y RecibeBuf(), alguien me puede explicar porque de verdad no entiendo!!!!

OSKR 19-03-2005 17:18:45

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!!)

Barzaugc 17-06-2005 19:15:14

De cuasualidad no tendrá alguien este codigo pero en Delphi, seria muiy bueno para los que no sabemos absolutamente nada de C.

Saludos.

jachguate 17-06-2005 19:24:35

No hace falta ir directamente al API para enviar un archivo por sockets, aunque si queres hacerlo de esta forma, pues francamente tampoco hay que te lo impida. :D

Para enviar ficheros, podes valerte de las INDY, en particular de TidTCPServer e TidTCPClient.

Hasta lugeo.

;)

juliopag1 18-06-2005 14:13:27

Tus dudas estan en este hilo!
 
Tus dudas estan en este hilo!

http://www.clubdelphi.com/foros/showthread.php?t=21441


La franja horaria es GMT +2. Ahora son las 07:56:32.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi