Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <arpa/inet.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netdb.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <string>
  10. #include <netinet/in.h>
  11.  
  12. using namespace std;
  13.  
  14. int main()
  15. {
  16.     sockaddr_in serverData;
  17.     //Create socket
  18.     int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  19.     if(sockfd == -1)
  20.     {
  21.         cout << "Can't create a socket" << endl;
  22.         return -1;
  23.     }
  24.  
  25.     serverData.sin_family = AF_INET;
  26.     serverData.sin_port = htonl(8080);
  27.     inet_pton(AF_INET, "0.0.0.0", &serverData.sin_addr);
  28.  
  29.     //Bind socket
  30.     int bindRes = bind(sockfd, (sockaddr *)&serverData, sizeof(serverData));
  31.     if(bindRes == -1)
  32.     {
  33.         cout << "Can't bind" << endl;
  34.         return -2;
  35.     }
  36.  
  37.     //Listen
  38.     int listenRes = listen(sockfd, 3);
  39.     if(listenRes == -1)
  40.     {
  41.         cout << "In listening" << endl;
  42.         return -3;
  43.     }
  44.  
  45.     sockaddr_in client;
  46.     //Accept the only connection and close the port
  47.     cout << "Waiting for connection " << endl;
  48.     int clientSock = accept(sockfd, (sockaddr*) &client, (socklen_t*) sizeof(client));
  49.     if(clientSock < 0)
  50.     {
  51.         cout << "Can't accept connection " << endl;
  52.         exit(93);
  53.     }
  54.  
  55.     close(sockfd);
  56.  
  57.     char clientName[NI_MAXHOST];
  58.     char clientPort[NI_MAXSERV];
  59.  
  60.     //Fill buffer with clients host name and port number
  61.     cout << "Here" << endl;
  62.     getnameinfo((sockaddr*) &client, (socklen_t) sizeof(client), clientName, NI_MAXHOST, clientPort, NI_MAXSERV, 0);
  63.  
  64.     cout << "Client name is: " << (string)clientName << endl;
  65.     cout << "Client port is: " << (string)clientPort << endl;
  66.  
  67.     while(1)
  68.     {
  69.         char buff[4096];
  70.        
  71.         //Receive data that was send by client
  72.         int bytesRecv = recv(clientSock, buff, 4096, 0);
  73.         if(bytesRecv == -1)
  74.         {
  75.             break;
  76.         }
  77.         if(bytesRecv == 0)
  78.         {
  79.             cout << "Client disconnected" << endl;
  80.             break;
  81.         }
  82.         else
  83.         {
  84.             //Output that data and send it back
  85.             cout << string(buff, 0, bytesRecv) << endl;
  86.  
  87.             send(clientSock, buff, bytesRecv + 1, 0);
  88.         }
  89.     }
  90.  
  91.     close(clientSock);
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement