Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 729153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:45:52+00:00 2026-05-14T06:45:52+00:00

I have a client and server, client sending file to server. When i transfer

  • 0

I have a client and server, client sending file to server. When i transfer files on my computer(in local) everything is ok(try to sen file over 700mb).

When i try to sent file use Internet to my friend in the end of sending appears error on server “Input string is not in correct format”.This error appears in this expression fSize = Convert::ToUInt64(tokenes[0]); – and i don’t mind wht it’s appear. File should be transfered and wait other transferring

ps: sorry for too much code, but i want to find solution

   private: void CreateServer()
   {

    try{
     IPAddress ^ipAddres = IPAddress::Parse(ipAdress);
     listener = gcnew System::Net::Sockets::TcpListener(ipAddres, port);
     listener->Start();
     clientsocket =listener->AcceptSocket();
     bool keepalive = true;
     array<wchar_t,1> ^split = gcnew array<wchar_t>(1){ '\0' };
     array<wchar_t,1> ^split2 = gcnew array<wchar_t>(1){ '|' };

     statusBar1->Text = "Connected" ;
     //
     while (keepalive)
     {
      array<Byte>^ size1 = gcnew array<Byte>(1024); 
      clientsocket->Receive(size1);
      System::String ^notSplited = System::Text::Encoding::GetEncoding(1251)->GetString(size1);

      array<String^> ^ tokenes = notSplited->Split(split2);
      System::String ^fileName = tokenes[1]->ToString();
      statusBar1->Text = "Receiving file" ; 
      unsigned long  fSize = 0;


                             //IN THIS EXPRESSIN APPEARS ERROR 
      fSize = Convert::ToUInt64(tokenes[0]);

      if (!Directory::Exists("Received"))
       Directory::CreateDirectory("Received");

      System::String ^path = "Received\\"+ fileName;
                      while (File::Exists(path))
      {
       int dotPos = path->LastIndexOf('.');
       if (dotPos == -1)
       {
        path += "[1]";
       }
       else
       {
        path = path->Insert(dotPos, "[1]");
       }
      }

      FileStream ^fs = gcnew FileStream(path, FileMode::CreateNew, FileAccess::Write);
      BinaryWriter ^f = gcnew BinaryWriter(fs);
       //bytes received
      unsigned long processed = 0;

      pBarFilesTr->Visible = true;
      pBarFilesTr->Minimum = 0;
      pBarFilesTr->Maximum = (int)fSize;
      // Set the initial value of the ProgressBar.
      pBarFilesTr->Value = 0; 
      pBarFilesTr->Step = 1024;

      //loop for receive file
      array<Byte>^ buffer = gcnew array<Byte>(1024); 
      while (processed < fSize) 
      {
       if ((fSize - processed) < 1024)
       {
        int bytes ;
        array<Byte>^ buf = gcnew array<Byte>(1024); 
        bytes = clientsocket->Receive(buf);
        if (bytes != 0)
        {
         f->Write(buf, 0, bytes);
         processed = processed + (unsigned long)bytes;
         pBarFilesTr->PerformStep();
        }
        break;                            
       }
       else
       {
        int bytes = clientsocket->Receive(buffer);
        if (bytes != 0)
        {
         f->Write(buffer, 0, 1024);
         processed = processed + 1024;
         pBarFilesTr->PerformStep();
        }
        else break;
       }                        
      }

      statusBar1->Text = "File was received" ;
      array<Byte>^ buf = gcnew array<Byte>(1); 
      clientsocket->Send(buf,buf->Length,SocketFlags::None);
      f->Close();
      fs->Close();
      SystemSounds::Beep->Play();
     }
    }catch(System::Net::Sockets::SocketException ^es)
    {
     MessageBox::Show(es->ToString());
    }
    catch(System::Exception ^es)
    {
     MessageBox::Show(es->ToString());
    }
   }

private: void CreateClient()
{

                       clientsock = gcnew System::Net::Sockets::TcpClient(ipAdress, port);
                       ns = clientsock->GetStream();
  sr = gcnew StreamReader(ns);
  statusBar1->Text = "Connected" ;

}

