⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connection.cpp

📁 ace开发环境 用来开发网络程序 其运用了设计模式、多平台、C++等多种知识
💻 CPP
字号:
// $Id: connection.cpp 78674 2007-06-30 05:15:07Z sowayaa $#include "connection.h"ACE_RCSID(stress_testing, connection, "$Id: connection.cpp 78674 2007-06-30 05:15:07Z sowayaa $")// Make the connection to the WEB serverint connection::connect(char *hostname_opt_port, int tcp_nodelay, int sockbufsiz) {  if(!hostname_opt_port) return 1;    char *hostname_with_port;  // Check to see if portnumber is specified in the hostnameport  // If not, append :80  if(!ACE_OS::strchr(hostname_opt_port,':')) {	  hostname_with_port = new char[ACE_OS::strlen(hostname_opt_port) + 3];	  ACE_OS::sprintf(hostname_with_port, "%s:%d", hostname_opt_port, 80);  }  else {    hostname_with_port = hostname_opt_port;  }	  // Beyond this point, hostname_with_port is of the form hostname:port  ACE_INET_Addr server_addr(hostname_with_port);    // Connect to server  ACE_SOCK_Connector con;  if(con.connect(stream_, server_addr) == -1) {    ACE_OS::perror("ACE_SOCK_Connector::connect");    return 1;  }    // tcp_nodelay processing.  // turn off weird ack things  if(tcp_nodelay) {	  struct protoent *p = ACE_OS::getprotobyname ("tcp");    int one = 1;        if (p && stream_.set_option (p->p_proto,				 TCP_NODELAY,				 (char *)& one, 				 sizeof (one)))     {      ACE_OS::perror("tcp_nodelay");      return 1;    }  }    if(sockbufsiz)     if (stream_.set_option (SOL_SOCKET,			    SO_RCVBUF,			    (char *) &sockbufsiz,			    sizeof sockbufsiz) == -1)     {      ACE_OS::perror("socket_queue_size");      return 1;    }    return 0;}int connection::read(void *buffer, size_t maxlen, unsigned int timeout_seconds) {  ACE_UNUSED_ARG (timeout_seconds);  return stream_.recv(buffer, maxlen);}int connection::write(const void *buffer, size_t maxlen, unsigned int timeout_seconds) {  ACE_UNUSED_ARG (timeout_seconds);  return stream_.send(buffer, maxlen);}int connection::write_n(const void *buffer, size_t len, unsigned int timeout_seconds) {  ACE_UNUSED_ARG (timeout_seconds);  if(stream_.send_n(buffer, len) == -1)    ACE_ERROR_RETURN((LM_ERROR, "Write failed for %s", buffer),1);  return 0;}int connection::read_n(void *buffer, size_t maxlen, unsigned int timeout_seconds) {  ACE_UNUSED_ARG (timeout_seconds);  if(stream_.recv_n(buffer, maxlen) == -1)    ACE_ERROR_RETURN((LM_ERROR, "Read failed.."),1);  return 1;}int connection::close(void) {  stream_.close_reader();  stream_.close_writer();  stream_.close();  return 0;}  connection::~connection(void) {  this->close();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -