Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <poll.h>
  8. #include "helper.h"
  9.  
  10. #define PORT 8080
  11. #define MAX_CLIENTS 10
  12.  
  13. int main() {
  14.     int server_fd, new_socket, valread;
  15.     struct sockaddr_in address;
  16.     int opt = 1;
  17.     int addrlen = sizeof(address);
  18.  
  19.     // Create socket
  20.     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
  21.         perror("socket failed");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.  
  25.     // Set socket options
  26.     if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
  27.                    &opt, sizeof(opt))) {
  28.         perror("setsockopt failed");
  29.         exit(EXIT_FAILURE);
  30.     }
  31.  
  32.     // Bind socket to port
  33.     address.sin_family = AF_INET;
  34.     address.sin_addr.s_addr = INADDR_ANY;
  35.     address.sin_port = htons(PORT);
  36.     if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
  37.         perror("bind failed");
  38.         exit(EXIT_FAILURE);
  39.     }
  40.  
  41.     // Listen for incoming connections
  42.     if (listen(server_fd, MAX_CLIENTS) < 0) {
  43.         perror("listen failed");
  44.         exit(EXIT_FAILURE);
  45.     }
  46.  
  47.     // Set up poll array
  48.     struct pollfd fds[MAX_CLIENTS];
  49.     fds[0].fd = server_fd;
  50.     fds[0].events = POLLIN;
  51.     int nfds = 1;
  52.  
  53.     // Wait for incoming connections and send message to client
  54.     while (1) {
  55.         if (poll(fds, nfds, -1) < 0) {
  56.             perror("poll failed");
  57.             exit(EXIT_FAILURE);
  58.         }
  59.  
  60.         // Check for incoming connections
  61.         if (fds[0].revents & POLLIN) {
  62.             if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
  63.                                      (socklen_t *)&addrlen)) < 0) {
  64.                 perror("accept failed");
  65.                 exit(EXIT_FAILURE);
  66.             }
  67.  
  68.             // Add new socket to poll array
  69.             fds[nfds].fd = new_socket;
  70.             fds[nfds].events = POLLIN;
  71.             nfds++;
  72.         }
  73.  
  74.         // Check for data from clients
  75.         for (int i = 1; i < nfds; i++) {
  76.             if (fds[i].revents & POLLIN) {
  77.                 struct message msg = {1, "Hello, client!"};
  78.                 if (send(fds[i].fd, &msg, sizeof(msg), 0) == -1) {
  79.                     perror("send failed");
  80.                     exit(EXIT_FAILURE);
  81.                 }
  82.             }
  83.         }
  84.     }
  85.  
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement