im trying to send image from one system to another(client server model),it works fine when i use loopback ip address(same machine as both client and server) but when trying to send to another system,the gimp doesnt recognize the image and displays an error after receiving the image...

here is the code
server.c

#include<netdb.h>
#include<netinet/in.h>
#include<string.h>
#include<stdio.h>


#define PORT 0x3456

int parseARGS(char **args, char *line)
{
    int tmp=0;
    args[tmp] = strtok( line, ":" );
    while ( (args[++tmp] = strtok(NULL, ":" ) ) != NULL );
    return tmp - 1;
}

int main()
{

    char *header[4096];
    int listenSOCKET, connectSOCKET;
    socklen_t clientADDRESSLENGTH;
    struct sockaddr_in clientADDRESS, serverADDRESS;
    char recvBUFF[4096];
    char *filename, *filesize;
    FILE * recvFILE;
    int received = 0;
    char tempstr[4096];

    int count1=1,count2=1, percent;

    listenSOCKET = socket(AF_INET, SOCK_STREAM, 0);
    if (listenSOCKET < 0) {
        printf("Cannot create socket\n");
        close(listenSOCKET);
        return 1;
    }

    serverADDRESS.sin_family = AF_INET;
    serverADDRESS.sin_port = htons(PORT);
    serverADDRESS.sin_addr.s_addr =INADDR_ANY;

    if (bind(listenSOCKET, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0) {
        printf("Cannot bind socket\n");
        close(listenSOCKET);
        return 1;
    }

    if(listen(listenSOCKET, 5) < 0)
    {
    printf("Unable to listen");
    close(listenSOCKET);
    return 1;
    }

    fflush(stdout);

    clientADDRESSLENGTH = sizeof(struct sockaddr_in);

    connectSOCKET = accept(listenSOCKET, (struct sockaddr *) &clientADDRESS, &clientADDRESSLENGTH);
    if (connectSOCKET < 0) {
        printf("Cannot accept connection\n");
        close(listenSOCKET);
        return 1;
    }


    while(1)
    {   
        if( recv(connectSOCKET, recvBUFF, sizeof(recvBUFF), 0) )
        {
            if(!strncmp(recvBUFF,"FBEGIN",6))
            {
                recvBUFF[strlen(recvBUFF) - 2] = 0;
                parseARGS(header, recvBUFF);
                filename = header[1];
                filesize = header[2];
            }
            recvBUFF[0] = 0;
            recvFILE = fopen ( filename,"w" );
            percent = atoi( filesize ) / 100;
            while(1)
            {
                if( recv(connectSOCKET, recvBUFF, 1, 0) != 0 )
                {
                    fwrite (recvBUFF , sizeof(recvBUFF[0]) , 1 , recvFILE );

                    if( count1 == count2 )
                    {
                        printf("33[0;0H"); //move cursor to 0, 0
                        printf( "\33[2J"); //clear line
                        printf("Filename: %s\n", filename);
                        printf("Filesize: %d Kb\n", atoi(filesize) / 1024);
                        printf("Percent : %d%% ( %d Kb)\n",count1 / percent ,count1 / 1024);
                        count1+=percent;
                    }
                    count2++;
                    received++;
                    recvBUFF[0] = 0;
                } else
                {
                close(listenSOCKET);
                return 0;
                }
            }
        close(listenSOCKET);
        }
        else
        {
            printf("Client dropped connection\n");
        }

    return 0;
    }
    }
 client.c

#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>



int fileSEND(char *server, int PORT, char *lfile, char *rfile)


    int socketDESC;
    struct sockaddr_in serverADDRESS;
    struct hostent *hostINFO;
    FILE * file_to_send;
    int ch;
    char toSEND[1];
    char remoteFILE[65536];
    int count1=1,count2=1, percent;


    hostINFO = gethostbyname("localhost");
    if (hostINFO == NULL)
    {
        printf("Problem interpreting host\n");
        return 1;
    }

    socketDESC = socket(AF_INET, SOCK_STREAM, 0);
    if (socketDESC < 0)
    {
        printf("Cannot create socket\n");
        return 1;
    }


    serverADDRESS.sin_family = AF_INET;
    serverADDRESS.sin_port = htons(PORT);
    serverADDRESS.sin_addr = *((struct in_addr * )hostINFO->h_addr);


    bzero(&(serverADDRESS.sin_zero),8);


    if (connect(socketDESC, (struct sockaddr *) &serverADDRESS, sizeof(serverADDRESS)) < 0)
    {
        printf("Cannot connect\n");
        return 1;
    }


    file_to_send = fopen (lfile,"r");
    if(!file_to_send)
    {
        printf("Error opening file\n");
        close(socketDESC);
        return 0;
    }
    else
    {
        long fileSIZE;
        fseek (file_to_send, 0, SEEK_END);
        fileSIZE =ftell (file_to_send);
        rewind(file_to_send);

        sprintf(remoteFILE,"FBEGIN:%s:%d\r\n", rfile, fileSIZE);
        send(socketDESC, remoteFILE, sizeof(remoteFILE), 0);

        percent = fileSIZE / 100;
        while((ch=getc(file_to_send))!=EOF)
        {
            toSEND[0] = ch;
            send(socketDESC, toSEND, 1, 0);

            if( count1 == count2 )
            {
                printf("33[0;0H");
                printf( "\33[2J");
                printf("Filename: %s\n", lfile);
                printf("Filesize: %d Kb\n", fileSIZE / 1024);
                printf("Percent : %d%% ( %d Kb)\n",count1 / percent ,count1 / 1024);
                count1+=percent;
            }
       count2++;

        }
    }
   fclose(file_to_send);
   close(socketDESC);

   return 0;


int main(int argc, char* argv[])
{

        if(argc!= 3)
        {
            printf("Invalid number of arguments\n");
            printf("Usage:./main program input_image output_image\n");
            exit(1);
        }
        else
        {
            fileSEND("localhost",0x3456, argv[1], argv[2]);
        }
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.