Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Go Back   Foros Club Delphi > Principal > Internet
Register FAQ Members List Calendar Guía de estilo Today's Posts

Colaboración Paypal con ClubDelphi

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 18/03/2005
juliopag1 juliopag1 is offline
Miembro
 
Join Date: Apr 2004
Posts: 42
Poder: 0
juliopag1 Va por buen camino
Unhappy 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!!!!
Reply With Quote
  #2  
Old 19/03/2005
OSKR's Avatar
OSKR OSKR is offline
Miembro
 
Join Date: Nov 2004
Location: San Cristóbal/Táchira/Venezuela
Posts: 389
Poder: 22
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!!)

Last edited by vtdeleon : 15/03/2007 at 05:48.
Reply With Quote
  #3  
Old 17/06/2005
Barzaugc Barzaugc is offline
Miembro
 
Join Date: Jun 2005
Posts: 50
Poder: 22
Barzaugc Va por buen camino
De cuasualidad no tendrá alguien este codigo pero en Delphi, seria muiy bueno para los que no sabemos absolutamente nada de C.

Saludos.
Reply With Quote
  #4  
Old 17/06/2005
jachguate's Avatar
jachguate jachguate is offline
Miembro
 
Join Date: May 2003
Location: Guatemala
Posts: 6,254
Poder: 30
jachguate Va por buen camino
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.

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

Hasta lugeo.

__________________
Juan Antonio Castillo Hernández (jachguate)
Guía de Estilo | Etiqueta CODE | Búsca antes de preguntar | blog de jachguate
Reply With Quote
  #5  
Old 18/06/2005
juliopag1 juliopag1 is offline
Miembro
 
Join Date: Apr 2004
Posts: 42
Poder: 0
juliopag1 Va por buen camino
Tus dudas estan en este hilo!

Tus dudas estan en este hilo!

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



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +2. The time now is 01:52.


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