views:

444

answers:

2

So good night. I'm new here, and I need a big help. I'm doing a program, and I need to send a file from one pc to another one. I'm trying to send it using buffer, but I'm having some problems. First, it sends the buffer - I thing - , but it creates the file and its size is 0 kb, I think I'm not writing the buffer in the file, bit, I really don't know. I'm using C++ Builder 2010, and it's hard find some tutorial about it, it has new components and so on. And, my buffer is from a *bmp file. I already saw here that another one asked almost the same thing, but I really need it more explained. The code:


The Client:

void __fastcall TForm1::TcpServer1Accept(TObject *Sender,
    TCustomIpClient *ClientSocket)
{
  char *buffer;
  TcpServer1->ReceiveBuf(buffer, sizeof(buffer), 0);

  CreateFile(buffer); //function that creates a file from the buffer

  //Drawing my bmp on the form
  Slide->LoadFromFile("ScreenShotBorland.bmp");
  Image1->Canvas->Draw(0, 0, Slide);

  free(buffer);
}

void CreateFile(char *buffer)
{

  FILE *fp = NULL;

  fp = fopen("teste.bmp", "wb");
  if (fp == NULL) {
    MessageBox(NULL, "ERRO!", "Error", MB_OK | MB_ICONERROR);
  }

  fwrite(buffer, 1, sizeof(buffer), fp);
  free(buffer);
  fclose(fp);
}

The Server:

void __fastcall TForm1::TcpClient1Connect(TObject *Sender)
{
  //Open a file to load the buffer
  long lSize;
  char *buffer;
  size_t result;
  FILE *pf2 = fopen("teste.bmp", "rb");

  if (pf2 == NULL) {
    MessageBox(NULL, "ERRO!", "Error", MB_OK | MB_ICONERROR);
  }

  fseek(pf2, 0, SEEK_END);
  lSize = ftell(pf2);
  rewind(pf2);

  //allocate the buffer in memorie
  buffer = (char*) malloc(sizeof(char) * lSize);
  if (buffer == NULL)
    MessageBox(NULL, "ERRO!", "Error", MB_OK | MB_ICONERROR);

  result = fread(buffer, 1, lSize, pf2);

  if (result != lSize)
    MessageBox(NULL, "ERRO!!!", "Error", MB_OK | MB_ICONERROR);

  //Transferring the file
  if (TcpClient1->Active)
    TcpClient1->SendBuf(buffer, sizeof(buffer), 0);

  free(buffer);
  fclose(pf2);
}
A: 

So, I fixed this code, but, I still don't get receive the buffer... I get send, but I don't get receive... Does someone have any idea??

Little Hot Angel
A: 
  • Your client uses a TTcpServer, and your server uses a TTcpClient. This is confusing and possibly incorrect (either incorrect terminology or incorrect code).
  • For the BufSize parameter to SendBuf and ReceiveBuf, your code sends sizeof(buffer). buffer is a char * (pointer to char), and the size of a pointer is 32 bits on x86. sizeof tells the size of the variable itself, not what the variable points to. C and C++ don't even know the size of what the variable points to, because buffer could be uninitialized, or NULL, or it could just be an alias to another buffer elsewhere, and so on.
  • The "client" code (the one receiving the buffer) has no way of doing the size of data to expect. To fix this, you should probably send the size before sending the contents. Note that your CreateFile function will then need to take that size as a parameter so it knows how big buffer is.
  • The "client" code (the one receiving the buffer) never allocates space for the buffer.
  • Suggestion: CreateFile is a Windows API function. Your code would be easier to read if you avoid giving application-specific functions the same names as generic Windows API functions. CreateScreenshotFile or SaveScreenshot would probably be better.
Josh Kelley