Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect blocking behaviour #1016

Open
adamdebek opened this issue Feb 15, 2024 · 0 comments
Open

Connect blocking behaviour #1016

adamdebek opened this issue Feb 15, 2024 · 0 comments

Comments

@adamdebek
Copy link
Contributor

I think connect() should only establish connection and return, but in our case it blocks until connection is accepted.

Code to reproduction (blocking):

#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
#include <stdlib.h>

int main(void)
{
        int fd[2];
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        addr.sin_port = 10000;
        int reuse = 1;

        if ( (fd[0] = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                perror("socket");
                exit(-1);
        }

        if (setsockopt(fd[0], SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
                perror("setsockopt");
                exit(-1);
        }

        if (bind(fd[0], (struct sockaddr*)&addr, sizeof(addr)) == -1) {
                perror("bind");
                exit(-1);
        }

        if (listen(fd[0], 1) == -1) {
                perror("listen");
                exit(-1);
        }

        if ( (fd[1] = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                perror("socket2");
                exit(-1);
        }

        if (connect(fd[1], (struct sockaddr*)&addr, sizeof(addr)) == -1) {
                perror("connect");
                exit(-1);
        }

        if ( (fd[0] = accept(fd[0], NULL, NULL)) == -1) {
                perror("accept");
                exit(-1);
        }

        char buf[10];
        write(fd[0], "wiadomosc", 10);
        read(fd[1], buf, 10);
        printf("%s\n", buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant