///////////////////////////////////////////////////////////////////// // sockFiles.cpp - demonstrate sending files over sockets // // connection // // ver 1.0 // // Language: Visual C++, ver 6.0, SP 5 // // Platform: Dell Dimension 8100, Win 2000 Pro, SP 2 // // Application: CSE691/791 - SWM&A Demonstration // // Author: Jim Fawcett, CST 2-187, Syracuse Univ // // (315) 443-3948, jfawcett@twcny.rr.com // ///////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////// // Build Process // ///////////////////////////////////////////////////////////////////// // Required Files: // // sockFiles.cpp, socks.h, socks.cpp, sysUtils.h, sysUtils.cpp // // // // Compiler: // // - create a console project sockFile // // - add files the files listed above // - Project Settings\debug\Program Arguments\ filename // // - Project Settings\C/C++\category=code generation // // \Use run-time library\Debug Multithreaded // // - Project Settings\Link\Object/library modules: // // add wsock32.lib to end of list with preceding space // // - start the socket server created with socks test stub // // - compile, link, and run or: // // msdev sockFiles.dsp /make /rebuild // // debug\sockFiles sockFiles.cpp // // // ///////////////////////////////////////////////////////////////////// /* Maintenance History =================== ver 1.0 : 23 Oct 2001 - first release */ #include "socks.h" // #include from socks.h must #include // come before #include -- sigh #include #include #include "sysUtils.h" using namespace std; #define BLOCKSIZE 256 int main(int argc, char *argv[]) { cout << "\n Demonstrating sending files over socket connection " << "\n ====================================================\n\n"; if(argc == 1) { cout << "\n please enter name of file to open on command line\n\n"; return 1; } char *fileName = argv[1]; // // attempt to open file named in first argument of the command line HANDLE fileHandle = CreateFile( fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL ); sysError err; if(fileHandle == INVALID_HANDLE_VALUE) { cout << "\n " << err.getLastMsg() << endl; return 1; } // open connection to server socket sockClt toServer; if(!toServer.connectTo("127.0.0.1")) { cout << "\n can't open socket connection\n\n"; return 1; } // read file in fixed size blocks and write to socket unsigned long int numRead; const int bufferSize = BLOCKSIZE; char buffer[bufferSize]; BOOL readOK = TRUE; while(1) { readOK = ReadFile(fileHandle,buffer,bufferSize,&numRead,NULL); if(readOK && numRead > 0) { buffer[numRead] = '\0'; cout << "\n sending " << numRead << " bytes"; toServer << buffer; } else break; } CloseHandle(fileHandle); cout << "\n\n"; return 0; }