INTERNET编程之CSOCKET编程续

今天,我们不用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;
}


好了,大工告成了.很简单吧.不过很实用的哦!!!呵呵.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值