Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <iostream>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <arpa/inet.h>
  8. #include <sys/types.h>
  9. #include <netinet/in.h>
  10.  
  11. #define MAX_SIZE 1024
  12.  
  13. using namespace std;
  14.  
  15. bool write_fully(int fd, const char *buffer, ssize_t count) {
  16.     const char *ptr = buffer;
  17.     const char *end = buffer + count;
  18.     while (ptr != end) {
  19.     ssize_t written = write(fd, (void*) ptr, end - ptr);
  20.     if (written == -1) {
  21.         return false;
  22.     }
  23.     ptr += written;
  24.     }
  25.     return true;
  26. }
  27.  
  28. void server_for_connection(int socket_fd) {
  29.     int read_count;
  30.     char request_buf[MAX_SIZE];
  31.     while (1) {
  32.     read_count = read(socket_fd, (void*) request_buf, MAX_SIZE);
  33.     if (read_count == 0) {
  34.         return; // EOF
  35.     } else if (read_count == -1) {
  36.         cerr << "read: " << strerror(errno) << endl;
  37.         return;
  38.     }
  39.  
  40.     string response = string(request_buf, read_count);
  41.  
  42.     cout << "read: " << response;
  43.  
  44.     if (response.substr(0,4).find("USER") != string::npos) {
  45.         string acceptedUser = "230 Welcome!!!\n";
  46.         const char* acceptedCstr = acceptedUser.c_str();
  47.         send(socket_fd, acceptedCstr, strlen(acceptedCstr), 0);  
  48.     }
  49.  
  50.     if (false == write_fully(socket_fd, request_buf, read_count)) {
  51.         cerr << "write: " << strerror(errno) << endl;
  52.         return;
  53.     }
  54.     }
  55. }
  56.  
  57. int main(int argc, char** argv) {
  58.     if (argc < 2) {
  59.         std::cerr << "Usage: " << argv[0] << " PORT-NUMBER" << std::endl;
  60.         return 1;
  61.     }
  62.  
  63.     const char *portname = argv[1];
  64.     string stringMsg = "220 Please reply.\n";
  65.     const char* exampleMsg = stringMsg.c_str();
  66.     socklen_t socksize = sizeof(struct sockaddr_in);
  67.  
  68.     struct sockaddr_in addr_dest;
  69.     struct sockaddr_in addr;
  70.  
  71.     addr.sin_family = AF_INET;
  72.     inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr.s_addr));
  73. //    addr.sin_addr.s_addr = INADDR_ANY;
  74.     addr.sin_port = htons(atoi(portname));
  75.  
  76.     int server_socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  77.  
  78.     if (bind(server_socket_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
  79.     cout << "Error with binding!!!" << endl;
  80.     return 1;
  81.     }
  82.  
  83.     listen(server_socket_fd, 1);
  84.     int client_socket_fd = accept(server_socket_fd, (struct sockaddr*) &addr_dest, &socksize);
  85.  
  86.     while(client_socket_fd) {
  87.     printf("Connection from: %s\n", inet_ntoa(addr.sin_addr));
  88.     send(client_socket_fd, exampleMsg, strlen(exampleMsg), 0);
  89.  
  90.     server_for_connection(client_socket_fd);
  91.    
  92.     close(client_socket_fd);
  93.     client_socket_fd = accept(server_socket_fd, (struct sockaddr*) &addr_dest, &socksize);
  94.     }
  95.  
  96.     close(server_socket_fd);
  97.  
  98.  
  99. //    std::cout << "Code to bind to 127.0.0.1 port " << portname
  100. //              << " and listen for connections unimplemented." << std::endl;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement