[转]INTERNET编程之CSOCKET编程续 - 章志强 - CSDNBlog

来源:互联网 发布:招聘网络推广专员 编辑:程序博客网 时间:2024/06/05 11:22
导读:
  今天,我们不用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 <<"创建套接字失败!错误原因:" <  return FALSE;
  }
  if( !socket_server.Listen() )
  {
  cerr <<"监听失败!错误原因:" <  return FALSE;
  }
  CSocket socket_client;
  if( !socket_server.Accept(socket_client) )
  {
  cerr <<"等待客户端的连接失败!错误原因:" <  return FALSE;
  }
  if ( socket_client == INVALID_SOCKET )
  {
  cerr <<"无效的客户端套接字!错误原因:" <  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!" <  nFirst = 1;
  }
  cout <<"Send :" ;cin >>tmp_buffer;
  strBuffer = tmp_buffer ;
  arOut <  arOut.Flush();
  strBuffer.MakeUpper();
  if ( strBuffer == "QUIT") break;
  
  
  arIn >>strBuffer ;
  arIn.Flush();
  cout <<"Recv :" <<(LPCTSTR)strBuffer <  strBuffer.MakeUpper();
  if ( strBuffer == "QUIT") break;
  
  } while( TRUE );
  cout <<"断开与客户端的连接!" <  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") <  nRetCode = 1;
  }
  else
  {
  if ( !AfxSocketInit(NULL) )
  {
  cerr <<_T("初始化套接字失败!") <  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("创建套接字失败!错误原因:") <  return FALSE;
  }
  if( !socket_client.Connect( "127.0.0.1", 4443 ) )
  {
  cerr <<_T("连接服务器失败!错误原因:") <  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 <  strBuffer.MakeUpper();
  if ( strBuffer == "QUIT") break;
  
  cout <<"Send :" ; cin >>tmp_buffer ;
  strBuffer = tmp_buffer;
  arOut <  arOut.Flush();
  strBuffer.MakeUpper();
  if ( strBuffer == "QUIT") break;
  } while( TRUE );
  cout <<"断开与服务器的连接!" <  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") <  nRetCode = 1;
  }
  else
  {
  if ( !AfxSocketInit(NULL) )
  {
  cerr <<_T("初始化套接字失败!") <  nRetCode = 1;
  }
  else
  {
  if( !CreateClient() )
  {
  nRetCode = 1;
  }
  }
  }
  return nRetCode;
  }
  好了,大工告成了.很简单吧.不过很实用的哦!!!呵呵.
  Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2149997

本文转自
http://blog.csdn.net/beyond_q/archive/2008/03/05/2149997.aspx
原创粉丝点击