Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros entornos y lenguajes > C++ Builder
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

 
 
Herramientas Buscar en Tema Desplegado
  #20  
Antiguo 04-10-2014
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.211
Poder: 22
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
Bueno, te he preparado un rápido ejemplo sobre el chat que te envié antes y sin usar componentes de VCL. Sokets "a pelo".

En el chat, si el cliente envía "Envia Fichero" (sin acento para no confundir caracteres), el servidor envía un fichero llamado Prueba0.bmp que es un bmp grande. El cliente lo recibe y lo guarda como Prueba.bmp. A continuación el server recibe la palabra y sigue la comunicación...

Te expongo las funciones que envían y reciben el fichero:

TransmitFile.h
Código PHP:
#ifndef TRANSMIT_FILE_H
#define TRANSMIT_FILE_H

void TransmitFile(SOCKET SockcharlpPathName);
void ReceiveFile(SOCKET SockcharlpPathNameint FileSize);
void ReceiveFile(SOCKET Sock);

#endif 
TransmitFile.cpp
Código PHP:
#include <windows.h>
#include <winsock2.h>
#include <Mswsock.h>

#include "TrasmitFile.h"

struct TFileData{
  
char FilePath[MAX_PATH*2];
  
int FileSize;
};

void TransmitFile(SOCKET SockcharlpPathName)
{
  if(
Sock == INVALID_SOCKET || !lpPathName) return;
  if(!*
lpPathName) return;

  
char Msg[10];
  
HFILE hFile _lopen(lpPathNameOF_READ);
  if(
hFile == -1) return;
  
int FileSize GetFileSize((HANDLE)hFile0);
  
TFileData FileData;
  
strcpy(FileData.FilePathlpPathName);
  
FileData.FileSize FileSize;
  
send(Sock, (char*)&FileDatasizeof(TFileData), 0);
  
recv(SockMsgsizeof(Msg),0);
  if(!
strncmp(Msg"Ok"2)){
    
TransmitFile(Sock, (HANDLE)hFileFileSize0,0,0,0);
  }
  
_lclose(hFile);
}

void ReceiveFile(SOCKET Sock)
{
  
TFileData FileData;
  
recv(Sock, (char*)&FileDatasizeof(FileData),0);
  
send(Sock"Ok"20);
  
ReceiveFile(Sock"Prueba.bmp"FileData.FileSize);

}

void ReceiveFile(SOCKET SockcharlpPathNameint FileSize)
{
  
int  Size 1024*10;
  
BYTE *Buffer = (BYTE*)VirtualAlloc(0SizeMEM_COMMITPAGE_READWRITE);
  
UINT  Len 0;
  
int   nBytes 0;
  
HANDLE hFile CreateFile(lpPathNameGENERIC_WRITE0NULLOPEN_ALWAYSFILE_FLAG_WRITE_THROUGH0);
  if(
hFile != INVALID_HANDLE_VALUE){
    do {
      
Len recv(SockBufferSize0);
      if(
Len == -1) break;
      
_lwrite((int)hFileBufferLen);
      
nBytes += Len;
    } while (
nBytes != FileSize);
    
CloseHandle(hFile);
  }
  
VirtualFree(Buffer0MEM_RELEASE);

Usa la API TransmitFile, que usa socket, puedes sustituirla por una función equivalente a bajo nivel.

PD/ Borro el anterior chat porque este es idéntico, añadiendo la nueva funcionalidad

Saludos.

Última edición por escafandra fecha: 24-03-2018 a las 13:09:57.
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Ellas y Ellos jcarteagaf Humor 1 06-08-2011 03:43:05
Llamada entre ventana usando paquetes. PittyDelphi OOP 3 27-01-2011 12:28:37
¿Saben algo de ellos? roman La Taberna 11 18-11-2008 18:59:07
Comunicación TCP/IP entre dos programas usando componentes INDY Roger_Delphi Internet 2 20-10-2008 14:02:49
Comunicacion entre procesos walito Varios 0 14-11-2007 21:55:53


La franja horaria es GMT +2. Ahora son las 13:42:36.


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