[转]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
今天,我们不用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 <<"创建套接字失败!错误原因:" <
}
if( !socket_server.Listen() )
{
cerr <<"监听失败!错误原因:" <
}
CSocket socket_client;
if( !socket_server.Accept(socket_client) )
{
cerr <<"等待客户端的连接失败!错误原因:" <
}
if ( socket_client == INVALID_SOCKET )
{
cerr <<"无效的客户端套接字!错误原因:" <
}
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!" <
}
cout <<"Send :" ;cin >>tmp_buffer;
strBuffer = tmp_buffer ;
arOut <
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
arIn >>strBuffer ;
arIn.Flush();
cout <<"Recv :" <<(LPCTSTR)strBuffer <
if ( strBuffer == "QUIT") break;
} while( TRUE );
cout <<"断开与客户端的连接!" <
}
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") <
}
else
{
if ( !AfxSocketInit(NULL) )
{
cerr <<_T("初始化套接字失败!") <
}
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("创建套接字失败!错误原因:") <
}
if( !socket_client.Connect( "127.0.0.1", 4443 ) )
{
cerr <<_T("连接服务器失败!错误原因:") <
}
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 <
if ( strBuffer == "QUIT") break;
cout <<"Send :" ; cin >>tmp_buffer ;
strBuffer = tmp_buffer;
arOut <
strBuffer.MakeUpper();
if ( strBuffer == "QUIT") break;
} while( TRUE );
cout <<"断开与服务器的连接!" <
}
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") <
}
else
{
if ( !AfxSocketInit(NULL) )
{
cerr <<_T("初始化套接字失败!") <
}
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
- [转]INTERNET编程之CSOCKET编程续 - 章志强 - CSDNBlog
- [转]INTERNET编程之CSOCKET编程 - 章志强 - CSDNBlog
- INTERNET编程之CSOCKET编程续
- INTERNET编程之CSOCKET编程
- INTERNET编程之CSOCKET编程实现UDP连接
- 转 MFC CSocket编程
- INTERNET编程之SOCKET编程
- 转:深入 CSocket 编程之阻塞和非阻塞模式
- INTERNET编程之INTERNET简单使用HTTP
- MFC下CSocket编程详解 (转)
- CSocket类编程模型
- 转帖 MFC CSocket编程
- MFC下CSocket编程
- CSocket网络编程基础
- 基于CSocket编程
- MFC下CSocket编程
- CSocket类编程模型
- INTERNET编程之SOCKET编程补充
- 获取数据库信息
- [转]CSocket 和CAsyncSocket类介绍
- 交往, 帮助他人, 知识积累, 心理素质, 大胆去做
- 取表中 某累记录的最大5行
- 函数指针&回调函数&linux中的signal函数
- [转]INTERNET编程之CSOCKET编程续 - 章志强 - CSDNBlog
- 如何得到字符的UNICODE编码
- SQL语句性能调整原则
- 购买数码相机前最需要知道的参数
- Windows下NTFS无损转换FAT32格式
- JSP Tag
- Visual C#程序设计技巧小结
- 关于2表删除的问题
- Manning. JUnit in Action 文摘-2