private:void Send()
   {
    try{
     OpenFileDialog ^openFileDialog1 = gcnew OpenFileDialog();
     System::String ^filePath = "";
     System::String ^fileName = "";

     //file choose dialog
     if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
     {
      filePath = openFileDialog1->FileName; 
      fileName = openFileDialog1->SafeFileName;
     }
     else
     {
      MessageBox::Show("You must select a file", "Error",
       MessageBoxButtons::OK, MessageBoxIcon::Exclamation);
      return;
     }
     statusBar1->Text = "Sending file" ; 

     NetworkStream ^writerStream = clientsock->GetStream();
     System::Runtime::Serialization::Formatters::Binary::BinaryFormatter ^format = 
      gcnew System::Runtime::Serialization::Formatters::Binary::BinaryFormatter();

     array<Byte>^ buffer = gcnew array<Byte>(1024);
     FileStream ^fs = gcnew FileStream(filePath, FileMode::Open);
     BinaryReader ^br = gcnew BinaryReader(fs);
                 //file size
     unsigned long fSize = (unsigned long)fs->Length;
     //transfer file size + name
     bFSize = Encoding::GetEncoding(1251)->GetBytes(Convert::ToString(fs->Length+"|"+fileName+"|"));
     writerStream->Write(bFSize, 0, bFSize->Length);

     //status bar
     pBarFilesTr->Visible = true;
     pBarFilesTr->Minimum = 0;
     pBarFilesTr->Maximum = (int)fSize;
     pBarFilesTr->Value = 0; // Set the initial value of the ProgressBar.
     pBarFilesTr->Step = 1024;

                 //bytes transfered
     unsigned long processed = 0; 
     int bytes = 1024;
     //loop for transfer
     while (processed < fSize) 
     {
      if ((fSize - processed) < 1024)
      {
       bytes = (int)(fSize - processed);
       array<Byte>^ buf = gcnew array<Byte>(bytes);
       br->Read(buf, 0, bytes);

       writerStream->Write(buf, 0, buf->Length); 
       pBarFilesTr->PerformStep();
       processed = processed + (unsigned long)bytes;
       break;
      }
      else
      {
       br->Read(buffer, 0, 1024);
       writerStream->Write(buffer, 0, buffer->Length);
       pBarFilesTr->PerformStep();
       processed = processed + 1024;
      }               
     }
     array<Byte>^ bufsss = gcnew array<Byte>(100);
     writerStream->Read(bufsss,0,bufsss->Length);

 statusBar1->Text = "File was sent" ;
 btnSend->Enabled = true;
 fs->Close();
 br->Close();
 SystemSounds::Beep->Play();
 newThread->Abort();
 }
 catch(System::Net::Sockets::SocketException ^es)
 {
  MessageBox::Show(es->ToString());
 }
  }

UPDATE: 2Ben Voigt – ok, i can add checking if clientsocket->Receive(size1); equal zero, but why he begin receiving data again , in the ending of receiving.

UPDATE:After adding this checking problem remains. AND WIN RAR SAY TO OPENING ARCHIVE – unexpected end of file!

UPDATE:2Kevin – http://img153.imageshack.us/img153/3760/erorr.gif
I think it continue receiving some bytes from client(that remains in the stream), but why? – exists cycle while (processed < fSize)

UPDATE: 2Ben Voigt -i did this fix processed += bytes; and file transfered successfully.Thanks!
I am not very good with english and I don’t understand what you mean when said “Consider what happens if your initial read snags part of the file data…” What means snags? And what initial data do you mean?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:45:52+00:00Added an answer on May 14, 2026 at 6:45 am

    Don’t ignore the return value from clientsocket->Receive(size1).

    DOC: “Socket.Receive Method (Byte[])”

    EDIT: Consider what happens if your initial read snags part of the file data. Also consider what happens if your last read (which for some reason is still 1024 bytes instead of the remaining byte count) snags part of the header for the next request.

    EDIT: You also haven’t yet done anything useful with the return value from Receive. Your line of code:

    processed = processed + 1024;
    

    needs to be

    processed += bytes;
    

    EDIT: “snags” means “captures” or “grabs”

    You have:

    clientsocket->Receive(size1);
    

    and a little later:

    clientsocket->Receive(buf);
    

    and you assume that all the header data is in size1 and all the file data is in buf. That’s not a safe assumption on a stream socket. Datagram sockets preserve message boundaries, stream sockets like TCP do not. In fact, Nagle’s algorithm might even cause the first part of the file data to be put into the same network packet as the header, but even if it doesn’t, the receiver’s TCP stack throws away the packet boundaries and just puts things into one big receive buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here are a couple of code samples... http://www.codeproject.com/KB/audio-video/SimpleAudioCD.aspx http://www.codeproject.com/KB/cs/csharpripper.aspx May 14, 2026 at 9:39 am
  • Editorial Team
    Editorial Team added an answer How do you route your foobar? (singular or plural? resource… May 14, 2026 at 9:39 am
  • Editorial Team
    Editorial Team added an answer I think the best way would be to provide styles… May 14, 2026 at 9:39 am

Related Questions

Lets say I have a chat program that every time someone sends a message,
I have a desktop application that will 'sync' with an iPhone application. The sync
What is the best way to send a series of data (float array) from
I am using Named Pipes to transfer data from a client (C++) to a
I have a web application written using CherryPy, which is run locally on 127.0.0.1:4321

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.