今天我们就来看看如何简单的应用CSocket编程.当然了,你也可以用 CAsyncSocket 来实现,其实作用是一样的,只是CSocket是对SOCKET的更高级抽象.好了,废话不说了.你现在就可以像用SOCKET一样使用CSocket编程.
那就来看看下面的代码吧:(这里,我们用的是支持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;
}
int nFirst = 0;
do
{
CString strBuffer;
char buffer[1024] = {0};
if( nFirst == 0 )
{
cout << "there is a new connection arrived!" << endl;
nFirst = 1;
}
cin >> buffer ;
socket_client.Send( buffer, strlen(buffer) + 1, 0 );
strBuffer = buffer;
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
socket_client.Receive( buffer, 1024, 0 );
cout << buffer << endl;
strBuffer = buffer;
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;
}
do
{
CString strBuffer;
char buffer[1024] = {0};
socket_client.Receive( buffer, 1024, 0 );
cout << buffer << endl;
strBuffer = buffer;
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
cin >> buffer ;
socket_client.Send( buffer, strlen(buffer) + 1, 0 );
strBuffer = buffer;
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;
}
这样,就大功告成了哦.简单吧.呵呵.