1

Trying to run C executable gives me

Error opening file --> No such file or directory

when the file is clearly there. Does anyone know the cause of this issue? I'm simply compiling a C program using gcc and trying to run the executable. See the following This is on a

root@EmRtkGps3[~/socket]# ls
serverFile.c  sf
root@EmRtkGps3[~/socket]# ./sf
Error opening file --> No such file or directoryroot@EmRtkGps3[~/socket]# 

The version of linux I'm running:

Linux EmRtkGps3 3.10.98-poky-edison+ #1 SMP PREEMPT Wed Jul 12 18:30:01 MSK 2017 i686 GNU/Linux

serverFile.c

/* Server code */
/* TODO : Modify to meet your need 
from 
https://stackoverflow.com/questions/11952898/c-send-and-receive-file
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/sendfile.h>

#define PORT_NUMBER     5000
#define SERVER_ADDRESS  "192.168.0.103"
#define FILE_TO_SEND    "/home/root/logs/*.UBX"

int main(int argc, char **argv)
{
        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;
        off_t 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%d 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, "%d", 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);

        return 0;
}
Lightsout
  • 3,454
  • 2
  • 36
  • 65
  • 2
    Wildcards, like `*`, are not part of C or handled by the [`open`](http://man7.org/linux/man-pages/man2/open.2.html) function. The [`open`](http://man7.org/linux/man-pages/man2/open.2.html) function only opens a *single* specified file. When you use them in the shell they are handled in the *shell* not by your program. You need to use [special](http://man7.org/linux/man-pages/man3/fnmatch.3.html) [functions](http://man7.org/linux/man-pages/man3/glob.3.html) to resolve wildcards. – Some programmer dude Apr 06 '18 at 02:48
  • Replace `*.UBX` with an actual file name. If you need to send multiple files, enumerate the files with that extension and send them one by one. – mnistic Apr 06 '18 at 02:53
  • I don't know the filename except it ends in .UBX – Lightsout Apr 06 '18 at 02:59
  • Do you have a file called /home/root/logs/*.UBX? I bet you don't. – user253751 Apr 06 '18 at 03:57
  • Do you want *any* file ending with `.UBX`? All of the files ending in `.UBX`? Any *specific* file ending in `.UBX`? And please follow the links to the `fnmatch` and `glob` manual pages in my previous comment. They will help you how to iterate over multiple files using a wildcard. – Some programmer dude Apr 06 '18 at 04:00
  • There should be exactly 1 file ending in .UBX and I want that 1 only. – Lightsout Apr 06 '18 at 05:00
  • Which one should I used fnmatch or glob and can you give me example? – Lightsout Apr 06 '18 at 05:12
  • per the posted code, the file to be sent is owned by 'root'. Do you, as a user, have permission to open/read that file? – user3629249 Apr 07 '18 at 21:42
  • suggest modifying the source to expect the file name to be supplied via a command line parameter that is sent to the server. Suggest using 'telnet' to determine the actual file name, then send that actual + to the server so the server knows exactly which file to send. This may require obtaining a 'root' login to the server, which the 'web master' will be very reluctant to provide – user3629249 Apr 07 '18 at 21:50
  • You might want to ask the web master to run a 'cron' that copies the desired file to a public area with a specific name, etc – user3629249 Apr 07 '18 at 21:55

0 Answers0