Advertisement
Guest User

Untitled

a guest
May 29th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. #include <windows.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<winsock2.h>
  5. #include <string.h>
  6.  
  7. /* This function is called by the Windows function DispatchMessage() */
  8. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  9. {
  10. switch (message) /* handle the messages */
  11. {
  12. case WM_DESTROY:
  13. PostQuitMessage (0); /* send a WM_QUIT to the message queue */
  14. break;
  15. default: /* for messages that we don't deal with */
  16. return DefWindowProc (hwnd, message, wParam, lParam);
  17. }
  18.  
  19. return 0;
  20. }
  21.  
  22. /* Make the class name into a global variable */
  23. char szClassName[ ] = "CodeBlocksWindowsApp";
  24.  
  25. int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nCmdShow)
  26. {
  27. WSADATA wsa;
  28. SOCKET s;
  29. struct sockaddr_in server;
  30. int bytesSent;
  31. int bytesRecv = SOCKET_ERROR;
  32. char sendbuf[1024] = "GET /projekt.html HTTP/1.1\r\n";
  33. char recvbuf[50000] = "";
  34.  
  35. // WSASTARTUP
  36. printf("\nInicjalizowanie Winsocka...\t");
  37. if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
  38. {
  39. printf("Blad o kodzie: %d\n",WSAGetLastError());
  40. return 1;
  41. }
  42. else
  43. printf("Zainicjalizowano.");
  44. //tworzenie socketu
  45. printf("\nTworzenie Socketu...\t");
  46. if((s = socket(AF_INET , SOCK_STREAM , IPPROTO_TCP )) == INVALID_SOCKET)
  47. {
  48. printf("Nie mozna bylo utworzyc socketu: %d\n" , WSAGetLastError());
  49. WSACleanup();
  50. }
  51. else
  52. printf("Socket utworzony.");
  53.  
  54. memset( &server, 0, sizeof( server ) );
  55. server.sin_addr.s_addr = inet_addr("127.0.0.1");
  56. server.sin_family = AF_INET;
  57. server.sin_port = htons(80);
  58.  
  59. //laczenie
  60. printf("\nLaczenie...\t");
  61. if (connect(s , (struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
  62. {
  63. printf("Blad polaczenia.");
  64. closesocket(s);
  65. WSACleanup();
  66. return 1;
  67. }
  68. //wysyla get
  69. printf("\nWysylanie.");
  70. bytesSent = send(s, sendbuf, strlen(sendbuf), 0);
  71. printf( "\nWyslano bajtow: %d\n", bytesSent );
  72.  
  73. while( bytesRecv == SOCKET_ERROR )
  74. {
  75. bytesRecv = recv( s, recvbuf, 50000, 0 );
  76.  
  77. if( bytesRecv == 0 || bytesRecv == WSAECONNRESET )
  78. {
  79. printf( "\nPolaczenie zakonczone." );
  80. break;
  81. }
  82.  
  83. if( bytesRecv < 0 )
  84. return 1;
  85.  
  86. printf( "\nOdebrano bajtow: %d", bytesRecv );
  87. printf( "\nOdebrany tekst: %s\n", recvbuf );
  88.  
  89. FILE *fp; /* używamy metody wysokopoziomowej - musimy mieć zatem identyfikator pliku, uwaga na gwiazdkę! */
  90.  
  91. if ((fp=fopen("strona.html", "w"))==NULL) {
  92. printf ("Nie mogę otworzyć pliku strona.html do zapisu!\n");
  93. exit(1);
  94. }
  95. fprintf (fp, "%s", recvbuf); /* zapisz nasz łańcuch w pliku */
  96. fclose (fp); /* zamknij plik */
  97. }
  98. //*********************************************************************************************************
  99. HWND hwnd; /* This is the handle for our window */
  100. MSG messages; /* Here messages to the application are saved */
  101. WNDCLASSEX wincl; /* Data structure for the windowclass */
  102.  
  103. /* The Window structure */
  104. wincl.hInstance = hThisInstance;
  105. wincl.lpszClassName = szClassName;
  106. wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
  107. wincl.style = CS_DBLCLKS; /* Catch double-clicks */
  108. wincl.cbSize = sizeof (WNDCLASSEX);
  109.  
  110. /* Use default icon and mouse-pointer */
  111. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  112. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  113. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  114. wincl.lpszMenuName = NULL; /* No menu */
  115. wincl.cbClsExtra = 0; /* No extra bytes after the window class */
  116. wincl.cbWndExtra = 0; /* structure or the window instance */
  117. /* Use Windows's default colour as the background of the window */
  118. wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  119.  
  120. /* Register the window class, and if it fails quit the program */
  121. if (!RegisterClassEx (&wincl))
  122. return 0;
  123.  
  124. /* The class is registered, let's create the program*/
  125. hwnd = CreateWindowEx (
  126. 0, /* Extended possibilites for variation */
  127. szClassName, /* Classname */
  128. "Chromolo", /* Title Text */
  129. WS_OVERLAPPEDWINDOW, /* default window */
  130. CW_USEDEFAULT, /* Windows decides the position */
  131. CW_USEDEFAULT, /* where the window ends up on the screen */
  132. 800, /* The programs width */
  133. 600, /* and height in pixels */
  134. HWND_DESKTOP, /* The window is a child-window to desktop */
  135. NULL, /* No menu */
  136. hThisInstance, /* Program Instance handler */
  137. NULL /* No Window Creation data */
  138. );
  139.  
  140. /* Make the window visible on the screen */
  141. ShowWindow (hwnd, nCmdShow);
  142. /* Run the message loop. It will run until GetMessage() returns 0 */
  143. while (GetMessage (&messages, NULL, 0, 0))
  144. {
  145. /* Translate virtual-key messages into character messages */
  146. TranslateMessage(&messages);
  147. /* Send message to WindowProcedure */
  148. DispatchMessage(&messages);
  149. closesocket(s);
  150. WSACleanup();
  151. }
  152.  
  153. /* The program return-value is 0 - The value that PostQuitMessage() gave */
  154. return messages.wParam;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement