无法通过C中的套接字通过写入和读取函数发送char数组

时间:2013-12-02 19:44:03

标签: c sockets blocking

我正在编写一组客户端/服务器程序,旨在通过本地网络获取文件信息。我试图允许客户端定义路径并将其发送到服务器,但是我的服务器读取功能不断返回负值,这意味着错误。我甚至试图硬编码我发送的字节数,以便可能排除阻塞问题。任何帮助表示赞赏。

服务器     #包括     #包括     #包括     #包括     #包括     #包括     #包括     #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");
    }

}

客户端

#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);

}

返回的值为负1,errno为14。

1 个答案:

答案 0 :(得分:1)

由于邪恶的奥托,分配记忆是个问题。我将服务器代码从char *更改为具有已分配内存的char,并且所有问题都已解决。