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

  • SEARCH
  • Home
  • 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 1044253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:47:54+00:00 2026-05-16T15:47:54+00:00

I have created small socket programs (client/server). Client will transfer all formats of files

  • 0

I have created small socket programs (client/server). Client will transfer all formats of files to server.The problem i am facing now is – I read mp3 file in binary mode and sent to the server. Server reads the content from the buffer and create a file.But when i am trying to open the file, it not getting open. But size remains same as original one. Can anyone point where i am doing wrong.
Here is my Server side code:

    recv(newSocket,fileSize,50,0); //Receiving file size

//  buffer=malloc(atoi(fileSize));
    int i=0;

    recv(newSocket,buffer,atoi(fileSize),0);
    fwrite(buffer,1,atoi(fileSize),out);

Client side:
fread(data,1,lSize,file);

    sprintf(temp,"%ld",lSize);

    send(socketDes,temp,strlen(temp),0);


    send(socketDes,data,strlen(data),0);
  • 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-16T15:47:55+00:00Added an answer on May 16, 2026 at 3:47 pm

    It might be that, when reading from a stream-based socket, you are not getting all the bytes you think you are getting. Then you are writing a file of the correct size, but only the beginning of the file will have any meaningful data.

    If you are using TCP (a stream-oriented connection), when you go to read from the socket into a buffer (you are probably using a byte/char array), you will only get all the bytes which are available at the time of the call. The call to the reading function will return immediately but the buffer into which the data is written will only be partially full.

    To fix this, you will need to check how many bytes were actually read from the socket (usually the reading call will return that number), and then repeatedly read from the socket until you have all the bytes you need.

    Update

    With regards to your code, it certainly looks like you only make one call to recv() and do not check to make sure the entire file was transferred. You will need to put this in a loop and continuously read data until you have read as much as you want to (in this case, the size of the entire file).

    You may also want to fix this problem on the sending side. Even though you have the sleep command between calls to send, send will only send a certain amount of data per call. You will need to place send in a loop and keep sending data until you have sent all the data you wish to send.

    Consider the following (untested pseudo-)code for recv:

    int numberOfReceivedBytes=0;
    while (numberOfReceivedBytes<fileSize){
        numberOfReceivedBytes+=recv(newSocket,buffer + numberOfReceivedBytes,sizeof(*buffer),0)
    }
    

    Furthermore, it looks like when you read the file size from the socket, you may accidentally get part of the file itself in addition to the size of the file. Make sure you know exactly how many bytes will indicate the actual size of the file and read only that many bytes. Also, atoi only works for the string (ASCII) representation of a number. Your server is receiving the actual number.

    To do sending:

    int numberOfSentBytes=0;
    while (numberOfSentBytes<fileSize){
        numberOfSentBytes+=send(newSocket,buffer+numberOfSentBytes,fileSize-numberOfSentBytes+1,0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • 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 Have a look at this example from Apple you just… May 16, 2026 at 7:04 pm
  • Editorial Team
    Editorial Team added an answer http://developer.android.com/guide/publishing/publishing.html#marketintent May 16, 2026 at 7:02 pm
  • Editorial Team
    Editorial Team added an answer If this doesn't work in Chrome, I would be really… May 16, 2026 at 7:00 pm

Trending Tags

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

Top Members

Related Questions

I am trying to create a small server type application and have a question
I have a small problem here and hope that someone can help me out.
I created a program that iterates over a bunch of files and invokes for
I have a program that listens on a port waiting for a small amount
I have a script that scans ports for open proxy servers. Problem is if
I have a machine running node.js (v0.1.32) with a tcp server (tcp.createServer) and a
I'm trying to create a small HTTP server in C# but I'm having some
I am trying to write a socket server that forks for every connection. I
I'Ve just created a small application with a toolbar, some toolbar buttons and an
I have developed a small C# form application which calls a web service. Everything

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.