Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. /*
  2. Author: Pindrought
  3. Date: 11/13/2015
  4. This is the solution for the client that you should have at the end of tutorial 1.
  5. */
  6. #include "pch.h"
  7. #include <iostream>
  8. #include <winsock2.h>
  9. #include <ws2tcpip.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string>
  13.  
  14. #pragma comment(lib,"ws2_32.lib")
  15. #pragma warning(disable:4996)
  16.  
  17.  
  18. std::string IP_ADDR;
  19. int PORT = 8080;
  20.  
  21. int main()
  22. {
  23. std::cout << "Podaj adres IP serwera: " << std::endl;
  24. std::cin >> IP_ADDR;
  25. std::cout << "Podaj port: " << std::endl;
  26. std::cin >> PORT;
  27.  
  28. //Winsock Startup
  29. WSAData wsaData;
  30. WORD DllVersion = MAKEWORD(2, 1);
  31. if (WSAStartup(DllVersion, &wsaData) != 0) //If WSAStartup returns anything other than 0, then that means an error has occured in the WinSock Startup.
  32. {
  33. MessageBoxA(NULL, "Winsock startup failed", "Error", MB_OK | MB_ICONERROR);
  34. exit(1);
  35. }
  36.  
  37. SOCKADDR_IN addr; //Address to be binded to our Connection socket
  38. int sizeofaddr = sizeof(addr); //Need sizeofaddr for the connect function
  39.  
  40. addr.sin_addr.s_addr = inet_addr(IP_ADDR.c_str()); //Address = localhost (this pc)
  41. addr.sin_port = htons(PORT); //Port = 8080
  42. addr.sin_family = AF_INET; //IPv4 Socket
  43.  
  44. SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL); //Set Connection socket
  45. if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //If we are unable to connect...
  46. {
  47. MessageBoxA(NULL, "Failed to Connect", "Error", MB_OK | MB_ICONERROR);
  48. return 0; //Failed to Connect
  49. }
  50. std::cout << "Connected!" << std::endl;
  51.  
  52. char buff[256];
  53. recv(Connection, buff, sizeof(buff), NULL); //Receive Message of the Day buffer into MOTD array
  54. std::cout << "Init Message form server:" << buff << std::endl;
  55.  
  56. std::cout << "Podaj liczby L1 i L2\n";
  57. std::cin >> buff; //getting L1
  58. send(Connection, buff, sizeof(buff), NULL); //sending L1
  59.  
  60. std::cin >> buff; //getting L2
  61. send(Connection, buff, sizeof(buff), NULL); //sending L2
  62.  
  63. recv(Connection, buff, sizeof(buff), NULL); //waiting for result
  64. std::cout << "Wynik: " << buff << std::endl;
  65. Sleep(2000);
  66. closesocket(Connection);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement