Advertisement
Guest User

Untitled

a guest
Apr 27th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7.  
  8. #include "err.h"
  9.  
  10. #define BUFFER_SIZE 2000
  11. #define QUEUE_LENGTH 5
  12. #define MAX_MENU_STR 100
  13. #define MIN_PORT 1024
  14. #define MAX_PORT 65535
  15.  
  16.  
  17. char RESET_TERM[] = {0x1B,'c','\0'};
  18. // IAC DO LINEMODE IAC WILL ECHO
  19. char NEGOTIATION[] = "\377\375\042\377\373\001";
  20.  
  21. char MAIN_MENU[] = "Opcja A\n\r"
  22.                     "Opcja B\n\r"
  23.                     "Koniec\n\r";
  24.  
  25. char MENU_SECOND[] = "Opcja B1\n\r"
  26.                      "Opcja B2\n\r"
  27.                      "Wstecz\n\r";
  28.  
  29.  
  30. // void menu_str(char *str, int menu_type, ) {
  31. //   //TODO
  32. // }
  33.  
  34.  
  35. char ARR_UP[] = {27, 91, 65};
  36. char ARR_DOWN[] = {27, 91, 66};
  37.  
  38. int main(int argc, char *argv[])
  39. {
  40.   int sock, msg_sock;
  41.   struct sockaddr_in server_address;
  42.   struct sockaddr_in client_address;
  43.   socklen_t client_address_len;
  44.  
  45.   char buffer[BUFFER_SIZE];
  46.   ssize_t len, snd_len;
  47.  
  48.   // checking the number of arguments
  49.   if (argc != 2) {
  50.     fprintf(stderr, "One argument required: port number\n");
  51.     return 1;
  52.   }
  53.  
  54.   // checking if given port number is in the correct range
  55.   int port_num = argv[1];
  56.   if (port_num <= MIN_PORT || port_num >= MAX_PORT) {
  57.     fprintf(stderr, "Port number not in range <%d, %d>\n", MIN_PORT, MAX_PORT);
  58.     return 1;
  59.   }
  60.  
  61.   sock = socket(PF_INET, SOCK_STREAM, 0); // creating IPv4 TCP socket
  62.   if (sock < 0)
  63.     syserr("socket");
  64.   // after socket() call; we should close(sock) on any execution path;
  65.   // since all execution paths exit immediately, sock would be closed when program terminates
  66.  
  67.   server_address.sin_family = AF_INET; // IPv4
  68.   server_address.sin_addr.s_addr = htonl(INADDR_ANY); // listening on all interfaces
  69.   server_address.sin_port = htons(port_num); // listening on port port_num
  70.  
  71.   // bind the socket to a concrete address
  72.   if (bind(sock, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
  73.     syserr("bind");
  74.  
  75.   // switch to listening (passive open)
  76.   if (listen(sock, QUEUE_LENGTH) < 0)
  77.     syserr("listen");
  78.  
  79.   for (;;) {
  80.     client_address_len = sizeof(client_address);
  81.     // get client connection from the socket
  82.     msg_sock = accept(sock, (struct sockaddr *) &client_address, &client_address_len);
  83.  
  84.     if (msg_sock < 0) {
  85.       syserr("accept");
  86.     } else {
  87.       printf("Client has connected.\n");
  88.     }
  89.  
  90.     // if(write(msg_sock, RESET_TERM, sizeof(RESET_TERM)) != sizeof(RESET_TERM)) {
  91.     //   syserr("partial / failed write");
  92.     // }
  93.  
  94.     if(write(msg_sock, NEGOTIATION, 6) != 6) {
  95.       syserr("failed negotiation");
  96.     }
  97.  
  98.     do {
  99.       if(write(msg_sock, RESET_TERM, sizeof(RESET_TERM)) != sizeof(RESET_TERM)) {
  100.         syserr("partial / failed write");
  101.       }
  102.  
  103.       if (write(msg_sock, MAIN_MENU, sizeof(MAIN_MENU)) != sizeof(MAIN_MENU)) {
  104.         syserr("failed menu");
  105.       }
  106.  
  107.       len = read(msg_sock, buffer, sizeof(buffer));
  108.       if(len == 3) {
  109.         if(strncmp(buffer, ARR_UP, 3) == 0) {
  110.           printf("A\n");
  111.         } else if(strncmp(buffer, ARR_DOWN, 3) == 0) {
  112.           printf("B\n");
  113.         } else {
  114.           printf("C\n");
  115.         }
  116.       }
  117.  
  118.       if (len < 0)
  119.         syserr("reading from client socket");
  120.       else {
  121.         // printf("read from socket: %zd bytes: %.*s\n", len, (int) len, buffer);
  122.         // snd_len = write(msg_sock, buffer, len);
  123.         // if (snd_len != len)
  124.         //   syserr("writing to client socket");
  125.       }
  126.     } while (len > 0);
  127.  
  128.     printf("Ending connection.\n");
  129.  
  130.     if (close(msg_sock) < 0)
  131.       syserr("close");
  132.   }
  133.  
  134.   return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement