ank3r
ank3r

Reputation: 17

First send by socket the size of the file I want to send

I want to send a file to program2, but first I want to send the size of that file. i have this in program1:

FILE *script;
int j;
int size;
char* codigo;
if (argc > 1){
  for (j = 1; j < argc; j++){
    if ((script = fopen(argv[j], "r")) == NULL){
        perror(argv[j]);
    }else{
        fseek(script, 0, SEEK_END);
        size = ftell(script); 
        fseek(script, 0, SEEK_SET); 
        codigo = malloc(sizeof(char)*(size +1));
            if(codigo) { 
            fread(codigo, sizeof(char), size, script);
                codigo[size] = '\0';
            }
            struct addrinfo hints;
                struct addrinfo *serverInfo;

                memset(&hints, 0, sizeof(hints));
                hints.ai_family = AF_UNSPEC; 
                hints.ai_socktype = SOCK_STREAM;

                getaddrinfo(ip, puerto, &hints, &serverInfo);


                int serverSocket;
                serverSocket = socket(serverInfo->ai_family, serverInfo->ai_socktype, serverInfo->ai_protocol);

                connect(serverSocket, serverInfo->ai_addr, serverInfo->ai_addrlen);
                freeaddrinfo(serverInfo);  

                printf("Conectado al servidor\n");
                send(serverSocket, codigo, strlen(codigo) + 1, 0);
                close(serverSocket);
    }
    fclose(script);
    free(codigo);
    free(ip);
    free(puerto);
  }
}

Here in program1 I'm sending the content of a script to program2. In program2 I have:

#define PACKAGESIZE 100

I define that for recv the package, but this packagesize can be variable, so I need to program1 send the size of the package i will send after that. in program2 when I have the size, then I can recive the package and print it.

Upvotes: 1

Views: 340

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595369

Use a fixed-sized integer for the file size, so both parties know how many bytes to exchange for the file size. send() the size first, then send() the data as needed. On the other side, recv() the size first, then recv() the data as needed.

For example (error handling omitted for brevity, DON'T ignore error handling!):

FILE *script;
int j, k, read;
uint32_t size, size2;
char codigo[1024];
if (argc > 1)
{
    for (j = 1; j < argc; j++)
    {
        script = fopen(argv[j], "rb");
        if (script == NULL)
        {
            perror(argv[j]);
            continue;
        }

        fseek(script, 0, SEEK_END);
        size = ftell(script); 
        fseek(script, 0, SEEK_SET);

        struct addrinfo hints;
        struct addrinfo *serverInfo, *server;

        memset(&hints, 0, sizeof(hints));
        hints.ai_family = AF_UNSPEC; 
        hints.ai_socktype = SOCK_STREAM;

        getaddrinfo(ip, puerto, &hints, &serverInfo);

        int serverSocket = -1;

        for(server = serverInfo; server != NULL; server = server->ai_next)
        {
            serverSocket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
            if (serverSocket != -1)
            {
                if (connect(serverSocket, server->ai_addr, server->ai_addrlen) == 0)
                    break;
                close(serverSocket);
                serverSocket = -1;
            }
        }

        freeaddrinfo(serverInfo);  

        if (serverSocket != -1)
        {
            printf("Conectado al servidor\n");

            size2 = htonl(size);
            send(serverSocket, (char*)&size2, sizeof(size2), 0);

            for(k = 0; k < size;)
            {
                read = fread(codigo, sizeof(char), 1024, script);
                if (read < 1) break;
                send(serverSocket, codigo, read, 0);
                k += read;
            }

            close(serverSocket);
        }

        fclose(script);
    }
}

FILE *script;
int j, read;
uint32_t size;
char codigo[1024];

int clientSocket = accept(...);
script = fopen(..., "wb");

recv(clientSocket, (char*)&size, sizeof(size), 0);
size = ntohl(size);

for(j = 0; j < size;)
{
    read = recv(clientSocket, codigo, 1024, 0);
    if (read < 1) break;
    fwrite(codigo, sizeof(char), read, script);
    j += read;
}

fclose(script);
close(clientSocket);

Upvotes: 1

Related Questions