Guest User

Untitled

a guest
Apr 22nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. // TestProjectClient1.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4.  
  5.  
  6.  
  7.  
  8. int wmain()
  9. {
  10.     //----------------------
  11.     // Initialize Winsock
  12.     WSADATA wsaData;
  13.     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  14.     if (iResult != NO_ERROR) {
  15.         wprintf(L"WSAStartup function failed with error: %d\n", iResult);
  16.         return 1;
  17.     }
  18.     //----------------------
  19.     // Create a SOCKET for connecting to server
  20.     SOCKET ConnectSocket;
  21.     ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  22.     if (ConnectSocket == INVALID_SOCKET) {
  23.         wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
  24.         WSACleanup();
  25.         return 1;
  26.     }
  27.     //----------------------
  28.     // The sockaddr_in structure specifies the address family,
  29.     // IP address, and port of the server to be connected to.
  30.     sockaddr_in clientService;
  31.     clientService.sin_family = AF_INET;
  32.     clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
  33.     clientService.sin_port = htons(28015);
  34.  
  35.     //----------------------
  36.     // Connect to server.
  37.     iResult = connect(ConnectSocket, (SOCKADDR *) & clientService, sizeof (clientService));
  38.     if (iResult == SOCKET_ERROR) {
  39.         wprintf(L"connect function failed with error: %ld\n", WSAGetLastError());
  40.         iResult = closesocket(ConnectSocket);
  41.         if (iResult == SOCKET_ERROR)
  42.             wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
  43.         WSACleanup();
  44.         return 1;
  45.     }
  46.  
  47.     wprintf(L"Connected to server.\n");
  48.    
  49.    
  50.     // send request for wallpaper
  51.     //
  52.     //
  53.     Request r;
  54.     r.s = XLARGE;
  55.     r.cat = animale | peisaje | masini ; // Animale si Masini si Peisaje
  56.    
  57.     send(ConnectSocket, (char*)(&r), sizeof(r),0);
  58.  
  59.     // recive wallpaper
  60.     //
  61.     //
  62.     long lSize = 256;
  63.     char *buffer = new char[256];
  64.     FILE *f = fopen("1.jpg","wb");
  65.  
  66.     while(true)
  67.     {
  68.         lSize = recv(ConnectSocket, buffer, lSize,0);
  69.         if( lSize <= 0 ) break;
  70.         fwrite(buffer,1,lSize,f);
  71.     }
  72.    
  73.     fclose(f);
  74.     delete buffer;
  75.    
  76.     //close socket
  77.     iResult = closesocket(ConnectSocket);
  78.     if (iResult == SOCKET_ERROR) {
  79.         wprintf(L"closesocket function failed with error: %ld\n", WSAGetLastError());
  80.         WSACleanup();
  81.         return 1;
  82.     }
  83.  
  84.     WSACleanup();
  85.     return 0;
  86. }
Add Comment
Please, Sign In to add comment