Combreal

socketServerWIthSelect02.cpp

Nov 10th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. //server
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <errno.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #pragma comment(lib, "ws2_32.lib")
  11. #pragma warning( disable : 4996)
  12. #define TRUE 1
  13. #define FALSE 0
  14. #define PORT 5555
  15. using namespace std;
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19.     int opt = TRUE;
  20.     int master_socket, addrlen, new_socket, client_socket[30], max_clients = 30, activity, i, valread, sd, sdBis;
  21.     int max_sd;
  22.     struct sockaddr_in address;
  23.     WSADATA WSAData;
  24.     WSAStartup(MAKEWORD(2, 0), &WSAData);
  25.     char buffer[1024];
  26.     fd_set readfds;
  27.     const char *message = "Welcome to FoD";
  28.     for (i = 0; i < max_clients; i++)
  29.     {
  30.         client_socket[i] = 0;
  31.     }
  32.     if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  33.     {
  34.         perror("socket failed");
  35.         exit(EXIT_FAILURE);
  36.     }
  37.     /*int Socketerror = WSAGetLastError();
  38.     cout << Socketerror << endl;*/
  39.     if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0)
  40.     {
  41.         perror("setsockopt");
  42.         exit(EXIT_FAILURE);
  43.     }
  44.     address.sin_family = AF_INET;
  45.     address.sin_addr.s_addr = INADDR_ANY;
  46.     address.sin_port = htons(PORT);
  47.     if (bind(master_socket, (struct sockaddr *)&address, sizeof(address)) < 0)
  48.     {
  49.         perror("bind failed");
  50.         exit(EXIT_FAILURE);
  51.     }
  52.     printf("Listener on port %d \n", PORT);
  53.     if (listen(master_socket, 3) < 0)
  54.     {
  55.         perror("listen");
  56.         exit(EXIT_FAILURE);
  57.     }
  58.     addrlen = sizeof(address);
  59.     puts("Waiting for connections ...");
  60.  
  61.     while (TRUE)
  62.     {
  63.         FD_ZERO(&readfds);
  64.         FD_SET(master_socket, &readfds);
  65.         max_sd = master_socket;
  66.         for (i = 0; i < max_clients; i++)
  67.         {
  68.             sd = client_socket[i];
  69.             if (sd > 0)
  70.             {
  71.                 FD_SET(sd, &readfds);
  72.             }
  73.             if (sd > max_sd)
  74.             {
  75.                 max_sd = sd;
  76.             }
  77.         }
  78.         activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
  79.         if ((activity < 0) && (errno != EINTR))
  80.         {
  81.             printf("select error");
  82.         }
  83.         if (FD_ISSET(master_socket, &readfds))
  84.         {
  85.             if ((new_socket = accept(master_socket, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0)
  86.             {
  87.                 perror("accept");
  88.                 exit(EXIT_FAILURE);
  89.             }
  90.             printf("New connection , socket fd is %d , ip is : %s , port : %d\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  91.             if (send(new_socket, message, strlen(message), 0) != strlen(message))
  92.             {
  93.                 perror("send");
  94.             }
  95.             puts("Welcome message sent successfully");
  96.             for (i = 0; i < max_clients; i++)
  97.             {
  98.                 if (client_socket[i] == 0)
  99.                 {
  100.                     client_socket[i] = new_socket;
  101.                     printf("Adding to list of sockets as %d\n", i);
  102.  
  103.                     break;
  104.                 }
  105.             }
  106.         }
  107.         for (i = 0; i < max_clients; i++)
  108.         {
  109.             memset(buffer, 0, sizeof buffer);
  110.             sd = client_socket[i];
  111.             if (FD_ISSET(sd, &readfds))
  112.             {
  113.                 if ((valread = recv(sd, buffer, 1024, 0)) < 0)
  114.                 {
  115.                     getpeername(sd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
  116.                     printf("Host disconnected , ip %s , port %d \n", inet_ntoa(address.sin_addr), ntohs(address.sin_port));
  117.                     closesocket(sd);
  118.                     client_socket[i] = 0;
  119.                 }
  120.                 else
  121.                 {
  122.                     buffer[valread] = '\0';
  123.                     cout << buffer << endl;
  124.                     for (i = 0; i < max_clients; i++)
  125.                     {
  126.                         send(client_socket[i], buffer, strlen(buffer), 0);
  127.                     }
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     system("pause");
  134. }
  135.  
  136. //client
  137. #include <string>
  138. #include <iostream>
  139. #include <winsock2.h>
  140. #include <Ws2tcpip.h>
  141. #include <thread>
  142. #pragma comment(lib, "ws2_32.lib")
  143. using namespace std;
  144.  
  145. void *sendMessage(void *sock_desc)
  146. {
  147.     while (TRUE)
  148.     {
  149.         string talk = "";
  150.         getline(cin, talk);
  151.         if (send(*((int *)sock_desc), talk.c_str(), sizeof(talk) + 1, 0) < 0)
  152.         {
  153.             cout << "Send fail" << endl;
  154.         }
  155.     }
  156. }
  157.  
  158. void *receiveMessage(void *sock_desc)
  159. {
  160.     char responce[2000];
  161.     while (TRUE)
  162.     {
  163.         memset(responce, 0, sizeof responce);
  164.         if (recv(*((int *)sock_desc), responce, sizeof(responce), 0) < 0)
  165.         {
  166.             puts("recv failed");
  167.  
  168.         }
  169.         cout << responce << endl;
  170.     }
  171. }
  172.  
  173. int main()
  174. {
  175.     WSADATA WSAData;
  176.     SOCKET server;
  177.     int sock;
  178.     SOCKADDR_IN addr;
  179.     WSAStartup(MAKEWORD(2, 0), &WSAData);
  180.     server = socket(AF_INET, SOCK_STREAM, 0);
  181.     InetPton(AF_INET, "192.168.1.16", &addr.sin_addr.s_addr);
  182.     addr.sin_family = AF_INET;
  183.     addr.sin_port = htons(5555);
  184.     sock = server;
  185.     int * newSocket = static_cast<int*>(malloc(1));
  186.     *newSocket = sock;
  187.     if (connect(server, (SOCKADDR *)&addr, sizeof(addr)) == 0)
  188.     {
  189.         cout << "Connected to server!" << endl;
  190.         /*while (talk != "quit")
  191.         {
  192.             memset(responce, 0, sizeof responce);
  193.             if (recv(server, responce, sizeof(responce), 0) > 0)
  194.             {
  195.                 cout << responce << endl;
  196.             }
  197.             else
  198.             {
  199.                 getline(cin, talk);
  200.                 if (talk.c_str() != NULL)
  201.                 {
  202.                     if (send(server, talk.c_str(), sizeof(talk), 0) <= 0)
  203.                     {
  204.                         talk = "quit";
  205.                     }
  206.                 }
  207.             }
  208.         }*/
  209.         //thread(&sendThread, NULL, sendMessage((void *)server));
  210.         //sendThread = thread(sendMessage((void *)server), this);
  211.         thread sendThread(sendMessage, (void *)newSocket);
  212.         thread receiveThread(receiveMessage, (void *)newSocket);
  213.         sendThread.join();
  214.         receiveThread.join();
  215.         closesocket(server);
  216.         WSACleanup();
  217.         cout << "Server has closed." << endl << endl;
  218.     }
  219.     else
  220.     {
  221.         cout << "Couldn't connect to server." << endl;
  222.     }
  223.     system("pause");
  224. }
Add Comment
Please, Sign In to add comment