hereson 发表于 2013-1-27 05:21:44

Dev-cpp下的一个socket的例子

这个例子很简单,服务端是单线程的,一般来说实际应用中多采用多线程方式监听客户请求的。
先列客户端的代码:Client.h

#ifndef _SERVERSOCKET_H
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#define _SERVERSOCKET_H
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#i nclude <sys/types.h>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif//! 下面是根据操作系统自动加载所需的库和头文件
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#ifdef   WIN32   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif          #i nclude   <winsock2.h>   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif          #pragma   comment(lib,"ws2_32.lib")   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#else   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif          #i nclude   <sys/socket.h>   
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#endif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#i nclude <string>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#i nclude <iostream>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifint WSAStartup
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif( 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    WORD wVersionRequested, 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif    LPWSADATA lpWSAData 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing namespace std; 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifclass CClient
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    int                 m_sockfd;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    struct sockaddr_in  m_addr;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifpublic:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    CClient(const string& strAddr, int iPort);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    ~CClient();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    BOOL Connect(); 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}; 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#endif
定义文件Client.cpp
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif#i nclude "Client.h" 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifCClient::CClient(const string& strAddr, int iPort)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    WSADATA   wsaData;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    int err = WSAStartup( MAKEWORD(2, 2), &wsaData ); 
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif    if (err == SOCKET_ERROR)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif        cout << "WSAStartup error" << endl; 
页: [1]
查看完整版本: Dev-cpp下的一个socket的例子