簡體   English   中英

結構問題和int到char *的轉換

[英]Structure issue and int to char* conversion

在我的第一個程序中,我找到一個本地IP地址(int iip),我想發送的是該IP地址以及結構(Ins c)中的端口(int port = 1100)。 然后,我希望第二個程序接收此結構,並選擇IP地址和端口,以便以后在TCP連接中使用。

n_addr inaddr;
inaddr.s_addr = htonl(iip);

    int udp_socket_info;
    struct sockaddr_in udp_server;

    udp_socket_info = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_socket_info == -1) {
    puts("Could not create udp socket");
    }
    puts("Udp socket created");

    udp_server.sin_addr.s_addr = inet_addr("225.0.0.37"); 
    udp_server.sin_port = htons(1100);
    udp_server.sin_family = AF_INET;

    int port = 1100;


 Ins c(iip, port);        
 sendto(udp_socket_info, &c, sizeof(c), 0, (struct sockaddr *)&udp_server, sizeof(udp_server));
puts("STRUCT Message Sent");

在一個程序中,我收到以下結構:

Ins c;
if (recvfrom(udp_socket_info, &c, sizeof(c), 0, &addr, &fromlen) < 0) {
perror("Recvfrom error:");
}
puts("STRUCT Message Received");

tcp_ip = c.address;
tcp_port = c.port;

在頭文件MonReqServer.hh中,我將tcp_ip和tcp_port聲明為char *,因為這是設置tcp套接字所需要的。

 private:
   char* tcp_ip;
   char* tcp_port;

以下是Ins.hh的標頭

namespace Pds {
class Ins
  {
  public:
    Ins();
    enum Option {DoNotInitialize};
    explicit Ins(Option);
    explicit Ins(unsigned short port);
    explicit Ins(int address);
    Ins(int address, unsigned short port);
    Ins(const Ins& ins, unsigned short port);
    explicit Ins(const sockaddr_in& sa);

    int                   address()     const; 
    void                  address(int address);
    unsigned short        portId()      const; 

    int operator==(const Ins& that)  const;

  protected:
    int      _address;
    unsigned _port;
  };
}

這些是我的錯誤:

MonReqServer.cc: In member function 'virtual void Pds::MonReqServer::routine()':
MonReqServer.cc:79: error: cannot resolve overloaded function 'address' based on conversion to type 'char*'
MonReqServer.cc:80: error: 'class Pds::Ins' has no member named 'port'

那么我的問題是,一旦收到地址,我該如何將其轉換為char *? 具體來說,從int切換為char *的命令是什么,我該怎么暗含它? 還為什么說Ins沒有名為port的成員? 我以為Ins應該使用一個IP地址和一個端口? 如果我的問題不清楚或需要其他信息,請告訴我。 謝謝!

首先,危險危險!

char* tcp_ip;
char* tcp_port;

這些指針需要指向某個事物,並且它必須具有某種持久性。 我認為這種情況不太可能發生,因此您應該在此處定義存儲。

char tcp_ip[SizeOfBiggestIPAddressPossible];
char tcp_port[SizeOfBiggestPortNumberPossible];

哪里

#define     SizeOfBiggestIPAddressPossible (4+1+4+1+4+1+4+1+4+1+4+1+4+1+4+1)
#define     SizeOfBiggestPortNumberPossible (5+1)

但是,這只是愚蠢的。 由於OP標記為C ++,所以我們只使用幾個字符串即可,從而避免了緩沖區溢出和其他怪異的麻煩。

std::string tcp_ip;
std::string tcp_port;

接下來,將數字更改為字符串在這里毫無價值。 字符串編號適用於端口號...

tcp_port = std::to_string(c.portId());

或者如果缺少C ++ 11支持,請創建以下函數

template <class TYPE>
std::string toString(TYPE val)
{
    std::stringstream temp;
    temp << val;
    return  temp.str();
}

叫它

tcp_port = toString(c.portId());

但是將數字轉換為IP地址完全是另一回事。 IP地址是帶點或冒號與數字混合的格式化字符串。 如果有一個很好的C ++方法可以做到這一點,我不知道。

在* nux land中,您調用inet_ntop

假設Ipv4(未經編譯和測試,但應接近):

char temp[INET_ADDRSTRLEN];
struct sockaddr_in sockAddr;
sockAddr.sin_addr.S_un.S_addr = htonl(c.address());
if (inet_ntop(AF_INET, &addr, temp, INET_ADDRSTRLEN)!= null)
{
    tcp_ip = temp;
}
else
{
    std::cerr << "Bad address" << std::endl;
    // handle error
}

在Windows中,您可以調用相同的函數,也可以調用整個Windows專用替代方案。

現在,您有兩個不錯的,禮貌的C ++字符串,可以為您完成所有的內存管理工作。 要獲取C樣式字符串(const char *),請使用c_str方法。

const char * addrp = c.address().c_str();

如果您需要一個非const char *,那么值得一巴掌。

編輯:錯過了明顯的IP4唯一的黑客

這將使您獲得字符串形式的地址,但不支持將來。 或者說IPv6過去已有35年了?

uint32_t temp = c.address(); 
std::stringstream addr;
addr << ((temp >> 24) & 0xFF) << '.' <<
        ((temp >> 16) & 0xFF) << '.' <<
        ((temp >>  8) & 0xFF) << '.' <<
        ((temp >>  0) & 0xFF);
tcp_ip = addr.str();

首先,您的成員稱為_port和_address,而不是端口和地址。 它們不是公共的,因此可以使用函數portId()和address()獲得它們。其次,要將數字轉換為c字符串,可以使用sprintf或itoa函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM