Advertisement
kator

tcp2

Mar 21st, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. /*
  2.  *
  3.  */
  4.  
  5. #include <netinet/in.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/socket.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <netdb.h>
  12.  
  13. int main(int argc, char** argv) {
  14.     if (argc != 3) {
  15.         printf("usage: ./tcp-server <IP> <PORT>\n");
  16.         return -1;
  17.     }
  18.    
  19.   socklen_t sl;
  20.   int sfd, cfd, on = 1;
  21.   struct sockaddr_in saddr, caddr;
  22.  
  23.   memset(&saddr, 0, sizeof(saddr));
  24.   saddr.sin_family = AF_INET;
  25.   saddr.sin_addr.s_addr = INADDR_ANY;
  26.   saddr.sin_port = htons(1234);
  27.   sfd = socket(PF_INET, SOCK_STREAM, 0);
  28.   setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*) &on, sizeof(on));
  29.   bind(sfd, (struct sockaddr*) &saddr, sizeof(saddr));
  30.   listen(sfd, 5);
  31.  
  32.   unsigned long long int asdf = 0;
  33.  
  34.   while(1) {
  35.    
  36.     memset(&caddr, 0, sizeof(caddr));
  37.     sl = sizeof(caddr);
  38.     cfd = accept(sfd, (struct sockaddr*) &caddr, &sl);
  39.     char buff[256] = {0};
  40.     int sz = read(cfd, &buff, 256);
  41.     if (sz <= 0) {
  42.         printf("something went wrong!");
  43.     }
  44.  
  45.     printf("[got:]\t%s\n", buff);
  46.  
  47.     int csfd;
  48.     struct sockaddr_in csaddr;
  49.     struct hostent* caddrent;
  50.    
  51.     caddrent = gethostbyname(argv[1]);
  52.     csfd = socket(PF_INET, SOCK_STREAM, 0);
  53.     memset(&csaddr, 0, sizeof(csaddr));
  54.     csaddr.sin_family = AF_INET;
  55.     csaddr.sin_port = htons(atoi(argv[2]));
  56.     memcpy(&csaddr.sin_addr.s_addr, caddrent->h_addr, caddrent->h_length);
  57.     connect(csfd, (struct sockaddr*) &csaddr, sizeof(csaddr));
  58.     //if (asdf > 10000) {
  59.         write(csfd, &buff, sz);
  60.     //}
  61.     //else {
  62.     //  char tmp_buff[] = "MUAHAHAHA\n\n\n\ninjecting content\v\v\v\t\t\n";
  63.     //  write(csfd, &tmp_buff, 34);
  64.     //}
  65.     close(csfd);
  66.  
  67.     //asdf++;
  68.  
  69.   }
  70.   close(sfd);
  71.   return EXIT_SUCCESS;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement