|
楼主 |
发表于 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异常导致消息不确定 |
|