P
P
Pinkman2021-11-25 07:42:08
C++ / C#
Pinkman, 2021-11-25 07:42:08

Why breaks the connection when changing the type?

Good afternoon!
I wrote my class:

ft::Socket::Socket(int fd) : _socket(fd) {}
ft::Socket::operator int() { return _socket; }

I don't change the data in it.
There is such a cycle, it looks through events using kevent. As far as I can see the code works (but if your keen eye noticed something wrong, I will be glad to comment), the connection is established and the clients hang without problems. But as soon as I change the type of the fd variable, everything breaks. More precisely, it works, it will accept one client, give it a portion of the data and that's it, no one else will connect, and the client is disconnected.
for (int i = 0; i < newEvents; i++) {
      // Если здесь поменять int на ft::Socket
      int fd(tEvent[i].ident);
      std::cerr << fd << "\n";
      if (tEvent[i].flags & EV_EOF) {
          std::cerr << "Connection close from remote host\n";
        close((int)fd);
      } else if (fd == _hostSock) {
        std::cerr << "New connection ";
        struct sockaddr_storage	remoteAdr;
        socklen_t size = sizeof(remoteAdr);
        int newSock = accept((int)fd, (struct sockaddr *)&remoteAdr, &size);
        if (newSock == -1)
          std::cerr << "error: Cant accept new connection\n";
        EV_SET(&event, newSock, EVFILT_READ, EV_ADD, 0, 0, NULL);
        kevent(kq, &event, 1, NULL, 0, NULL);
        std::cerr << "connection accepted\n";
        int flags = fcntl(newSock, F_GETFL, 0);
        if (fcntl(newSock, F_SETFL, flags | O_NONBLOCK) < 0)
          std::cerr << "Cant set socket to nonblock statement";
        EV_SET(&event, newSock, EVFILT_WRITE, EV_ADD | EV_ONESHOT, 0, 0, NULL);
        kevent(kq, &event, 1, NULL, 0, NULL);

How to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pinkman, 2021-11-25
@famousman204

All decided. I had close(fd) in my destructor. Accordingly, when leaving the visibility zone, the class calls the destructor and that's it. It turns out it seems like the socket is in the queue, but it is already closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question