找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 3097|回复: 4

这个ACE程序输出结果怎么会这样?

[复制链接]
发表于 2008-4-20 13:36:37 | 显示全部楼层 |阅读模式
从ACE程序员教程那里打出来的...发现和我预期的结果相差很大
服务器端:
#include "stdafx.h"
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/Log_Msg.h"
#include "ace/Time_Value.h"
#include <iostream>
using namespace std;
#define SIZE_DATA 18
#define SIZE_BUF  1024
#define NO_ITERATIONS 5
class server
{
private:
ACE_SOCK_Acceptor peer_acceptor_;
ACE_SOCK_Stream  new_stream_;
ACE_INET_Addr  server_addr_;
ACE_INET_Addr  client_addr_;
char*    data_buf_;
public:
server(int port) : server_addr_(port) , peer_acceptor_()
{
  data_buf_ = new char[SIZE_BUF];
}
~server()
{
  delete [] data_buf_;
}
int handle_connection()
{
  for (int i = 0; i < NO_ITERATIONS; ++i)
  {
   int bytes_count = 0;
   if ( (bytes_count = new_stream_.recv_n(data_buf_, SIZE_DATA, 0)) == -1)
   {
    ACE_ERROR((LM_ERROR, "%p\n", "Error in recv."));
   }
   else
   {
    data_buf_[bytes_count] = '\0';
    ACE_DEBUG((LM_DEBUG, "Server recieveed: %s.\n", data_buf_));
   }
  }
  if (new_stream_.close() == -1)
  {
   ACE_ERROR((LM_ERROR, "%p\n", "close error."));
  }
  return 0;
}
int accept_connections()
{
  if (peer_acceptor_.open(server_addr_) == -1)
   ACE_ERROR_RETURN((LM_ERROR, "%p\n", "open error"), -1);
  
  ACE_DEBUG((LM_DEBUG, "Starting server at: %d\n", server_addr_.get_port_number()));
  while (true)
  {
   ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT);
   if (peer_acceptor_.accept(new_stream_, &client_addr_/*, &timeout*/) == -1)
   {
    ACE_ERROR((LM_DEBUG, "%p\n", "accept error."));
    continue;
   }
   else
   {
    ACE_DEBUG((LM_DEBUG, "Connection established while remote %s:%d\n",
       client_addr_.get_host_name(),
       client_addr_.get_port_number()));
    handle_connection();
   }
  }
}
};
int _tmain(int argc, char* argv[])
{
server serv(8080);
serv.accept_connections();
return 0;
}
这里是客户端:
#include "stdafx.h"
#include "ace/Log_Msg.h"
#include "ace/SOCK_Connector.h"
//#include "ace/ACE_SOCK_Stream.h"
using namespace std;
#define BUF_SIZE 18
#define NO_ITERATIONS 5
class client
{
private:
ACE_SOCK_Connector connector_;
ACE_INET_Addr  remote_addr_;
ACE_SOCK_Stream  client_stream_;
char* data_buf_;
public:
client(char* hostname, int port) : remote_addr_(port, hostname)
{
  char* p = "Hello, ACE Server!";
  int n = strlen(p);
  data_buf_ = new char[BUF_SIZE];
  strncpy(data_buf_, p, n);
  //data_buf_[n] = '\0';
}
~client()
{
  delete [] data_buf_;
}
int connect_to_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) \n", "connection failed"), -1);
  }
  else
  {
   //怎么下面这里老是出错...有没有人知道啊?Server端这样做为什么没有错..
   //ACE_DEBUG((LM_DEBUG, "(%p | %t) connected to: %s:%d",
   //   remote_addr_.get_host_name(),
   //   remote_addr_.get_port_number()));
  }
  return 0;
}
int send_to_server()
{
  for (int i = 0; i < NO_ITERATIONS; ++i)
  {
   //把data_buf_改成"Hello, ACE Server!"结果就正常
   if (client_stream_.send_n(data_buf_, strlen(data_buf_) + 1, 0) == -1)
   {
    ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) ", "send error"), 0);
    break;
   }
  }
  close();
}
int close()
{
  if (client_stream_.close() == -1)
   ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) \n", "close error"), -1);
  else
   return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
