0

I'm writing a set of client/server programs that are meant to get file information over the local network. I am attempting to allow the client to have the path defined and send it to the server, however my server read function is constantly returning a negative value, meaning error. I have even attempted to hard code in the number of bytes I am sending, in order to possibly rule out a blocking issue. Any help is appreciated.

Server #include #include #include #include #include #include #include #include

#define DEFAULT_PROTOCOL 0

char* path;

main()
{
    int serverFd, clientFd, serverLen, clientLen;
    struct sockaddr_un serverAddress;
    struct sockaddr_un clientAddress;
    struct sockaddr* serverSockAddrPtr;
    struct sockaddr* clientSockAddrPtr;

    signal(SIGCHLD, SIG_IGN);

    serverSockAddrPtr = (struct sockaddr*) &serverAddress;
    serverLen = sizeof(serverAddress);

    clientSockAddrPtr = (struct sockaddr*) &clientAddress;
    clientLen = sizeof(clientAddress);

    serverFd = socket(PF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
    serverAddress.sun_family = PF_LOCAL;
    strcpy (serverAddress.sun_path, "recipe");
    unlink ("recipe");
    bind (serverFd, serverSockAddrPtr, serverLen);
    listen (serverFd, 5);

    while(1)
    {
        clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

        if (fork() == 0)
        {

            int n = read(clientFd, path, 7);
            printf("%d\n", n);
            printf("Path is %s\n", path);
            writeRecipe(clientFd);
            close (clientFd);
            exit(0);

        } else {
            close (clientFd);
        }

    }
}

writeRecipe(fd)

int fd;

{

    static char str[80];

    DIR *dir;
    struct dirent *ent;
    struct stat fileStat;
    if ((dir = opendir (path)) != NULL) {
      while ((ent = readdir (dir)) != NULL) {

        if(stat(ent->d_name,&fileStat) < 0)    
            printf("fail");
        sprintf(str, "Inode: %d |Link Count: %d   |File Size: %d |Modified: %d\n",fileStat.st_ino, fileStat.st_nlink, fileStat.st_size, fileStat.st_mtime );
        write(fd, str, strlen(str) + 1);
      }
      closedir (dir);
    } else {
      /* could not open directory */
        printf("fail");
    }

}

Client

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <linux/limits.h>
#include <sys/socket.h>
#include <sys/un.h>

#define DEFAULT_PROTOCOL 0

main (int argc, char* argv[] )

{
    char* path;
    if (argc > 1) {
        path = argv[1];
    } else {
        path = ".";
    }
    printf("Path is %s\n", path);
    int clientFd, serverLen, result;
    struct sockaddr_un serverAddress;
    struct sockaddr* serverSockAddrPtr;

    serverSockAddrPtr = (struct sockaddr*) &serverAddress;
    serverLen = sizeof(serverAddress);

    clientFd = socket (PF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
    serverAddress.sun_family = PF_LOCAL;
    strcpy (serverAddress.sun_path, "recipe");

    do
        {
            result = connect(clientFd, serverSockAddrPtr, serverLen);
            if (result == -1) sleep (1);
        }
    while (result == -1);
    int n = write(clientFd, path, strlen(path));
    printf("%d\n", n);
    readRecipe (clientFd);
    close (clientFd);
    exit(0);
}

readRecipe(fd)

int fd;

{
    char str[200];
    while (readLine(fd, str))
        printf("%s\n", str);


}

readLine(fd, str)

int fd;
char* str;

{
    int n;

    do
        {
            n = read (fd, str, 1);
        }
    while (n > 0 && *str++ != 0);
    return (n > 0);

}

The value that is returned is negative 1, the errno is 14.

2
  • What is errno when read() returns a negative value? Commented Dec 2, 2013 at 19:47
  • 2
    did you allocate any memory for read to read into? Commented Dec 2, 2013 at 19:54

1 Answer 1

1

Allocating memory was the issue, thanks to evil otto. I changed the server code from char* to a char with allocated memory, and all problems are solved.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.