2012-05-31 98 views
0

我米linux下显影程序全光照IPC套接字通信插座之间(内核版本是2.6.25.20) 这里client.c的源代码和server.cIPC套接字错误

client.c后

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

#define NSTRS  3   /* no. of strings */ 
#define ADDRESS  "mysocket" /* addr to connect */ 

/* 
* Strings we send to the server. 
*/ 
char *strs[NSTRS] = { 
    "This is the first string from the client.\n", 
    "This is the second string from the client.\n", 
    "This is the third string from the client.\n" 
}; 

main() 
{ 
    char c; 
    FILE *fp; 
    register int i, s, len; 
    struct sockaddr_un saun; 

    /* 
    * Get a socket to work with. This socket will 
    * be in the UNIX domain, and will be a 
    * stream socket. 
    */ 
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 
     perror("client: socket"); 
     exit(1); 
    } 

    /* 
    * Create the address we will be connecting to. 
    */ 
    saun.sun_family = AF_UNIX; 
    strcpy(saun.sun_path, ADDRESS); 

    /* 
    * Try to connect to the address. For this to 
    * succeed, the server must already have bound 
    * this address, and must have issued a listen() 
    * request. 
    * 
    * The third argument indicates the "length" of 
    * the structure, not just the length of the 
    * socket name. 
    */ 
    len = sizeof(saun.sun_family) + strlen(saun.sun_path); 

    if (connect(s, &saun, len) < 0) { 
     perror("client: connect"); 
     exit(1); 
    } 

    /* 
    * We'll use stdio for reading 
    * the socket. 
    */ 
    fp = fdopen(s, "r"); 

    /* 
    * First we read some strings from the server 
    * and print them out. 
    */ 
    for (i = 0; i < NSTRS; i++) { 
     while ((c = fgetc(fp)) != EOF) { 
      putchar(c); 

      if (c == '\n') 
       break; 
     } 
    } 

    /* 
    * Now we send some strings to the server. 
    */ 
    for (i = 0; i < NSTRS; i++) 
     send(s, strs[i], strlen(strs[i]), 0); 

    /* 
    * We can simply use close() to terminate the 
    * connection, since we're done with both sides. 
    */ 
    close(s); 

    exit(0); 
} 

server.c

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

#define NSTRS  3   /* no. of strings */ 
#define ADDRESS  "mysocket" /* addr to connect */ 

/* 
* Strings we send to the client. 
*/ 
char *strs[NSTRS] = { 
    "This is the first string from the server.\n", 
    "This is the second string from the server.\n", 
    "This is the third string from the server.\n" 
}; 

main() 
{ 
    char c; 
    FILE *fp; 
    int fromlen; 
    register int i, s, ns, len; 
    struct sockaddr_un saun, fsaun; 

    /* 
    * Get a socket to work with. This socket will 
    * be in the UNIX domain, and will be a 
    * stream socket. 
    */ 
    if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { 
     perror("server: socket"); 
     exit(1); 
    } 

    /* 
    * Create the address we will be binding to. 
    */ 
    saun.sun_family = AF_UNIX; 
    strcpy(saun.sun_path, ADDRESS); 

    /* 
    * Try to bind the address to the socket. We 
    * unlink the name first so that the bind won't 
    * fail. 
    * 
    * The third argument indicates the "length" of 
    * the structure, not just the length of the 
    * socket name. 
    */ 
    unlink(ADDRESS); 
    len = sizeof(saun.sun_family) + strlen(saun.sun_path); 

    if (bind(s, &saun, len) < 0) { 
     perror("server: bind"); 
     exit(1); 
    } 

    /* 
    * Listen on the socket. 
    */ 
    if (listen(s, 5) < 0) { 
     perror("server: listen"); 
     exit(1); 
    } 

    /* 
    * Accept connections. When we accept one, ns 
    * will be connected to the client. fsaun will 
    * contain the address of the client. 
    */ 
    if ((ns = accept(s, &fsaun, &fromlen)) < 0) { 
     perror("server: accept"); 
     exit(1); 
    } 

    /* 
    * We'll use stdio for reading the socket. 
    */ 
    fp = fdopen(ns, "r"); 

    /* 
    * First we send some strings to the client. 
    */ 
    for (i = 0; i < NSTRS; i++) 
     send(ns, strs[i], strlen(strs[i]), 0); 

    /* 
    * Then we read some strings from the client and 
    * print them out. 
    */ 
    for (i = 0; i < NSTRS; i++) { 
     while ((c = fgetc(fp)) != EOF) { 
      putchar(c); 

      if (c == '\n') 
       break; 
     } 
    } 

    /* 
    * We can simply use close() to terminate the 
    * connection, since we're done with both sides. 
    */ 
    close(s); 

    exit(0); 
} 

BUIL后鼎和运行客户端和服务器我得到一个错误,从服务器在接受阶段,其打印以下错误server: accept: Invalid argument

在其他Linux系统没有问题相同的应用程序运行(内核版本2.6.30)

如何修复源代码以使其在第一个平台上运行?

回答

3
if ((ns = accept(s, &fsaun, &fromlen)) < 0) { 

你必须把它传递给函数之前初始化fromlen

的addrlen中参数是一个值结果参数:主叫方必须 初始化它含有结构 通过addr指向的的大小(以字节为单位);在返回时它将包含对等地址的实际大小 对等地址。

喜欢的东西:

fromlen = sizeof(fsaun); 
/* And then accept. */ 

和正确类型fromlensocklen_t,不int