www.pudn.com > MpegDShow.rar > MySocket.cpp


// MySocket.cpp: implementation of the CMySocket class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "MpegImage.h" 
#include "MySocket.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
#define BUFSIZE 1024 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CMySocket::CMySocket() 
{ 
 
} 
 
 
CMySocket::~CMySocket() 
{	 
	CMySocket::UnInit(); 
} 
 
 
bool CMySocket::Init() 
{ 
	if(!AfxSocketInit()) 
			return false; 
 
	m_Socket = socket(AF_INET, SOCK_STREAM, 0); 
	if(m_Socket == INVALID_SOCKET) 
			return false; 
 
	if(InitConfig() == false) 
		return false; 
 
	if(ConnectServer(m_strServerIP, m_iServerPort) == false) 
		return false; 
	 
	return true; 
} 
 
 
void CMySocket::UnInit() 
{ 
	closesocket(m_Socket); 
} 
 
 
bool CMySocket::ConnectServer(CString strServerIP, int iServerPort) 
{ 
	LPCTSTR	lpszAddrSock; 
	lpszAddrSock = (LPCTSTR)strServerIP; 
	unsigned long longAddrSocket = inet_addr(lpszAddrSock); 
	m_AddrSocket.sin_family = AF_INET; 
	m_AddrSocket.sin_port = htons(iServerPort); 
	memcpy(&m_AddrSocket.sin_addr, &longAddrSocket,  
		sizeof(longAddrSocket)); 
	if(connect(m_Socket, (LPSOCKADDR)&m_AddrSocket, sizeof(m_AddrSocket))  
		== SOCKET_ERROR) 
			return false; 
 
	return true; 
} 
 
 
bool CMySocket::SendFile(CString strFilePath) 
{ 
	CFile sendfile; 
	if(!(sendfile.Open(strFilePath, CFile::modeRead | CFile::typeBinary))) 
		return false; 
 
	if(CMySocket::SendFileInfo(sendfile) == false) 
		return false; 
 
	if(CMySocket::SendFileBuf(sendfile) == false) 
		return false; 
	 
	sendfile.Close(); 
 
	return true; 
} 
 
 
bool CMySocket::SendFileInfo(const CFile& sendfile) 
{ 
	FILEINFO sendfileinfo; 
	sendfileinfo.iFileLength = sendfile.GetLength(); 
	int error; 
	error = send(m_Socket, (char *)&sendfileinfo, sizeof(sendfileinfo), 0); 
	if((error == SOCKET_ERROR) || (error == 0)) 
		return false;	 
 
	return true; 
} 
 
 
bool CMySocket::SendFileBuf(CFile& sendfile) 
{ 
	char* chBuf = new char[BUFSIZE]; 
	int iTempNum = 0;	 
	int iTotalNum = 0; 
	int iFileLength= sendfile.GetLength(); 
	int iError = 0; 
	 
	while(1) 
		{ 
			iTempNum = sendfile.Read(chBuf, BUFSIZE); 
			if(iTempNum == 0)break; 
			iError = send(m_Socket, chBuf, iTempNum, 0); 
			if((iError == SOCKET_ERROR) || (iError == 0)) 
				{ 
					delete []chBuf; 
					return false; 
				} 
			iTotalNum += iTempNum; 
			if(iTotalNum >= iFileLength) 
				break; 
		} 
	 
	delete []chBuf; 
	return true; 
} 
 
 
CString CMySocket::ReadTxt(CString strFileName) 
{ 
	CStdioFile csfTxtFile; 
	CString    strFileString = ""; 
	if(csfTxtFile.Open(strFileName, CFile::modeRead)) 
	{ 
		csfTxtFile.ReadString(strFileString); 
		csfTxtFile.Close(); 
	} 
	return strFileString; 
} 
 
 
CString CMySocket::GetCurrentPath() 
{ 
	TCHAR tchCurrentPath[MAX_PATH] = {0}; 
	GetModuleFileName(NULL, tchCurrentPath, MAX_PATH); 
 
	CString strCurrentPath = ""; 
	strCurrentPath.Format("%s", tchCurrentPath); 
	return strCurrentPath; 
} 
 
 
bool CMySocket::InitConfig() 
{ 
	CString strCurrentPath = CMySocket::GetCurrentPath(); 
	if(strCurrentPath == "") 
		return false; 
 
	strCurrentPath += "Server.txt"; 
	CString strFileString = CMySocket::ReadTxt(strCurrentPath); 
	if(strFileString == "") 
		return false; 
 
	CString strTemp = ""; 
	int iStringPos = strFileString.Find('*'); 
	strTemp = strFileString.Left(iStringPos); 
	m_iServerPort = atoi(strTemp.GetBuffer(0)); 
	strTemp.ReleaseBuffer(); 
	 
	int iStringLength = strFileString.GetLength(); 
	strFileString = strFileString.Right(iStringLength - (iStringPos + 1)); 
	iStringPos = strFileString.Find('*'); 
	strTemp = strFileString.Left(iStringPos); 
	m_strServerIP = strTemp; 
	return true; 
}