Accept()套接字调用 - 参数无效

时间:2015-04-24 22:37:45

标签: c linux sockets

我不太清楚为什么这段代码会向accept()调用返回无效参数。我没有问题就绑定和听,所以我应该能够接受()。如果我的acceptSocket()调用中有什么问题,我没有看到它。谁能指出我正确的方向?代码是:

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
void setSocket(struct sockaddr_in* s_){
    s_->sin_family=AF_INET;
    s_->sin_port=htons(9999);
    s_->sin_addr.s_addr=INADDR_ANY;
    memset(&(s_->sin_zero), '\0', 8);
    printf("s_->sin_family = %i\n", s_->sin_family);    
    printf("s_->sin_port = %i\n", s_->sin_port);
    printf("s_->sin_addr.s_addr= %i\n", s_->sin_addr.s_addr);
}

void createSocket(int *sock){
    if ((*sock = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        fprintf(stderr, "Socket creation error: %s\n", strerror(errno));
        exit(1);
    }
    printf("sock = %i\n", *sock);
    fflush(stdout);
}

void bindSocket(int sock, struct sockaddr_in *s_){
    printf("s_->sin_family = %i\n",s_->sin_family);
    printf("s_->sin_port = %i\n",s_->sin_port);
    printf("s_->sin_addr.s_addr = %i\n",s_->sin_addr.s_addr);
    if((bind(sock, (struct sockaddr*)s_, sizeof(*s_))) == -1)
        fprintf(stderr, "Socket bind error: %s\n", strerror(errno));
    printf("sizeof(s_) = %lu\n", sizeof(*s_));
}

void acceptSocket(int sfd, struct sockaddr_in *s_){
    int st = sizeof(*s_);
    printf("sizeof(*s_) = %lu\n", sizeof(*s_));
//  printf("(socklen_t*)&st_) = %i\n",(socklen_t*)&st);
    fflush(stdout);
    if(accept(sfd, (struct sockaddr*)s_, (socklen_t *)&st) == -1)
        fprintf(stderr, "Socket accept error: %s\n", strerror(errno));
}

void closeSocket(int sockfd){
    if(close(sockfd) == 1)
        fprintf(stderr, "Socket close error: %s\n", strerror(errno));
}

void dialogue(int sockfd){
    char s[1000];
    int bytesRead = read(sockfd, s, 1000);
    if(bytesRead == 1)
        fprintf(stderr, "Socket read error: %s\n", strerror(errno));
    if(write(sockfd, "I just read %i bytes!\n", *s, 100) == -1)
        fprintf(stderr, "Socket write error: %s\n", strerror(errno));
    if(write(sockfd, *s, bytesRead) == -1)
        fprintf(stderr, "Socket Write error: %s\n", strerror(errno));
}

void listenSocket(int sfd){
    if(listen(sfd, 100) == -1)
        fprintf("Socket listen error: %s\n", strerror(errno));
}

int main(int argc, char* argv[]){
    int sockfd;
    struct sockaddr_in socket_;
    createSocket(&sockfd);
    setSocket(&socket_);
    printf("sockfd = %i\n", sockfd);
    fflush(stdout);
    bindSocket(sockfd, &socket_);
    listenSocket(sockfd);
    while(1){
        acceptSocket(sockfd, &socket_);
        dialogue(sockfd);
    }

    exit(0);
}

1 个答案:

答案 0 :(得分:1)

请学习accept功能的手册。它通常在出错时返回-1,成功时它返回一个套接字的有效描述符,您可以通过该描述符进行通信。

基本上使用以下代码而不是当前函数:

int acceptSocket(int sfd, struct sockaddr_in *s_){
    int st = sizeof(*s_);
    printf("sizeof(*s_) = %lu\n", sizeof(*s_));
//  printf("(socklen_t*)&st_) = %i\n",(socklen_t*)&st);
    fflush(stdout);

    int out = accept(sfd, (struct sockaddr*)s_, (socklen_t *)&st);

    if(out == -1) {
        fprintf(stderr, "Socket accept error: %s\n", strerror(errno));
    }
    return out;
}

然后继续使用返回值作为dialogue函数的参数。我不能保证这会完全修复你的代码,但它肯定是TCP网络通信的开始。

相关问题