简体   繁体   English

listen()不会阻止进程

[英]listen() does not block process

EDIT: nevermind! 编辑:没关系! I spent hours trying to get the error... I was using connect() instead of accept()... Shame on me. 我花了数小时试图获取错误...我使用的是connect()而不是accept()...

EDIT 2: I'd like to specify that this code might have no sense, this is because I'm currently studying the subject and what you find in this page is one of my very first tests. 编辑2:我想指定此代码可能没有意义,这是因为我目前正在研究该主题,因此您在本页中找到的内容是我的第一个测试之一。 Thank you! 谢谢!

This is the code of a very simple server that I'm writing... I can't understand why the listen() function doesn't block the process which goes straight ahead to the connect() which fails giving me error 22 - Invalid argument . 这是我正在编写的一个非常简单的服务器的代码...我不明白为什么listen()函数不会阻止直接进入connect()的进程,而失败却给我错误22 - Invalid argument What's the problem? 有什么问题? Thank you. 谢谢。

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>

#define BUFMAXSIZE 1024
#define MAXLISTEN 10
#define SOCKERROR -1

int main(int argc, char *argv[]) {

    int localPort;

    int sockfd, sockbind, socklist, sockaccept;
    struct sockaddr_in local, remote;
    int remoteSize;

    char readMsg[BUFMAXSIZE];
    int totalReadLen, singleLoopRead;

    char answer[BUFMAXSIZE];
    int totalSentLen, singleLoopSent, answerLen;

    // Checking parameters
    if(argc < 2) {
        printf("Usage: server LOCAL_PORT_NUMBER\n");
        exit(1);
    }
    else {
        localPort = atoi(argv[1]);
    }

    // Creating socket
    printf("Creating socket...\n");
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd == SOCKERROR) {
        printf("Error: could not open socket.\n");
        exit(1);
    }
    printf("Socket created.\n");

    // Setting local information
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_addr.s_addr = htonl(INADDR_ANY);
    local.sin_port = htons(localPort);

    // Binding
    printf("Setting local information...\n");
    sockbind = bind(sockfd, (struct sockaddr*) &local, sizeof(local));
    if(sockbind == SOCKERROR) {
        printf("Error: could not bind socket.\n");
        exit(1);
    }
    printf("Socket binded.\n");

    // Listening
    printf("Listening on %d:%d...\n", ntohl(local.sin_addr.s_addr), ntohs(local.sin_port));
    socklist = listen(sockfd, MAXLISTEN);
    if(socklist == SOCKERROR) {
        printf("Error: could not listen.\n");
        exit(1);
    }

    // Accepting connection
    printf("Accepting connection...\n");
    memset(&remote, 0, sizeof(remote));
    remoteSize = sizeof(remote);
    sockaccept = connect(sockfd, (struct sockaddr*) &remote, &remoteSize);
    if(sockaccept == SOCKERROR) {
        printf("Error: could not accept connection.\n");
        exit(1);
    }
    printf("Connection accepted.\n");

    // Reading data
    printf("Reading data...\n");
    totalReadLen = 0;
    singleLoopRead = 0;
    while(BUFMAXSIZE > totalReadLen && (singleLoopRead = read(sockaccept, &(readMsg[totalReadLen]), BUFMAXSIZE - totalReadLen)) > 0) {
        totalReadLen += singleLoopRead;
        if(singleLoopRead < 0) {
            printf("Error: could not read data.\n");
            exit(1);
        }
        else {
            printf("Data read: %d.\n", singleLoopRead);
        }
    }
    printf("Read message: %s.", readMsg);

    // Sending answer
    printf("Sending answer...");
    totalSentLen = 0;
    singleLoopSent = 0;
    strncpy(answer, "Data have been received.", BUFMAXSIZE - 1);
    answerLen = strlen(answer);
    fflush(stdout);
    while((singleLoopSent = write(sockaccept, &(answer[totalSentLen]), answerLen - totalSentLen)) > 0) {
        totalSentLen += singleLoopSent;
        if(singleLoopSent < 0) {
            printf("Error: could not send answer.\n");
            fflush(stdout);
            exit(1);
        }
        else {
            printf("Total data sent: %d bytes.\n", totalSentLen);
        }
    }
    fflush(stdout);
    printf("Answer correctly sent.\n");

    // Closing sockets
    printf("Closing connection...\n");
    close(sockaccept);
    close(sockfd);
    printf("Connection closed.\n");

    return 0;
}

I can't understand why the listen() function doesn't block the process 我不明白为什么listen()函数不会阻止该过程

Because it isn't a blocking function. 因为它不是阻止功能。 It puts the port into the LISTEN state. 它将端口置于LISTEN状态。 It is accept() which is the blocking function, which you should be calling next. 它是accept()这是阻塞函数,您接下来应该调用它。

which goes straight ahead to the connect() which fails giving me error 22 - Invalid argument. 它直接进入connect(),失败了,并给我错误22-无效的参数。

Because the socket is in LISTEN state, because you called listen() . 由于套接字处于LISTEN状态,因为调用了listen() You can't connect a listening socket. 您无法连接监听插座。 You should be calling accept() . 您应该调用accept()

What's the problem? 有什么问题?

The problem is that your code doesn't make sense. 问题是您的代码没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM