Advertisement
demozylak

po cw 8

Dec 4th, 2016
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.16 KB | None | 0 0
  1. //**********************************************
  2. //* client_main.cpp
  3. //**********************************************
  4. #include <iostream>
  5. #include "Client.h"
  6. #include <string>
  7.  
  8. //CLIENrt
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13.  
  14.     Client c("localhost");
  15.  
  16.     char bufor[DEFAULT_BUFLEN];
  17.  
  18.     string stuff = "";
  19.     do {
  20.         cout << "send stuff: ";
  21.         cin >> stuff;
  22.  
  23.         memcpy(bufor, stuff.c_str(), stuff.size() + 1); // overflow
  24.  
  25.         c.sen(bufor);
  26.  
  27.         memcpy(bufor, "deadbeefdeadbeef", strlen("deadbeefdeadbeef") + 1);
  28.  
  29.         int btz = c.rec(bufor);
  30.         cout << "\n[receiving " << btz << "bytes.] : " << bufor << endl;
  31.  
  32.  
  33.  
  34.  
  35.     } while (stuff != "Q");
  36.            
  37.  
  38.    
  39.     //cout << "Listening for client ... " <<endl;
  40.     //cout << "Ret Code: " << s.acc() << endl;
  41.  
  42.  
  43.     cout << "KONIETZ";
  44.  
  45.     system("pause");
  46.  
  47.     return 0;
  48.  
  49. }
  50.  
  51.  
  52.  
  53.  
  54. //**********************************************
  55. //* Client.h
  56. //**********************************************
  57. #pragma once
  58.  
  59. #define WIN32_LEAN_AND_MEAN
  60.  
  61. #include <windows.h>
  62. #include <winsock2.h>
  63. #include <ws2tcpip.h>
  64. #include <iostream>
  65.  
  66. #pragma comment (lib, "Ws2_32.lib")
  67. #pragma comment (lib, "Mswsock.lib")
  68. #pragma comment (lib, "AdvApi32.lib")
  69.  
  70. #define DEFAULT_BUFLEN 512
  71. #define DEFAULT_PORT "6666"
  72.  
  73.  
  74. using namespace std;
  75.  
  76. class Client
  77. {
  78. protected:
  79.     WSADATA wsaData;
  80.     SOCKET ConnectSocket;
  81.     int iResult;
  82.  
  83. public:
  84.     Client(const char * addr);
  85.     ~Client(void);
  86.  
  87.     int sen(char * sendbuf);
  88.     int rec(char * recvbuf);
  89. };
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97. //**********************************************
  98. //* Client.cpp
  99. //**********************************************
  100. #include "Client.h"
  101.  
  102. Client::Client(const char * addr)
  103. {
  104.    
  105.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  106.     if (iResult != 0) {
  107.         cout << "wsastartup failed" << endl;
  108.     }
  109.  
  110.     struct addrinfo *result = NULL,
  111.                     *ptr = NULL,
  112.                     hints;
  113.     ZeroMemory( &hints, sizeof(hints) );
  114.     hints.ai_family = AF_UNSPEC;
  115.     hints.ai_socktype = SOCK_STREAM;
  116.     hints.ai_protocol = IPPROTO_TCP;
  117.  
  118.     iResult = getaddrinfo(addr, DEFAULT_PORT, &hints, &result);
  119.     if ( iResult != 0 ) {
  120.         WSACleanup();
  121.         cout << "getaddrinfo failed" << endl;
  122.     }
  123.  
  124.  
  125.    // Attempt to connect to an address until one succeeds
  126.     for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
  127.  
  128.         // Create a SOCKET for connecting to server
  129.         ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  130.             ptr->ai_protocol);
  131.         if (ConnectSocket == INVALID_SOCKET) {
  132.            
  133.             WSACleanup();
  134.             cout << "connect socket create failed" << endl;
  135.         }
  136.  
  137.         // Connect to server.
  138.         iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  139.         if (iResult == SOCKET_ERROR) {
  140.             closesocket(ConnectSocket);
  141.             ConnectSocket = INVALID_SOCKET;
  142.             continue;
  143.         }
  144.         break;
  145.     }
  146.  
  147.     freeaddrinfo(result);
  148.  
  149.     if (ConnectSocket == INVALID_SOCKET) {
  150.         WSACleanup();
  151.         cout << "connect failed" << endl;
  152.     }
  153.  
  154. }
  155.  
  156. Client::~Client(void)
  157. {
  158.         // shutdown the connection since no more data will be sent
  159.     iResult = shutdown(ConnectSocket, SD_SEND);
  160.     if (iResult == SOCKET_ERROR) {
  161.         cout << "shutdown failed with error: "<< WSAGetLastError()<< endl;
  162.         closesocket(ConnectSocket);
  163.         WSACleanup();
  164.  
  165.     }
  166. }
  167.  
  168.  
  169.  
  170. int Client::sen(char * sendbuf)
  171. {
  172.  
  173.     // Send an initial buffer
  174.     iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf) + 1, 0 );
  175.     if (iResult == SOCKET_ERROR) {
  176.         cout << "send failed with error: " <<WSAGetLastError() << endl;
  177.         closesocket(ConnectSocket);
  178.         WSACleanup();
  179.         return -1;
  180.     }
  181.  
  182.     cout << "Bytes Sent: " <<  iResult;
  183.  
  184.  
  185.  
  186.  
  187.  
  188.     return 0;
  189. }
  190.  
  191. int Client::rec(char * recvbuf)
  192. {
  193.  
  194.  
  195.     return recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
  196.  
  197.  
  198.     return 0;
  199. }
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. //**********************************************
  207. //* server_main.cpp
  208. //**********************************************
  209. #include <iostream>
  210. #include "Server.h"
  211. //SERVER
  212. using namespace std;
  213.  
  214. int main()
  215. {
  216.  
  217.     Server s;
  218.  
  219.     s.run();
  220.  
  221.     system("pause");
  222.  
  223.     return 0;
  224.  
  225. }
  226.  
  227.  
  228.  
  229.  
  230. //**********************************************
  231. //* Server.h
  232. //**********************************************
  233. #pragma once
  234.  
  235. #define WIN32_LEAN_AND_MEAN
  236.  
  237. #include <windows.h>
  238. #include <winsock2.h>
  239. #include <ws2tcpip.h>
  240. #include <iostream>
  241.  
  242. #pragma comment (lib, "Ws2_32.lib")
  243. #pragma comment (lib, "Mswsock.lib")
  244. #pragma comment (lib, "AdvApi32.lib")
  245.  
  246. #define DEFAULT_BUFLEN 512
  247. #define DEFAULT_PORT "6666"
  248.  
  249. using namespace std;
  250.  
  251. class Server
  252. {
  253. protected:
  254.     WSADATA wsaData;
  255.     int iResult;
  256.  
  257.     SOCKET ListenSocket;
  258.     SOCKET ClientSocket;
  259.     struct addrinfo *result;
  260.  
  261. public:
  262.     Server(void);
  263.     ~Server(void);
  264.     int acc(void); // accept
  265.     int echo();
  266.     void shtd(); // shutdown
  267.     void run();
  268.  
  269. };
  270.  
  271.  
  272.  
  273.  
  274.  
  275. //**********************************************
  276. //* Server.cpp
  277. //**********************************************
  278. #include "Server.h"
  279.  
  280.  
  281.  
  282.  
  283. Server::Server(void)
  284. {
  285.     ListenSocket = INVALID_SOCKET;
  286.     ClientSocket = INVALID_SOCKET;
  287.  
  288.  
  289.     result = NULL;
  290.     struct addrinfo hints;
  291.  
  292.    
  293.     // Initialize Winsock
  294.     iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  295.     if (iResult != 0) {
  296.         cout << "wsa startup failed" <<endl;
  297.     }
  298.  
  299.     ZeroMemory(&hints, sizeof(hints));
  300.     hints.ai_family = AF_INET;
  301.     hints.ai_socktype = SOCK_STREAM;
  302.     hints.ai_protocol = IPPROTO_TCP;
  303.     hints.ai_flags = AI_PASSIVE;
  304.  
  305.     // Resolve the server address and port
  306.     iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
  307.     if ( iResult != 0 ) {
  308.         WSACleanup();
  309.         cout << "getaddr info failed" <<endl;
  310.     }
  311.  
  312.     // Create a SOCKET for connecting to server
  313.     ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  314.     if (ListenSocket == INVALID_SOCKET) {
  315.         freeaddrinfo(result);
  316.         WSACleanup();
  317.         cout << "listen socket create failed" <<endl;
  318.     }
  319.  
  320.     // Setup the TCP listening socket
  321.     iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
  322.     if (iResult == SOCKET_ERROR) {
  323.         freeaddrinfo(result);
  324.         closesocket(ListenSocket);
  325.         WSACleanup();
  326.         cout << "bind failed" <<endl;
  327.     }
  328.  
  329.     freeaddrinfo(result);
  330.  
  331. }
  332.  
  333. Server::~Server(void)
  334. {
  335.  
  336.     closesocket(ListenSocket);
  337.     closesocket(ClientSocket);
  338.     WSACleanup();
  339.  
  340. }
  341.  
  342.  
  343. int Server::acc(void)
  344. {
  345.  
  346.     iResult = listen(ListenSocket, SOMAXCONN);
  347.     if (iResult == SOCKET_ERROR) {
  348.         closesocket(ListenSocket);
  349.         WSACleanup();
  350.         return -5;
  351.     }
  352.  
  353.     // Accept a client socket
  354.     ClientSocket = accept(ListenSocket, NULL, NULL);
  355.     if (ClientSocket == INVALID_SOCKET) {
  356.         closesocket(ListenSocket);
  357.         WSACleanup();
  358.         return -6;
  359.     }
  360.  
  361.     return 0;
  362.  
  363. }
  364.  
  365. void Server::run()
  366. {
  367.  
  368.     int echoRet = 1;
  369.     do {
  370.         cout << "Listening for client ... " <<endl;
  371.         cout << "Client connect ret code: " << acc() << endl;
  372.         do {
  373.             echoRet = echo();
  374.             cout << "Echoing " << echoRet << "bytes"<<endl;
  375.         } while(echoRet > 0);
  376.  
  377.     } while(echoRet >= 0);
  378.  
  379.     cout << "shutting down";
  380.     shtd();
  381.  
  382.  
  383.  
  384. }
  385.  
  386. int Server::echo() // 0 - rozlaczenie klienta -1 - shutdown >0 - ilosc przeslanych bajtow
  387. {
  388.     int iSendResult;
  389.     char recvbuf[DEFAULT_BUFLEN];
  390.  
  391.     iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0);
  392.     if (iResult > 0) {
  393.         //cout << "Bytes received: %d\n"<<iResult;
  394.  
  395.         if (strcmp(recvbuf, "shutdown") == 0) {
  396.             return -1;
  397.         }
  398.  
  399.  
  400.     // Echo the buffer back to the sender
  401.         iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
  402.         if (iSendResult == SOCKET_ERROR) {
  403.             return 0;
  404.         }
  405.         return iResult;
  406.     }
  407.     else if (iResult == 0)
  408.         return 0;
  409.     else  {
  410.         return 0;
  411.     }
  412.  
  413.  
  414.    
  415.  
  416. }
  417.  
  418. void Server::shtd()
  419. {
  420.     iResult = shutdown(ClientSocket, SD_SEND);
  421.     if (iResult == SOCKET_ERROR) {
  422.         cout << "shutdown failed with error: " << WSAGetLastError() << endl;
  423.         closesocket(ClientSocket);
  424.         WSACleanup();
  425.     }
  426.  
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement