Advertisement
AntonioVillanueva

Socket epoll non bloc mulitple sockets ...

Sep 23rd, 2024
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. #include <cstring>
  2. #include <iostream>
  3. #include <netinet/in.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <string>
  7. #include <arpa/inet.h>
  8. #include <sys/epoll.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11.  
  12. //#define IP "127.0.0.1"
  13. #define IP "192.168.10.11"
  14. #define PORT 3310
  15. #define BUFFER_SIZE 1024
  16. #define MAX_EVENTS 1
  17.  
  18. using namespace std;
  19.  
  20. int setNonBlocking(int fd) {
  21.     int flags = fcntl(fd, F_GETFL, 0);
  22.     if (flags == -1) return -1;
  23.     return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
  24. }
  25.  
  26. int main() {
  27.     int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
  28.     if (clientSocket == -1) {
  29.         cerr << "Failed to create socket" << endl;
  30.         return 1;
  31.     }
  32.  
  33.     sockaddr_in serverAddress;
  34.     serverAddress.sin_family = AF_INET;
  35.     serverAddress.sin_port = htons(PORT);
  36.     serverAddress.sin_addr.s_addr = inet_addr(IP);
  37.  
  38.     if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1) {
  39.         if (errno != EINPROGRESS) {
  40.             cerr << "Connection failed to " << IP << ":" << PORT << endl;
  41.             close(clientSocket);
  42.             return 1;
  43.         }
  44.     }
  45.  
  46.     if (setNonBlocking(clientSocket) == -1) {
  47.         cerr << "Failed to set socket to non-blocking" << endl;
  48.         close(clientSocket);
  49.         return 1;
  50.     }
  51.  
  52.     int epollfd = epoll_create1(0);
  53.     if (epollfd == -1) {
  54.         cerr << "Failed to create epoll file descriptor" << endl;
  55.         close(clientSocket);
  56.         return 1;
  57.     }
  58.  
  59.     struct epoll_event ev, events[MAX_EVENTS];
  60.     ev.events = EPOLLOUT | EPOLLIN;
  61.     ev.data.fd = clientSocket;
  62.     if (epoll_ctl(epollfd, EPOLL_CTL_ADD, clientSocket, &ev) == -1) {
  63.         cerr << "Failed to add file descriptor to epoll" << endl;
  64.         close(epollfd);
  65.         close(clientSocket);
  66.         return 1;
  67.     }
  68.  
  69.     const char* message = "PING\n";
  70.     bool messageSent = false;
  71.     char buffer[BUFFER_SIZE] = {0};
  72.     int bytesReceived = 0;
  73.  
  74.     while (true) {
  75.         int nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
  76.         if (nfds == -1) {
  77.             cerr << "epoll_wait failed" << endl;
  78.             break;
  79.         }
  80.  
  81.         for (int n = 0; n < nfds; ++n) {
  82.             if (events[n].events & EPOLLOUT) {
  83.                 if (!messageSent) {
  84.                     int bytesSent = send(clientSocket, message, strlen(message), 0);
  85.                     if (bytesSent == -1) {
  86.                         cerr << "Failed to send message" << endl;
  87.                         goto cleanup;
  88.                     }
  89.                     cout << "Message sent: " << message;
  90.                     messageSent = true;
  91.                     ev.events = EPOLLIN;
  92.                     epoll_ctl(epollfd, EPOLL_CTL_MOD, clientSocket, &ev);
  93.                 }
  94.             } else if (events[n].events & EPOLLIN) {
  95.                 bytesReceived = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
  96.                 if (bytesReceived == -1) {
  97.                     if (errno != EAGAIN && errno != EWOULDBLOCK) {
  98.                         cerr << "Failed to receive response" << endl;
  99.                         goto cleanup;
  100.                     }
  101.                 } else if (bytesReceived == 0) {
  102.                     cout << "Server closed the connection" << endl;
  103.                     goto cleanup;
  104.                 } else {
  105.                     cout << "Response received: " << buffer << " , Bytes Rx: " << bytesReceived << endl;
  106.                     goto cleanup;
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112. cleanup:
  113.     close(epollfd);
  114.     close(clientSocket);
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement