daily pastebin goal
68%
SHARE
TWEET

Untitled

a guest Mar 8th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //server code for creating and handling socket from
  2. //lines 3-53
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <unistd.h>
  10.  
  11. #define BUFFER_SIZE 1024
  12. #define on_error(...) { fprintf(stderr, __VA_ARGS__); fflush(stderr); exit(1); }
  13.  
  14. int main (int argc, char *argv[]) {
  15.  
  16.     //use port 23 instead of a port specified by a user
  17.     int port = 23;
  18.     FILE *stream;
  19.  
  20.     int server_fd, client_fd, err, size;
  21.     struct sockaddr_in server, client;
  22.     char buf[BUFFER_SIZE], ret[BUFFER_SIZE];
  23.  
  24.     server_fd = socket(AF_INET, SOCK_STREAM, 0);
  25.     if (server_fd < 0) on_error("Could not create socket\n");
  26.  
  27.     server.sin_family = AF_INET;
  28.     server.sin_port = htons(port);
  29.     server.sin_addr.s_addr = htonl(INADDR_ANY);
  30.  
  31.     int opt_val = 1;
  32.     setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt_val, sizeof opt_val);
  33.  
  34.     err = bind(server_fd, (struct sockaddr *) &server, sizeof(server));
  35.     if (err < 0) on_error("Could not bind socket\n");
  36.  
  37.     err = listen(server_fd, 128);
  38.     if (err < 0) on_error("Could not listen on socket\n");
  39.  
  40.     printf("Server is listening on %d\n", port);
  41.  
  42.     while (1) {
  43.         socklen_t client_len = sizeof(client);
  44.         client_fd = accept(server_fd, (struct sockaddr *) &client, &client_len);
  45.         printf("Establishing connection with client...\n");
  46.         if (client_fd < 0) on_error("Could not establish new connection\n")
  47.         else printf("Connection established on port %d\n", port);
  48.  
  49.         while (1) {
  50.             int read = recv(client_fd, buf, BUFFER_SIZE, 0);
  51.  
  52.             if (!read) break;
  53.             if (read < 0) on_error("Client read failed\n");
  54.             /*
  55.             err = send(client_fd, buf, read, 0);
  56.             if (err < 0) on_error("Client write failed\n");
  57.             */
  58.             printf("%s", buf);
  59.             strncat(buf, "", 1);
  60.             stream = popen("ls", "r"); //this is temporary until i figure out why the server wont process input properly
  61.  
  62.             while (fgets(buf, BUFFER_SIZE, stream) != NULL) {
  63.                 write(client_fd, stream, BUFFER_SIZE);
  64.             }
  65.  
  66.             pclose(stream);
  67.  
  68.             //err = send(client_fd, buf, read, 0);
  69.             //if (err < 0) on_error("Client write failed\n");
  70.         }
  71.     }
  72.  
  73.     return 0;
  74. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top