从本期开校到现在,主要的精力花在了evc的学习,运用上。这里对学习的情况做一个总结。
(以在一个工程中实现的功能模块为序)
在一个名叫Orange的工程中,实现的功能模块有:
①用Tcp协议,在客户端和服务器端实现图片的传输。
②在客户端的显示设备上,显示从服务器端传来的图片,并显示。
③实现一个启动动画。
(下面详细总结)
①用Tcp协议,在客户端和服务器端实现图片的传输。
首先,需要知道Tcp协议在工作的时候,客户端和服务器端都要做哪些动作来完成通信的功能。
(以函数的形式说明他们的过程)
a.Server:socket()-->bind()-->listen()-->accept()-->recv()-->send-->recv()-->closesocket().
b.Client: socket()------------------------->connect()-->send()-->recv()------------>closesocket() 。
然后,需要知道对文件的操作。(因为传输的是图片,也是一种文件)
(这里不做相信说明,百度一下)。
最后,由于文件我传输的文件一般都大于4k,所以我选择了在服务器端用循环的方式读,在客户端再用
循环的方式写文件。相关函数如下:
/*-----------------------------------------------------------------
【函数介绍】: 从服务器端向客户端发送数据,按照约定pts2[]有4个值,故不指定它的长度
【入口参数】: buf:待发送的数据
len:待发送的数据长度
fileName:待发送的图片文件的名字
pts2[]:待发送的坐标值
【出口参数】: (无)
【返回 值】: TRUE:发送数据成功;FALSE:发送数据失败
------------------------------------------------------------------*/
bool CTCPCustom_CE::SendData(CString fileName,POINT pts2[])
{
// used to monitor the progress of a sending operation
int fileLength, cbLeftToSend;
// pointer to buffer for sending data
// (memory is allocated after sending file size)
BYTE* sendData = NULL;
CFile sourceFile;
CFileException fe;
BOOL bFileIsOpen = FALSE;
if( !( bFileIsOpen = sourceFile.Open(fileName,//_T("//Temp//mapxiao.bmp"),
CFile::modeRead | CFile::typeBinary, &fe ) ) )
{
/* you should handle the error here */
TCHAR strCause[256];
fe.GetErrorMessage( strCause, 255 );
if(fe.m_cause==CFileException::fileNotFound)
AfxMessageBox(_T("File not found"),MB_YESNO|MB_ICONSTOP);
return FALSE;
}
// first send length of file
fileLength = sourceFile.GetLength();
fileLength = htonl( fileLength );
cbLeftToSend = sizeof( fileLength );
do
{
int cbBytesSent;
char* bp = (char*)(&fileLength) + sizeof(fileLength) - cbLeftToSend;
cbBytesSent = send(m_socket,bp,cbLeftToSend,0);
// test for errors and get out if they occurred
if ( cbBytesSent == SOCKET_ERROR )
{
int iErrorCode = WSAGetLastError();
//触发socket的Error事件
m_pTCPServer_CE->OnClientError(m_pTCPServer_CE->m_pOwnerWnd,this,iErrorCode);
//触发与服务器端断开连接事件
m_pTCPServer_CE->OnClientClose(m_pTCPServer_CE->m_pOwnerWnd,this);
//关闭socket
Close();
return FALSE;
}
// data was successfully sent, so account
// for it with already-sent data
cbLeftToSend -= cbBytesSent;
}
while ( cbLeftToSend>0 );
// now send the file's data
sendData = new BYTE[1024];
cbLeftToSend = sourceFile.GetLength();
do
{
// read next chunk of SEND_BUFFER_SIZE bytes from file
int sendThisTime, doneSoFar,buffOffset;
sendThisTime = sourceFile.Read( sendData, 1024 );
buffOffset = 0;
do
{
char* bp=(char*)(sendData+buffOffset);
doneSoFar=send(m_socket,bp,sendThisTime,0);
// test for errors and get out if they occurred
if ( doneSoFar == SOCKET_ERROR )
{
int iErrorCode = WSAGetLastError();
//触发socket的Error事件
m_pTCPServer_CE->OnClientError(m_pTCPServer_CE->m_pOwnerWnd,this,iErrorCode);
//触发与服务器端断开连接事件
m_pTCPServer_CE->OnClientClose(m_pTCPServer_CE->m_pOwnerWnd,this);
//关闭socket
Close();
return FALSE;
}
// data was successfully sent,
// so account for it with already-sent data
buffOffset += doneSoFar;
sendThisTime -= doneSoFar;
cbLeftToSend -= doneSoFa