client cl("localhost", 8080);
cl.connect_to_server();
cl.send_to_server();
return 0;
}
我想结果应该是:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed: Hello, ACE Server!.
Server recieveed: Hello, ACE Server!..
Server recieveed: Hello, ACE Server!..
Server recieveed: Hello, ACE Server!.
但得到的结果却是:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed: .
Server recieveed: rver!.
Server recieveed: CE Server!.
Server recieveed: lo, ACE Server!?
 楼主| 发表于 2008-4-20 13:37:15 | 显示全部楼层
以下是引用szwolf在2006-9-28 0:36:00的发言:
从ACE程序员教程那里打出来的...发现和我预期的结果相差很大
服务器端:
#include "stdafx.h"  
#include "ace/SOCK_Acceptor.h"
#include "ace/SOCK_Stream.h"
#include "ace/Log_Msg.h"
#include "ace/Time_Value.h"
#include <iostream>
using namespace std;
#define SIZE_DATA 18
#define SIZE_BUF  1024
#define NO_ITERATIONS 5
class server
{
private:
ACE_SOCK_Acceptor peer_acceptor_;
ACE_SOCK_Stream  new_stream_;
ACE_INET_Addr  server_addr_;
ACE_INET_Addr  client_addr_;
char*    data_buf_;
public:
server(int port) : server_addr_(port) , peer_acceptor_()
{
  data_buf_ = new char[SIZE_BUF];
}
~server()
{
  delete [] data_buf_;
}
int handle_connection()
{
  for (int i = 0; i < NO_ITERATIONS; ++i)
  {
   int bytes_count = 0;
   if ( (bytes_count = new_stream_.recv_n(data_buf_, SIZE_DATA, 0)) == -1)
   {
    ACE_ERROR((LM_ERROR, "%p\n", "Error in recv."));
   }
   else
   {
    data_buf_[bytes_count] = '\0';
    ACE_DEBUG((LM_DEBUG, "Server recieveed: %s.\n", data_buf_));
   }
  }
  if (new_stream_.close() == -1)
  {
   ACE_ERROR((LM_ERROR, "%p\n", "close error."));
  }
  return 0;
}
int accept_connections()
{
  if (peer_acceptor_.open(server_addr_) == -1)
   ACE_ERROR_RETURN((LM_ERROR, "%p\n", "open error"), -1);
  
  ACE_DEBUG((LM_DEBUG, "Starting server at: %d\n", server_addr_.get_port_number()));
  while (true)
  {
   ACE_Time_Value timeout(ACE_DEFAULT_TIMEOUT);
   if (peer_acceptor_.accept(new_stream_, &client_addr_/*, &timeout*/) == -1)
   {
    ACE_ERROR((LM_DEBUG, "%p\n", "accept error."));
    continue;
   }
   else
   {
    ACE_DEBUG((LM_DEBUG, "Connection established while remote %s:%d\n",
       client_addr_.get_host_name(),
       client_addr_.get_port_number()));
    handle_connection();
   }
  }
}
};
int _tmain(int argc, char* argv[])
{
server serv(8080);
serv.accept_connections();
return 0;
}
这里是客户端:
#include "stdafx.h"
#include "ace/Log_Msg.h"
#include "ace/SOCK_Connector.h"
//#include "ace/ACE_SOCK_Stream.h"
using namespace std;
#define BUF_SIZE 18
#define NO_ITERATIONS 5
class client
{
private:
ACE_SOCK_Connector connector_;
ACE_INET_Addr  remote_addr_;
ACE_SOCK_Stream  client_stream_;
char* data_buf_;
public:
client(char* hostname, int port) : remote_addr_(port, hostname)
{
  char* p = "Hello, ACE Server!";
  int n = strlen(p);
  data_buf_ = new char[BUF_SIZE +1];
  strncpy(data_buf_, p, n);
  data_buf_[n] = '\0';
}
~client()
{
  delete [] data_buf_;
}
int connect_to_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) \n", "connection failed"), -1);
  }
  else
  {
   //怎么下面这里老是出错...有没有人知道啊?Server端这样做为什么没有错..
   //ACE_DEBUG((LM_DEBUG, "(%p | %t) connected to: %s:%d",
   //   remote_addr_.get_host_name(),
   //   remote_addr_.get_port_number()));
  }
  return 0;
}
int send_to_server()
{
  for (int i = 0; i < NO_ITERATIONS; ++i)
  {
   //把data_buf_改成"Hello, ACE Server!"结果就正常
   //if (client_stream_.send_n(data_buf_, strlen(data_buf_) + 1, 0) == -1)
   if (client_stream_.send_n(data_buf_, strlen(data_buf_) , 0) == -1)
   {
    ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) ", "send error"), 0);
    break;
   }
  }
  close();
}
int close()
{
  if (client_stream_.close() == -1)
   ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) \n", "close error"), -1);
  else
   return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
