Aslai

Untitled

Feb 11th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #define _WIN32
  2. #ifdef _WIN32
  3.     #include <windows.h>
  4.     #include <winsock2.h>
  5.     #include <ws2tcpip.h>
  6.     #include<sys/time.h>
  7.     void mysleep(unsigned long t){ Sleep(t); }
  8. #else
  9.     #include <sys/types.h>
  10.     #include <sys/socket.h>
  11.     #include <netinet/in.h>
  12.     #include <netdb.h>
  13.     #include <unistd.h>
  14.     #include <termios.h>
  15.     #include <errno.h>
  16.     #include <signal.h>
  17.     #define SOCKET int
  18.     void mysleep(unsigned long t){ usleep(t*1000); }
  19. #endif
  20. #include<stdio.h>
  21. #include<stdlib.h>
  22.  
  23. int main(int argc, char*argv[]){
  24.     if( argc == 1 ){
  25.         printf("Please drag a file to upload on to the application" );
  26.         mysleep(3000);
  27.         return 1;
  28.     }
  29.     #ifdef _WIN32
  30.     WSADATA globalWSAData;
  31.     WSAStartup( MAKEWORD(2, 2), &globalWSAData );
  32.     #endif
  33.     srand(time(0));
  34.     int port = rand()%(65536-500)+500;
  35.     char ip[80];
  36.     gethostname(ip, sizeof(ip));
  37.     printf("On your iDevice, connect to:\thttp://%s:%i/\n\n", ip, port );
  38.  
  39.     SOCKET m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  40.     if (m_socket < 0) {
  41.             printf("Failed to create interface" ); return 0;
  42.     }
  43.  
  44.     sockaddr_in service;
  45.     service.sin_family = AF_INET;
  46.     service.sin_addr.s_addr = INADDR_ANY;
  47.     service.sin_port = htons( port );
  48.     if (bind(m_socket,(sockaddr*) (&service), sizeof(service)) < 0) {
  49.         closesocket(m_socket);
  50.          printf("Failed to create interface" ); return 0;
  51.         return 0;
  52.     }
  53.     listen( m_socket, 10 );
  54.  
  55.     FILE* f = fopen( argv[1], "rb" );
  56.     if( f < 0 ) return 1;
  57.     size_t len = 0;
  58.     while( fgetc( f ) != EOF ){len++;}
  59.     fseek( f, 0, SEEK_SET );
  60.  
  61.     int l = strlen( argv[1] );
  62.     for( ; l > 0 && (argv[1][l]!='\\'&&argv[1][l]!='/');--l ){}
  63.     if( l != 0 ) ++l;
  64.  
  65.     while( true ){
  66.         SOCKET sock = accept( m_socket, 0, 0 );
  67.         if( sock > 0 ){
  68.             char* fmt =
  69. "HTTP/1.1 200 OK\r\n\
  70. Content-Description: File Transfer\r\n\
  71. Content-Disposition: filename=%s\r\n\
  72. Content-Type: application/octet-stream\r\n\
  73. Content-Transfer-Encoding: binary\r\n\
  74. Content-Length: %i\r\n\r\n";
  75.  
  76.             int writelen = snprintf( 0, 0, fmt, argv[1]+l, len );
  77.             char* towrite = (char*) malloc( writelen + 1 );
  78.             snprintf( towrite, writelen + 1, fmt, argv[1]+l, len );
  79.             char* sending = towrite;
  80.             while( writelen > 0 ){
  81.                 int tmp = send( sock, sending, writelen, 0 );
  82.                 writelen -= tmp;
  83.                 sending += tmp;
  84.             }
  85.             size_t sent=0;
  86.             size_t percent = 0;
  87.             while( !feof(f) ){
  88.                 if( percent != 100*sent/len ){
  89.                     percent = 100*sent/len;
  90.                     printf("\r%03i%% [", 100*sent/len);
  91.                     for( int i = 0; i < 50; ++i ){
  92.                         printf("%c", 50*sent/len>=i?'=':' ');
  93.                     }
  94.                     printf("]");
  95.                 }
  96.                 char buffer[1000];
  97.                 int bufs = fread( buffer, 1, 1000, f );
  98.                 sending = buffer;
  99.                 while( bufs > 0 ){
  100.                     int tmp = send( sock, sending, bufs, 0 );
  101.                     sending += tmp;
  102.                     bufs -= tmp;
  103.                     sent += tmp;
  104.                 }
  105.             }
  106.             printf("Successfully sent file\n");
  107.             mysleep(3000);
  108.             return 0;
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment