Advertisement
Guest User

sample

a guest
May 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <winsock2.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <stdlib.h>
  7.  
  8. #pragma comment(lib, "ws2_32.lib") //Winsock Library
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.     cout << "Connecting to port...";
  14.     unsigned short int s_port;
  15.     cout << "Enter server port: ";
  16.     cin >> s_port;
  17.     cout << "OK. Port: " << s_port << endl;
  18.    
  19.    
  20.     cout << "Initialization...";
  21.     WSADATA wsaData;
  22.     int res = WSAStartup(MAKEWORD(2,2), &wsaData);
  23.     if (res != NO_ERROR)
  24.         cout << "ERROR. " << WSAGetLastError() << endl;
  25.     cout << "OK. Initialized" << endl;
  26.    
  27.     cout << "Creating socket...";
  28.     SOCKET clientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  29.     if (clientSocket == INVALID_SOCKET) {
  30.         cout << "ERROR. Invalid socket!" << endl;
  31.         return 0;
  32.     }
  33.     cout << "OK. Socket created." << endl;
  34.    
  35.    
  36.    
  37.     cout << "Binding...";
  38.     struct sockaddr_in client;
  39.     client.sin_family = AF_INET;
  40.     client.sin_port = htons(s_port);
  41.     client.sin_addr.s_addr = inet_addr("10.0.1.3");
  42.     int clientSize = sizeof(client);
  43.    
  44.     if (bind(clientSocket, (struct sockaddr*) &client, clientSize) == SOCKET_ERROR) {
  45.         cout << "ERROR. Binding not completed!" << endl;
  46.     }
  47.     cout << "OK. Socket binded" << endl;
  48.    
  49.     const int part_size = 1024;
  50.     char *FilenameBuffer = new char[part_size];
  51.    
  52.     while (1) {
  53.         memset(FilenameBuffer, '\0', part_size);
  54.        
  55.         cout << "Trying to recieve data...";
  56.         int recv_len;
  57.         if ((recv_len = recvfrom(clientSocket, FilenameBuffer, part_size, 0, (struct sockaddr *) &client, &clientSize)) == SOCKET_ERROR) {
  58.             cout << "ERROR. Recvfrom() failed" << endl;
  59.             return 0;
  60.         }
  61.         cout << "New client connected!" << endl;
  62.         cout << "Received filename from " << inet_ntoa(client.sin_addr) << " : " << ntohs(client.sin_port) << endl;
  63.         cout << "Bytes Recv: " << recv_len << endl;
  64.         cout << "Recieved data (filename): " << FilenameBuffer << endl;
  65.        
  66.         cout << "transfer???????" << endl;
  67.        
  68.         struct stat si;
  69.         if (stat(FilenameBuffer, &si)) {
  70.             perror("stat");
  71.             if (sendto(clientSocket, "File not found", 15, 0, (struct sockaddr *)&client, clientSize) == SOCKET_ERROR) {
  72.                 cout << "ERROR. Sending error!" << endl;
  73.                 return 0;
  74.             }
  75.             return 0;
  76.         }
  77.        
  78.         cout << "Si: " << si.st_size << endl;
  79.         string tmp = stoi(si.st_size);
  80.         cout << "Si size: " << tmp.length() << endl; //????????
  81.        
  82.         char *buf = new char[temp.length()];
  83.         char tmp[part_size];
  84.         buf = itoa(si.st_size, tmp, 10);
  85.        
  86.        
  87.         int t = sendto(clientSocket, buf2, temp.length(), 0, (struct sockaddr *)&client, clientSize);
  88.         if (t == SOCKET_ERROR) {
  89.             cout << "ERROR. Sending file size error!" << endl;
  90.             return 0;
  91.         }
  92.         cout << "OK. " << inet_ntoa(client.sin_addr) << " send bytes " << t << " by " << t << endl;
  93.        
  94.         int parts_count = si.st_size / part_size;
  95.         int last_part_size = si.st_size % part_size;
  96.        
  97.         cout << "Parts_count: " << parts_count << endl << "Last_part_size: " << last_part_size << endl; //?????????
  98.        
  99.         FILE *file;
  100.        
  101.         if ((file=fropen(FilenameBuffer, "rb")) == NULL) {
  102.             cout << "ERROR. Open file error!" << endl;
  103.             if (sendto(clientSocket, "File not found", 15, 0, (struct sockaddr *)&client, clientSize) == SOCKET_ERROR) {
  104.                 cout << "ERROR. Sending error!" << endl;
  105.                 return 0;
  106.             }
  107.             return 0;
  108.         } else {
  109.             char *buffer = new char[part_size];
  110.            
  111.             for (int i = 0; i < parts_count; i++) {
  112.                 memset(buffer, '\0', part_size);
  113.                 if (fread(buffer, 1, part_size, file) != part_size) {
  114.                     cout << "ERROR. Fread error!" << endl;
  115.                     return 0;
  116.                 }
  117.                 if (sendto(clientSocket, buffer, part_size, 0, (struct sockaddr *) &client, clientSize) == SOCKET_ERROR) {
  118.                     cout << "ERROR. Sending file error!" << endl;
  119.                 }
  120.                 Sleep(1); //?????
  121.             }
  122.            
  123.             if (last_part_size) {
  124.                 if (fread(buffer, 1, last_part_size, file) != last_part_size) {
  125.                     cout << "ERROR. Fread error!" << endl;
  126.                     return 0;
  127.                 }
  128.                 if (sendto(clientSocket, buffer, last_part_size, 0, (struct sockaddr *) &client, clientSize) == SOCKET_ERROR) {
  129.                     cout << "ERROR. Fread error!" << endl;
  130.                     return 0;
  131.                 }
  132.             }
  133.             delete[] buffer;
  134.             fclose(f);
  135.         }
  136.         cout << "End of sending file." << endl;
  137.     }
  138.     closesocket(clientSocket);
  139.     WSACleanup();
  140.     cout << "Socket closed! End of program working." << endl;
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement