socket 编程入门教程(一)TCP server 端:8、本章的完整源代码

// Filename: TcpServerClass.hpp

#ifndef TCPSERVERCLASS_HPP_INCLUDED
#define  TCPSERVERCLASS_HPP_INCLUDED

#include 
< unistd.h >
#include 
< iostream >
#include 
< sys / socket.h >
#include 
< arpa / inet.h >

class  TcpServer
{
private :
    
int  listenSock;
    
int  communicationSock;
    sockaddr_in servAddr;
    sockaddr_in clntAddr;
public :
    TcpServer(
int  listen_port);
    
bool  isAccept();
    
void  handleEcho();
};


#endif   //  TCPSERVERCLASS_HPP_INCLUDED

// Filename: TcpServerClass.cpp

#include 
" TcpServerClass.hpp "

TcpServer::TcpServer(
int  listen_port)
{
    
if  ( (listenSock  =  socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))  <   0  ) {
        
throw   " socket() failed " ;
    }

    memset(
& servAddr,  0 sizeof (servAddr));
    servAddr.sin_family 
=  AF_INET;
    servAddr.sin_addr.s_addr 
=  htonl(INADDR_ANY);
    servAddr.sin_port 
=  htons(listen_port);

    
if  ( bind(listenSock, (sockaddr * ) & servAddr,  sizeof (servAddr))  <   0  ) {
        
throw   " bind() failed " ;
    }

    
if  ( listen(listenSock,  10 <   0  ) {
        
throw   " listen() failed " ;
    }
}

bool  TcpServer::isAccept()
{
    unsigned 
int  clntAddrLen  =   sizeof (clntAddr);

    
if  ( (communicationSock  =  accept(listenSock, (sockaddr * ) & clntAddr,  & clntAddrLen))  <   0  ) {
        
return   false ;
    } 
else  {
        std::cout 
<<   " Client(IP:  "   <<  inet_ntoa(clntAddr.sin_addr)  <<   " ) connected.\n " ;
        
return   true ;
    }
}

void  TcpServer::handleEcho()
{
    
const   int  BUFFERSIZE  =   32 ;
    
char  buffer[BUFFERSIZE];
    
int  recvMsgSize;
    
bool  goon  =   true ;

    
while  ( goon  ==   true  ) {
        
if  ( (recvMsgSize  =  recv(communicationSock, buffer, BUFFERSIZE,  0 ))  <   0  ) {
            
throw   " recv() failed " ;
        } 
else   if  ( recvMsgSize  ==   0  ) {
            goon 
=   false ;
        } 
else  {
            
if  ( send(communicationSock, buffer, recvMsgSize,  0 !=  recvMsgSize ) {
                
throw   " send() failed " ;
            }
        }
    }

    close(communicationSock);
}

演示程序:
// Filename: main.cpp
// Tcp Server C++ style, single work

#include 
< iostream >
#include 
" TcpServerClass.hpp "

int  echo_server( int  argc,  char *  argv[]);

int  main( int  argc,  char *  argv[])
{
    
int  mainRtn  =   0 ;
    
try  {
        mainRtn 
=  echo_server(argc, argv);
    }
    
catch  (  const   char *  s ) {
        perror(s);
        exit(EXIT_FAILURE);
    }

    
return  mainRtn;
}

int  echo_server( int  argc,  char *  argv[])
{
    
int  port;
    
if  ( argc  ==   2  ) {
        port 
=  atoi(argv[ 1 ]);
    } 
else  {
        port 
=   5000 ;
    }

    TcpServer myServ(port);

    
while  (  true  ) {
        
if  ( myServ.isAccept()  ==   true  ) {
            myServ.handleEcho();
        }
    }

    
return   0 ;
}


posted on 2008-07-16 12:57  lf426 阅读(3074)  评 论(1)   编辑  收藏  引用 所属分类:  SDL入门教 程 、 Linux与 C++ 、 socket 编程入门教程
socket 编程入门教程(一)TCP server 端:8、本章的完整源代码 - 资料收集 - 资料收集博客  
FeedBack:
#  re: Linux socket 编程入门(一)TCP server 端:8、本章的完整源代码  2009-10-31 11:09  Austin
不能编译通过ubuntu server 9.04
GCC 4.33

TcpServerClass.o: In function `__static_initialization_and_destruction_0(int, int)':
TcpServerClass.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
TcpServerClass.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
TcpServerClass.o: In function `TcpServer::handleEcho()':
TcpServerClass.cpp:(.text+0xbd): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0xd7): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0xdf): undefined reference to `__cxa_throw'
TcpServerClass.cpp:(.text+0x125): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x13f): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x147): undefined reference to `__cxa_throw'
TcpServerClass.o: In function `TcpServer::isAccept()':
TcpServerClass.cpp:(.text+0x1da): undefined reference to `std::cout'
TcpServerClass.cpp:(.text+0x1df): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
TcpServerClass.cpp:(.text+0x1eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
TcpServerClass.cpp:(.text+0x1fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
TcpServerClass.o: In function `TcpServer::TcpServer(int)':
TcpServerClass.cpp:(.text+0x24b): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x265): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x26d): undefined reference to `__cxa_throw'
TcpServerClass.cpp:(.text+0x2f3): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x30d): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x315): undefined reference to `__cxa_throw'
TcpServerClass.cpp:(.text+0x33d): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x357): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x35f): undefined reference to `__cxa_throw'
TcpServerClass.o: In function `TcpServer::TcpServer(int)':
TcpServerClass.cpp:(.text+0x3a3): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x3bd): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x3c5): undefined reference to `__cxa_throw'
TcpServerClass.cpp:(.text+0x44b): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x465): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x46d): undefined reference to `__cxa_throw'
TcpServerClass.cpp:(.text+0x495): undefined reference to `__cxa_allocate_exception'
TcpServerClass.cpp:(.text+0x4af): undefined reference to `typeinfo for char const*'
TcpServerClass.cpp:(.text+0x4b7): undefined reference to `__cxa_throw'
TcpServerClass.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
main.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
main.o: In function `main':
main.cpp:(.text+0x11d): undefined reference to `__cxa_begin_catch'
main.cpp:(.text+0x148): undefined reference to `__cxa_end_catch'
main.o:(.gcc_except_table+0x18): undefined reference to `typeinfo for char const*'
main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [main] Error 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值