Advertisement
Guest User

Untitled

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