Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to C programming and working on tcp/ip protocol. When we try to communicate from android to C by sending a text file, i am able to receive the text file in partial not the complete file. There is a mismatch in bytes with the source file and the received file.How to eradicate this.

[edit]
// I have given the code for further reference

C++
#include<stdio.h>
#include<unistd.h>
#include<string.h>    
#include<stdlib.h>
#include<sys ioctl.h="">
#include<sys types.h="">
#include<arpa inet.h="">
#include<sys socket.h="">

int a = 1;
char str1[3], str2[] = "ok", str3[] = "no";
char *pBuf;

int receive_text(long long int new_socket)
{
    long int buffersize = 0, recv_size = 0, size = 0, read_size, write_size;
    char verify = '1';
    int errno;
    FILE *text;

    //Find the size of the text
    recv(new_socket, (char *)& size, sizeof(int), 0);
    //Send our verification signal
    send(new_socket, & verify, sizeof(char), 0);
    //Make sure that the size is bigger than 0

    /*if (size <= 0)
    {
    printf("Error has occurred. Size less than or equal to 0 \n");
    return -1;
    }*/

    text = fopen("/home/sosdt009/Desktop/received.txt", "w");
    if (text == NULL)
    {
        puts("Error has occurred. Text file could not be opened \n");
        return -1;
    }

    //Loop while we have not received the entire file yet
    while (recv_size < size)
    {
        ioctl(new_socket, FIONREAD, & buffersize);
        //We check to see if there is data to be read from the socket 
        if (buffersize > 0)
        {
            pBuf = malloc(buffersize);
            if (!pBuf)
            {
                fprintf(stderr, "Memory Error. Cannot allocate!\n");
                exit(-1);
            }
            read_size = recv(new_socket, pBuf, buffersize, 0);
            if (read_size  < 0)
            {
                printf("%s", strerror(errno));
            }

            //Write the currently read data into our text file
            write_size = fwrite(pBuf, 1, buffersize, text);
            free(pBuf);
            //Increment the total number of bytes read
            recv_size += write_size;
        }
    }

    fclose(text);
    //printf("text successfully Received! \n");
    return 1;
}

int main(int argc, char *argv[])
{
    int socket_desc, new_socket, c, read_size, buffer = 0;
    struct sockaddr_in server, client;
    char *readin;

    //Create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1)
    {
        printf("Could not create socket:");
    }

    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons(6777);

    //Bind
    if (bind(socket_desc, (struct sockaddr *)& server, sizeof(server)) < 0)
    {
        puts("bind failed");
        return 1;
    }
    puts("Bind completed");
    //Listen
    listen(socket_desc, 3);

    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);
    if ((new_socket = accept(socket_desc, (struct sockaddr *)& client, (socklen_t *)& c)))
    {
        puts("Connection accepted");
    }
    fflush(stdout);
    close(socket_desc);
    if (new_socket< 0)
    {
        perror("Accept Failed");
        return 1;
    }
    while (1)
    {
        receive_text(new_socket);
    }
    close(socket_desc);
    return 0;
}

[/edit]
Posted
Updated 19-May-15 21:59pm
v2
Comments
Richard MacCutchan 18-May-15 6:02am    
How to eradicate this.
Probably by correcting the error(s) in your code. But without more information we cannot beging to guess where you are going wrong.
venkat28vk 18-May-15 8:57am    
There are no errors in c code, problem is i have checked by transmitting a single byte by byte from android to C, but still there are some loss at the last of the file.
Richard MacCutchan 18-May-15 9:02am    
Please see my previous comment. And do not be fooled, just because there are no compiler or linker errors in your code does not mean there are no logic errors.
venkat28vk 20-May-15 0:49am    
Moved to question [rjm]
Richard MacCutchan 20-May-15 4:09am    
There is nothing obvious in your code that I can see, although I would question your call to receive_text being in an infinite loop. I can only suggest you add some debug code to your program, or run it in the debugger, to see where the missing data is going. Are you sure that the sending program is actually sending all the data?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900