今天,我們不用Send和Receive來實現通信,我們改用CSocketFile 和 CArchive 來實現.
當然了,這裏還是用支持MFC的Win32工程:
首先,是服務器端編程:(我建議大家也要注意一下編程風格問題,這對於寫大項目是有幫助的.)
// CSocketServer.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "CSocketServer.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
BOOL CreateServer()
{
CSocket socket_server;
if( !socket_server.Create(4443) )
{
cerr << "創建套接字失敗!錯誤原因:" << GetLastError() << endl;
return FALSE;
}
if( !socket_server.Listen() )
{
cerr << "監聽失敗!錯誤原因:" << GetLastError() << endl;
return FALSE;
}
CSocket socket_client;
if( !socket_server.Accept(socket_client) )
{
cerr << "等待客戶端的連接失敗!錯誤原因:" << GetLastError() << endl;
return FALSE;
}
if ( socket_client == INVALID_SOCKET )
{
cerr << "無效的客戶端套接字!錯誤原因:" << GetLastError() << endl;
return FALSE;
}
CSocketFile file(&socket_client);
CArchive arIn(&file, CArchive::load);
CArchive arOut(&file, CArchive::store);
int nFirst = 0;
do
{
CString strBuffer;
char tmp_buffer[1024] = {0};
if( nFirst == 0 )
{
cout << "there is a new connection arrived!" << endl;
nFirst = 1;
}
cout << "Send :" ;cin >> tmp_buffer;
strBuffer = tmp_buffer ;
arOut << strBuffer ;
arOut.Flush();
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
arIn >> strBuffer ;
arIn.Flush();
cout << "Recv :" << (LPCTSTR)strBuffer << endl;
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
} while( TRUE );
cout << "斷開與客戶端的連接!" << endl;
return TRUE;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
if ( !AfxSocketInit(NULL) )
{
cerr << _T("初始化套接字失敗!") << endl;
nRetCode = 1;
}
else
{
if( !CreateServer() )
{
nRetCode = 1;
}
}
}
return nRetCode;
}
然後,就是客戶端了:
// CSocketClient.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "CSocketClient.h"
#include "afxsock.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
BOOL CreateClient()
{
CSocket socket_client;
if( !socket_client.Create() )
{
cerr << _T("創建套接字失敗!錯誤原因:") << GetLastError() << endl;
return FALSE;
}
if( !socket_client.Connect( "127.0.0.1", 4443 ) )
{
cerr << _T("連接服務器失敗!錯誤原因:") << GetLastError() << endl;
return FALSE;
}
CSocketFile file(&socket_client);
CArchive arIn(&file, CArchive::load);
CArchive arOut(&file, CArchive::store);
do
{
CString strBuffer;
char tmp_buffer[1024] = {0};
arIn >> strBuffer ;
arIn.Flush();
cout << "recv : " << (LPCTSTR)strBuffer << endl;
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
cout << "Send :" ; cin >> tmp_buffer ;
strBuffer = tmp_buffer;
arOut << strBuffer ;
arOut.Flush();
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
} while( TRUE );
cout << "斷開與服務器的連接!" << endl;
return TRUE;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
if ( !AfxSocketInit(NULL) )
{
cerr << _T("初始化套接字失敗!") << endl;
nRetCode = 1;
}
else
{
if( !CreateClient() )
{
nRetCode = 1;
}
}
}
return nRetCode;
}
好了,大工告成了.很簡單吧.不過很實用的哦!!!呵呵.