nixp.ru v3.0

29 марта 2024,
пятница,
00:53:23 MSK

Longobard написал 25 апреля 2004 года в 16:59 (879 просмотров) Ведет себя как мужчина; открыл 291 тему в форуме, оставил 2499 комментариев на сайте.

Помогите!

Есть класс connection

вот его описание:

#ifndef CONNECTION_H
  #define CONNECTION_H
  #include "autoheader.h"
using std::string;
/* Класс connection для обработки подключений. В него включены дескрипторы сокета и ip клиента.
Реализация в connection.cpp */
struct traffic_count
{
  long double input;
  long double output;
  char in_type;
  char out_type;
};
class connection
{
private:
  int connfd;
  string client_ip;
  struct traffic_count traf;
  bool count_in_traf;
  bool count_out_traf;
public:
  connection( int cofd, const char* IP, bool count_in = false, bool count_out = false);
  ~connection();
  void f_write(void * ptr, size_t nbytes );
  ssize_t f_read(void * ptr, size_t nbytes);
  struct traffic_count GetTraffic (void) const;
  string GetClientIP (void);
};
#define TT_BYTE 0x1
#define TT_KBYTE 0x2
#define TT_MBYTE 0x3
#define TT_GBYTE 0x4
#define TT_TBYTE 0x5
#endif

Вот реализация:

#include "autoheader.h"
#include "unp.h"
#include "connection.h"
#include 
void connection::f_write( void * ptr, size_t nbytes ) {
      if ( write( connfd, ptr, nbytes ) != ( ssize_t )nbytes )
            err_msg( "Write error!" );
      if ( count_out_traf ) {
            traf.output += ( long int ) nbytes;
            if ( traf.output > ( 1024 * pow ( traf.output, traf.out_type ) ) && traf.out_type <= 0x5 ) {
                  traf.out_type++;
                  traf.out_type /= 1024;
            }
      }
}
ssize_t connection::f_read( void * ptr, size_t nbytes ) {
      ssize_t n;
      if ( ( n = read( connfd, ptr, nbytes ) ) == -1 )
            err_sys( "Read error" );
      if ( count_in_traf ) {
            traf.input += ( long int ) n;
            if ( traf.input > ( 1024 * pow ( traf.input, traf.in_type ) ) && traf.in_type <= 0x5 ) {
                  traf.in_type++;
                  traf.input /= 1024;
            }
      }
      return  n ;
}
connection::connection( int fd , const char * IP, bool count_in , bool count_out ) {
      string client_ip = IP;
      count_in_traf = count_in;
      count_out_traf = count_out;
      connfd = fd;
      client_ip.erase ( client_ip.rfind( "." ), client_ip.length() );
      err_msg( "Connection from %s", client_ip.c_str() );
      if ( count_in_traf || count_out_traf )
            memset ( & traf, 0, sizeof ( traf ) );
}
connection::~connection ( void ) {
      Close ( connfd );
}
string
connection::GetClientIP ( void ) const {
      return client_ip;
}

А вот его использование:

char buffer[512];
      connection client (connfd,IP);
      ssize_t readed = client.f_read (&buffer, sizeof (buffer));
      buffer[readed] = '\0';
      http_request request ( buffer );
      string ip = client.GetClientIP();
      err_msg ("at 36 in servr_main.cpp client ip = %s",ip.c_str());

Здесь функция err_msg пишет в лог. В результате в лог пишется IP из конструктора (пишется без проблем), а из main`a в лог пишется пустая строка. То есть в логе такая ситуция:

Connection from: 127.0.0.1

IP:

в чем дело? Почему client_ip в конструкторе инициализирован, а функция GetClientIP возвращает пустую строку?!!!??!! Надуеюсь на вашу квалифицированную помощь. Заранее сенькс.

Longobard

Нашел. Ошибка тупей некуда :)

string client_ip = IP;

меняем на

client_ip = IP;

И все работает :) Короче тред можно закрывать :)

Последние комментарии

ecobeingecobeing.ru
Экология и вегетарианство на благо всем живым существам Планеты.