Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. /* 1. Initialize Winsock.
  2.    2. Create a socket.
  3.    3. Connect to the server.
  4.    4. Send and receive data.
  5.    5. Disconnect.
  6. */
  7.  
  8. //GLITCH
  9.  
  10. #ifndef WIN32_LEAN_AND_MEAN
  11. #define WIN32_LEAN_AND_MEAN
  12.  
  13. #define C_USER "USER %srn"
  14. #define C_PASS "PASS %srn"
  15. #define C_STAT "STATrn"
  16. #endif
  17.  
  18. #include <winsock2.h>
  19. #include <ws2tcpip.h>
  20. #include <iphlpapi.h>
  21. #include <stdio.h>
  22. #include <iostream>
  23. #include <stdlib.h>
  24. #include <conio.h>
  25. #include <errno.h>
  26.  
  27. #pragma comment(lib, "Ws2_32.lib")
  28.  
  29. using namespace std;
  30.  
  31.  
  32. int main() {
  33.     //instalacja winsocka
  34.     WSADATA wsaData;
  35.  
  36. int iResult;
  37.  
  38. // Initialize Winsock
  39. iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
  40. if (iResult != 0) {
  41.     printf("dupa1 %d\n", iResult);
  42.     return 1;
  43. }
  44.  
  45. struct addrinfo *result = NULL,
  46.                 *ptr = NULL,
  47.                 hints;
  48.  
  49. ZeroMemory( &hints, sizeof(hints) );
  50. hints.ai_family = AF_UNSPEC;
  51. hints.ai_socktype = SOCK_STREAM;
  52. hints.ai_protocol = IPPROTO_TCP;
  53.  
  54. #define DEFAULT_PORT "110"
  55.  
  56. iResult = getaddrinfo("o2.pl",DEFAULT_PORT,&hints,&result);
  57. if (iResult != 0) {
  58.     printf("dupa socket nie polaczyl sie z wesola poczta %d\n", iResult); // co robi 3 argument funkcji getaddrinfo??
  59.     WSACleanup();
  60.     return 1;
  61. }
  62.  
  63. //tworzymy socketa odbierajacego i wysylajacego dane
  64. SOCKET ConnectSocket = INVALID_SOCKET;
  65. ptr=result;
  66. ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
  67.     ptr->ai_protocol);
  68. if (ConnectSocket == INVALID_SOCKET) {
  69.     printf("dupa3 socket odbierajacy cos z poczty nie dziala: %ld\n", WSAGetLastError());
  70.     freeaddrinfo(result);
  71.     WSACleanup();
  72.     return 1;
  73. }
  74.  
  75. //laczenie sie z serwerem
  76. iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
  77. if (iResult == SOCKET_ERROR) {
  78.     closesocket(ConnectSocket);
  79.     ConnectSocket = INVALID_SOCKET;
  80. }
  81.  
  82. freeaddrinfo(result);
  83. //spr czy sie polaczyl
  84. if (ConnectSocket == INVALID_SOCKET) {
  85.     printf(" dupa Unable to connect to server!\n");
  86.     WSACleanup();
  87.     return 1;
  88. }
  89.  
  90. // wyslanie loginu
  91.  
  92. char sendbuf[0x64]  = {0};
  93. char recvbuf[0x64]={0};
  94.                 sprintf(sendbuf, C_USER, "poczta_sieci@o2.pl");
  95.                 iResult = send(ConnectSocket, sendbuf, strlen(sendbuf), 0);
  96.                 iResult = recv(ConnectSocket, recvbuf,512, 0);
  97.  
  98.                 if(recvbuf[0]!='+')
  99.                     printf("Blad\n");
  100. // wyslanie hasla
  101.  
  102.                 sprintf(sendbuf, C_PASS, "pocztasieci");
  103.                 iResult = send(ConnectSocket, sendbuf, strlen(sendbuf),0);
  104.                 iResult = recv(ConnectSocket, recvbuf,512, 0);
  105.  
  106.                 if(recvbuf[0]!='+')
  107.                     printf("Blad\n");
  108.  
  109.                 else
  110.                 printf("Polaczono z poczta!\n");
  111.  
  112.  
  113. system("pause");
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement