Lukas Hejcman Lukas Hejcman - 1 year ago 76
C Question

Invalid conversion from 'int*' to 'off_t*' in C?

I am writting a program that will send one file over the network using sockets to another program using C. However, I've hit a roadblock, and since I am pretty new to C, I was unable to resolve it. When trying to compile, I get this error:

data_logger.c: In function ‘void sender()’:
data_logger.c:163:72: error: invalid conversion from ‘int*’ to ‘off_t* {aka long int*}’ [-fpermissive]
while (((sent_bytes = sendfile(peer_socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)){


I can't understand what is wrong. All help is greatly appreciated. Thanks :)

File:

void sender(){

int server_socket;
int peer_socket;
socklen_t sock_len;
ssize_t len;
struct sockaddr_in server_addr;
struct sockaddr_in peer_addr;
int fd;
int sent_bytes = 0;
char file_size[256];
struct stat file_stat;
int offset;
int remain_data;

/* Create server socket */
server_socket = socket(AF_INET, SOCK_STREAM, 0);

if (server_socket == -1){
fprintf(stderr, "Error creating socket --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

/* Zeroing server_addr struct */
memset(&server_addr, 0, sizeof(server_addr));

/* Construct server_addr struct */
server_addr.sin_family = AF_INET;
inet_pton(AF_INET, SERVER_ADDRESS, &(server_addr.sin_addr));
server_addr.sin_port = htons(PORT_NUMBER);

/* Bind */
if ((bind(server_socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) == -1){
fprintf(stderr, "Error on bind --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

/* Listening to incoming connections */
if ((listen(server_socket, 5)) == -1){
fprintf(stderr, "Error on listen --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

fd = open(FILE_TO_SEND, O_RDONLY);

if (fd == -1){
fprintf(stderr, "Error opening file --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

/* Get file stats */
if (fstat(fd, &file_stat) < 0){
fprintf(stderr, "Error fstat --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

fprintf(stdout, "File Size: \n%ld bytes\n", file_stat.st_size);

sock_len = sizeof(struct sockaddr_in);

/* Accepting incoming peers */
peer_socket = accept(server_socket, (struct sockaddr *)&peer_addr, &sock_len);
if (peer_socket == -1){
fprintf(stderr, "Error on accept --> %s", strerror(errno));
exit(EXIT_FAILURE);
}
fprintf(stdout, "Accept peer --> %s\n", inet_ntoa(peer_addr.sin_addr));

sprintf(file_size, "%ld", file_stat.st_size);

/* Sending file size */
len = send(peer_socket, file_size, sizeof(file_size), 0);

if (len < 0){
fprintf(stderr, "Error on sending greetings --> %s", strerror(errno));
exit(EXIT_FAILURE);
}

fprintf(stdout, "Server sent %d bytes for the size\n", len);

offset = 0;
remain_data = file_stat.st_size;

/* Sending file data */
while (((sent_bytes = sendfile(peer_socket, fd, &offset, BUFSIZ)) > 0) && (remain_data > 0)){
fprintf(stdout, "1. Server sent %d bytes from file's data, offset is now : %d and remaining data = %d\n", sent_bytes, offset, remain_data);
remain_data -= sent_bytes;
fprintf(stdout, "2. Server sent %d bytes from file's data, offset is now : %d and remaining data = %d\n", sent_bytes, offset, remain_data);
}

close(peer_socket);
close(server_socket);


}

Answer Source

You need to know what parameter types the sendfile() function takes. See sendfile() man page.

Notice that the 3rd parameter that you are calling offset is of type off_t *. Make sure you are passing in a pointer to type off_t and your error will be fixed.

So instead of defining offset as:

int offset;

Try

off_t offset;