client cl("localhost", 8080);
cl.connect_to_server();
cl.send_to_server();
return 0;
}
我想结果应该是:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed: Hello, ACE Server!.
Server recieveed: Hello, ACE Server!..
Server recieveed: Hello, ACE Server!..
Server recieveed: Hello, ACE Server!.
但得到的结果却是:
Starting server at: 8080
Connection established while remote happy-family:18556
Server recieveed: Hello, ACE Server!.
Server recieveed: .
Server recieveed: rver!.
Server recieveed: CE Server!.
Server recieveed: lo, ACE Server!?


发送的消息string长度为18,拷贝到18个字节的sendbuff中不带结束符,send_n中strlen异常导致消息不确定
 楼主| 发表于 2008-4-20 13:37:36 | 显示全部楼层
//if (client_stream_.send_n(data_buf_, strlen(data_buf_) + 1, 0) == -1)
if (client_stream_.send_n(data_buf_, strlen(data_buf_) , 0) == -1)
我试了一下用你下面那段代码strlen(data_buf_) 就可以,但是用strlen(data_buf_) + 1就不可以,为什么会这样,不是应该把最后的'\0'也给发送过去么?
 楼主| 发表于 2008-4-20 13:37:59 | 显示全部楼层
我用string类重定义了data_buf_,效果也是一样的.
#include "ace/Log_Msg.h"
#include "ace/SOCK_Connector.h"
#include <string>
//#include "ace/ACE_SOCK_Stream.h"
using namespace std;
#define BUF_SIZE 18
#define NO_ITERATIONS 5
class client
{
private:
ACE_SOCK_Connector connector_;
ACE_INET_Addr remote_addr_;
ACE_SOCK_Stream client_stream_;
string data_buf_;
public:
client(char* hostname, int port) : remote_addr_(port, hostname)
{
data_buf_ = "hello! ACE Server";
data_buf_ += '\0';
}
~client()
{

}
int close()
{
if (client_stream_.close() == -1)
ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) \n", "close error"), -1);
else
return 0;
return 0;
}
int connect_to_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) \n", "connection failed"), -1);
}
else
{
//怎么下面这里老是出错...有没有人知道啊?Server端这样做为什么没有错..
//ACE_DEBUG((LM_DEBUG, "(%p | %t) connected to: %s:%d",
// remote_addr_.get_host_name(),
// remote_addr_.get_port_number()));
}
return 0;
}
int send_to_server()
{
for (int i = 0; i < NO_ITERATIONS; ++i)
{
//把data_buf_改成"Hello, ACE Server!"结果就正常
//if (client_stream_.send_n(data_buf_, strlen(data_buf_) + 1, 0) == -1)
if (client_stream_.send_n(data_buf_.c_str(), data_buf_.size() , 0) == -1)
{
ACE_ERROR_RETURN((LM_ERROR, "(%p | %t) ", "send error"), 0);
break;
}
}
close();
}

};
int main(int argc, char* argv[])
{
client cl("localhost", 8080);
cl.connect_to_server();
cl.send_to_server();
return 0;
}
 楼主| 发表于 2008-4-20 13:38:49 | 显示全部楼层
以下是引用szwolf在2006-9-28 13:34:00的发言:

谢谢: )
//if (client_stream_.send_n(data_buf_, strlen(data_buf_) + 1, 0) == -1)
if (client_stream_.send_n(data_buf_, strlen(data_buf_) , 0) == -1)
我试了一下用你下面那段代码strlen(data_buf_) 就可以,但是用strlen(data_buf_) + 1就不可以,为什么会这样,不是应该把最后的'\0'也给发送过去么?

接收端接收的缓冲是18,每次接收18个字节,使用strlen(data_buf_) + 1就变成一次发送19个字节了,超出的一个\0会作为下次消息接收
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-23 13:41 , Processed in 0.019250 second(s), 7 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表