//透過TCP實現:#define PORT 34000void SendFile(LPCTSTR strFilename) //引數為傳送檔案路徑及檔名{ AfxSocketInit(NULL); CSocket sockSrvr; sockSrvr.Create(PORT); // Creates our server socket sockSrvr.Listen(); // Start listening for the client at PORT CSocket sockRecv; sockSrvr.Accept(sockRecv); // Use another CSocket to accept the connection CFile myFile; if(!myFile.Open(strFilename, CFile::modeRead | CFile::typeBinary)) return; int myFileLength = myFile.GetLength(); // Going to send the correct File Size sockRecv.Send(&myFileLength, 4); // 4 bytes long byte* data = new byte[myFileLength]; myFile.Read(data, myFileLength); //1024 int iRet ; iRet = sockRecv.Send(data, myFileLength); //Send the whole thing now cout
//透過TCP實現:#define PORT 34000void SendFile(LPCTSTR strFilename) //引數為傳送檔案路徑及檔名{ AfxSocketInit(NULL); CSocket sockSrvr; sockSrvr.Create(PORT); // Creates our server socket sockSrvr.Listen(); // Start listening for the client at PORT CSocket sockRecv; sockSrvr.Accept(sockRecv); // Use another CSocket to accept the connection CFile myFile; if(!myFile.Open(strFilename, CFile::modeRead | CFile::typeBinary)) return; int myFileLength = myFile.GetLength(); // Going to send the correct File Size sockRecv.Send(&myFileLength, 4); // 4 bytes long byte* data = new byte[myFileLength]; myFile.Read(data, myFileLength); //1024 int iRet ; iRet = sockRecv.Send(data, myFileLength); //Send the whole thing now cout