linux_ha 发表于 2008-1-31 12:35:30

我的客户端怎么recv_n返回0?

服务器端:

#include "ace/SOCK_Acceptor.h"#include "ace/SOCK_Stream.h"#include "ace/Log_Msg.h"#define SIZE_DATA 18#define SIZE_BUF 1024#define NO_ITERATIONS 5 class Server{public:Server (int port): server_addr_(port),peer_acceptor_(server_addr_){data_buf_= new char;data_return_="Ok, I have done!"
} //Handle the connection once it has been established. Here the//connection is handled by reading SIZE_DATA amount of data from the//remote and then closing the connection stream down.int handle_connection(){// Read data from client
for(int i=0;i<NO_ITERATIONS;i++){int byte_count=0;if( (byte_count=new_stream_.recv_n (data_buf_, SIZE_DATA, 0))==-1)ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));else{data_buf_=0;ACE_DEBUG((LM_DEBUG,"Server received %s \n",data_buf_));}}
// send data to current client
if (new_stream_.send_n (data_return_, ACE_OS::strlen(data_return_)+1) == -1)
            ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","send_n"),0);
// Close new endpointif (new_stream_.close () == -1)ACE_ERROR ((LM_ERROR, "%p\n", "close"));return 0;} //Use the acceptor component peer_acceptor_ to accept the connection//into the underlying stream new_stream_. After the connection has been//established call the handle_connection() method.int accept_connections (){if (peer_acceptor_.get_local_addr (server_addr_) == -1)ACE_ERROR_RETURN ((LM_ERROR,"%p\n","Error in get_local_addr"),1); ACE_DEBUG ((LM_DEBUG,"Starting server at port %d\n",server_addr_.get_port_number ())); // Performs the iterative server activities.while(1){ACE_Time_Value timeout (ACE_DEFAULT_TIMEOUT);if (peer_acceptor_.accept (new_stream_, &client_addr_, &timeout)== -1){ACE_ERROR ((LM_ERROR, "%p\n", "accept"));continue;}else{ACE_DEBUG((LM_DEBUG,"Connection established with remote %s:%d\n",client_addr_.get_host_name(),client_addr_.get_port_number()));//Handle the connectionhandle_connection();}}} private:char *data_buf_;char *data_return_;ACE_INET_Addr server_addr_;ACE_INET_Addr client_addr_;ACE_SOCK_Acceptor peer_acceptor_;ACE_SOCK_Stream new_stream_;}; int main (int argc, char *argv[]){
if(argc<2){ACE_ERROR((LM_ERROR,"Usage %s <port_num>", argv));ACE_OS::exit(1);}Server server(ACE_OS::atoi(argv));server.accept_connections();}
//客户端#define SIZE_BUF 128
#define NO_ITERATIONS 5
#define SIZE_DATA 18
class Client{public:Client(char *hostname, int port):remote_addr_(port,hostname){data_request_="Hello from Client";data_duf_=new char[ size_buf ];} //Uses a connector component `connector_’ to connect to a//remote machine and pass the connection into a stream//component client_stream_int connect_to_server()
{// Initiate blocking connection with server.ACE_DEBUG ((LM_DEBUG, "(%P|%t) Starting connect to %s:%d\n",remote_addr_.get_host_name(),remote_addr_.get_port_number()));if (connector_.connect (client_stream_, remote_addr_) == -1)ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","connection failed"),-1);elseACE_DEBUG ((LM_DEBUG,"(%P|%t) connected to %s\n",remote_addr_.get_host_name ()));return 0;} //Uses a stream component to send data to the remote host.int send_to_server(){// Send data to serverfor(int i=0;i<NO_ITERATIONS; i++){if (client_stream_.send_n (data_request_,ACE_OS::strlen(data_request_)+1, 0) == -1){ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","send_n"),0);break;}} //Close down the connection//close();}
//Uses a stream component to read data from the remote hostint read_from_server()
{
   int byte_count = 0;
   if ((byte_count = client_stream_.recv_n(data_buf_, SIZE_DATA)) == -1)
   {
             ACE_ERROR ((LM_ERROR, "%p\n", "Error in recv"));
   }
   else
   {
             data_buf_ = 0;
             ACE_DEBUG((LM_DEBUG,"Server received %s %d\n",data_buf_, ACE_OS::strlen(data_buf_)));
   }
   close();
}
 //Close down the connection properly.int close(){if (client_stream_.close () == -1)ACE_ERROR_RETURN ((LM_ERROR,"(%P|%t) %p\n","close"),-1);elsereturn 0;} private:ACE_SOCK_Stream client_stream_;ACE_INET_Addr remote_addr_;ACE_SOCK_Connector connector_;char *data_buf_;char *data_request_;}; int main (int argc, char *argv[]){if(argc<3){ACE_DEBUG((LM_DEBUG,”Usage %s <hostname> <port_number>\n”, argv));ACE_OS::exit(1);}Client client(argv,ACE_OS::atoi(argv));client.connect_to_server();client.send_to_server();client.reab_from_server();}

问题:为什么客户端recv_n接收时总是返回0呢?其他的还正常

chenxiaohui318 发表于 2008-3-18 15:43:41

试着在客户端一方断开SOCKet试一下。。。
我觉得不需要两方都进行断开。。

chenxiaohui318 发表于 2008-3-18 16:14:58

发送之前把BUF打印一下,看看数据有没有发送出去。。。
页: [1]
查看完整版本: 我的客户端怎么recv_n返回0?