在非套接字上发送()套接字操作

时间:2014-07-19 00:51:11

标签: c sockets client-server

在尝试实现服务器和客户端时,我注意到客户端正在写入stdout 通过网络。我发现connect()返回的文件描述符总是为零,这就解释了为什么它写入stdout。但我无法弄清楚为什么connect()总是返回零而不是有效的套接字。我发现同样问题的网上所有文章都是由于在connect()调用周围包装if()的优先级问题。但我还没有这样做,任何帮助都会受到赞赏。

服务器代码

 int setUpServer(struct fuzzerObj *ptr, int *firstClient)
 {
     /* Declarations */
    int hostSocket, yes = 1, rtrn, clientfd;

    union
    {
        struct sockaddr_in in;

    }address;

    /* Create Socket */
    hostSocket = socket(AF_INET, SOCK_STREAM, 0);
    if(hostSocket < 0)
    {
        errorHandler("Could not create socket\n", FUNCTION_ID_SET_UP_SERVER);
        return -1;
    }

    /* Reuse Address */
    rtrn = setsockopt(hostSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
    if(rtrn < 0)
    {
        errorHandler("Couldn't Reuse Address\n", FUNCTION_ID_SET_UP_SERVER);
        return -1;
    }

    /* Set Up Struct */
    address.in.sin_len = sizeof(address.in);
    address.in.sin_family = AF_INET;
    address.in.sin_port = htons(BBPORT_NUMBER);
    address.in.sin_addr.s_addr = htonl(INADDR_ANY);
    memset(address.in.sin_zero, 0, sizeof(address.in.sin_zero));

    /* Bind Address to Socket */
    rtrn = bind(hostSocket, (struct sockaddr*) &address, address.in.sin_len);
    if(rtrn < 0)
    {
        errorHandler("Can't Bind Address to Socket\n", FUNCTION_ID_SET_UP_SERVER);
        return -1;
    }

    /* listen */
    rtrn = listen(hostSocket, ptr->numberOfClients);
    if(rtrn < 0)
    {
        errorHandler("Can't Listen\n", FUNCTION_ID_SET_UP_SERVER);
        return -1;
    }

    while(1)
    {
        rtrn = acceptClient(hostSocket, &clientfd);
        if(rtrn < 0)
        {
            printf("Can't Accept Client\n");
            return -1;
        }
        break;
    }
    *firstClient = clientfd;
    return 0;
 }

客户端代码

 #include <CoreFoundation/CoreFoundation.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <arpa/inet.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <stdbool.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <errno.h>

 #define BLACKBOX_PORT 9696

 int main(int argc, char *argv[])
 {
     /* Check To See If an argument was passed */
     if(argc < 2)
     {
         printf("No enough Arguments\n");
         return -1;
      }

     /* Declaration's */
    const char *ip = argv[1];
    int sockfd, fd, rtrn;
    char *outBuf;
    struct sockaddr_in servaddr;

    /* Get Socket to Connect to Fuzz Server */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        perror("Can't Create Socket");
        return -1;
    }

    /* Fill Out Struct */
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(BLACKBOX_PORT);
    inet_pton(AF_INET, ip, &servaddr.sin_addr);

    /* Attempt Connection */
    fd = connect(sockfd,(struct sockaddr *)&servaddr, sizeof(servaddr));
    if(fd < 0)
    {
        perror("Can not connect to BlackBox Fuzz server");
        return -1;
     }

     /* Allocate Space in Memory for Outgoing Connection */
     rtrn = asprintf(&outBuf, "Mac OSX 10.9\n");
     if(rtrn < 0)
     {
         perror("Copy Error");
         return -1;
     }

     /* Send Data to Fuzzer via Socket */
     rtrn = send(fd, outBuf, strlen(outBuf), 0);
     if(rtrn < 0)
     {
          perror("Can't write Data to BlackBox Server");
          return -1;
     }
     free(outBuf);

     return 0;
  }

1 个答案:

答案 0 :(得分:3)

成功拨打connect()后,sockfd已连接。 0返回值表示呼叫成功。如果该值不是0,则表示存在错误,并且连接尝试失败或尚未完成(如果连接是非阻塞的)。

确定connect()成功后,请致电send()上的sockfd,而不是connect()来电的返回值。

     rtrn = send(sockfd, outBuf, strlen(outBuf), 0);