Guest User

Untitled

a guest
Jul 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <string.h>
  2. #include <sys/types.h>
  3. #include <winsock.h>
  4. #include <iostream>
  5. #include <sstream>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9.  
  10. using namespace std;
  11.  
  12. #define MYPORT "3490"  // the port users will be connecting to
  13. #define BACKLOG 10     // how many pending connections queue will hold
  14.  
  15. void *get_in_addr(struct sockaddr *sa)
  16. {
  17.     if (sa->sa_family == AF_INET) {
  18.         return &(((struct sockaddr_in*)sa)->sin_addr);
  19.     }
  20.  
  21.     return &(((struct sockaddr_in6*)sa)->sin6_addr);
  22. }
  23.  
  24. int main(int argc, char *argv[])
  25. {
  26.  
  27.     WSADATA wsaData;
  28.     if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
  29.         fprintf(stderr, "WSAStartup failed.\n");
  30.         exit(1);
  31.     }
  32.     struct sockaddr_storage their_addr;
  33.     socklen_t addr_size;
  34.     struct addrinfo hints, *res;
  35.     int sockfd, new_fd, len, numbytes;
  36.     len = 100;
  37.  
  38.     char s[100];
  39.  
  40.     char buf[100];
  41.  
  42.     memset(&hints, 0, sizeof hints);
  43.     hints.sin_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
  44.     hints.ai_socktype = SOCK_STREAM;
  45.     hints.ai_flags = AI_PASSIVE;     // fill in my IP for me
  46.  
  47.     getaddrinfo(NULL, "3490", &hints, &res);
  48.       cout << "Getaddrinfo error" << endl;
  49.  
  50.     sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  51.     if(sockfd == -1)
  52.       {
  53.     cout << "Socket error" << endl;
  54.     return 0;
  55.       }
  56.  
  57.     if(bind(sockfd, res->ai_addr, res->ai_addrlen) == -1)
  58.      {
  59.       cout << "Bind error" << endl;
  60.       return 0;
  61.      }
  62.     listen(sockfd, BACKLOG);
  63.  
  64.     addr_size = sizeof their_addr;
  65.     new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &addr_size);
  66.     while(1)
  67.     {
  68.  
  69.       /*perror("Connection status");
  70.       int numbytes = recv(new_fd, buf, len, 0);
  71.       perror("Message status");
  72.       buf[numbytes] = '\0';
  73.       printf("Client said: %s\n",buf);
  74.       string what = buf;
  75.  
  76.       int msglen = strlen(msg);
  77.       int bytes_sent = send(new_fd, msg, msglen, 0);
  78.       cout << "bytes sent back to client: " << bytes_sent << endl;*/
  79.       closesocket(new_fd);
  80.     }
  81.     closesocket(sockfd);
  82.     WSACleanup();
  83.     return 0;
  84. }
Add Comment
Please, Sign In to add comment