Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WIN32
- #ifdef _WIN32
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include<sys/time.h>
- void mysleep(unsigned long t){ Sleep(t); }
- #else
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <termios.h>
- #include <errno.h>
- #include <signal.h>
- #define SOCKET int
- void mysleep(unsigned long t){ usleep(t*1000); }
- #endif
- #include<stdio.h>
- #include<stdlib.h>
- int main(int argc, char*argv[]){
- if( argc == 1 ){
- printf("Please drag a file to upload on to the application" );
- mysleep(3000);
- return 1;
- }
- #ifdef _WIN32
- WSADATA globalWSAData;
- WSAStartup( MAKEWORD(2, 2), &globalWSAData );
- #endif
- srand(time(0));
- int port = rand()%(65536-500)+500;
- char ip[80];
- gethostname(ip, sizeof(ip));
- printf("On your iDevice, connect to:\thttp://%s:%i/\n\n", ip, port );
- SOCKET m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (m_socket < 0) {
- printf("Failed to create interface" ); return 0;
- }
- sockaddr_in service;
- service.sin_family = AF_INET;
- service.sin_addr.s_addr = INADDR_ANY;
- service.sin_port = htons( port );
- if (bind(m_socket,(sockaddr*) (&service), sizeof(service)) < 0) {
- closesocket(m_socket);
- printf("Failed to create interface" ); return 0;
- return 0;
- }
- listen( m_socket, 10 );
- FILE* f = fopen( argv[1], "rb" );
- if( f < 0 ) return 1;
- size_t len = 0;
- while( fgetc( f ) != EOF ){len++;}
- fseek( f, 0, SEEK_SET );
- int l = strlen( argv[1] );
- for( ; l > 0 && (argv[1][l]!='\\'&&argv[1][l]!='/');--l ){}
- if( l != 0 ) ++l;
- while( true ){
- SOCKET sock = accept( m_socket, 0, 0 );
- if( sock > 0 ){
- char* fmt =
- "HTTP/1.1 200 OK\r\n\
- Content-Description: File Transfer\r\n\
- Content-Disposition: filename=%s\r\n\
- Content-Type: application/octet-stream\r\n\
- Content-Transfer-Encoding: binary\r\n\
- Content-Length: %i\r\n\r\n";
- int writelen = snprintf( 0, 0, fmt, argv[1]+l, len );
- char* towrite = (char*) malloc( writelen + 1 );
- snprintf( towrite, writelen + 1, fmt, argv[1]+l, len );
- char* sending = towrite;
- while( writelen > 0 ){
- int tmp = send( sock, sending, writelen, 0 );
- writelen -= tmp;
- sending += tmp;
- }
- size_t sent=0;
- size_t percent = 0;
- while( !feof(f) ){
- if( percent != 100*sent/len ){
- percent = 100*sent/len;
- printf("\r%03i%% [", 100*sent/len);
- for( int i = 0; i < 50; ++i ){
- printf("%c", 50*sent/len>=i?'=':' ');
- }
- printf("]");
- }
- char buffer[1000];
- int bufs = fread( buffer, 1, 1000, f );
- sending = buffer;
- while( bufs > 0 ){
- int tmp = send( sock, sending, bufs, 0 );
- sending += tmp;
- bufs -= tmp;
- sent += tmp;
- }
- }
- printf("Successfully sent file\n");
- mysleep(3000);
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment