-6

I'm working with sockets, but when I compile my program I get some errors.

This is my code:

address.sin_family = AF_INET;
address.sin_port = htons(string); // here I get an error
inet_aton(str.c_str(),&address.sin_addr);

What I get is:

cannot convert ‘__gnu_cxx::__alloc_traits > >::value_type {aka std::__cxx11::basic_string}’ to ‘uint16_t {aka short unsigned int}’ for argument ‘1’ to ‘uint16_t htons(uint16_t)’

How can I solve this error?

Thanks in advance.

orlow65
  • 23
  • 5
  • 2
    The function `htons` says it takes a `uint16_t`, and you are passing it `string`. What is unclear about that? – Cory Kramer Dec 29 '15 at 18:08
  • @CoryKramer how can I make a conversion? – orlow65 Dec 29 '15 at 18:09
  • 1
    could use `std::stoi`, or a `stringstream` would probably be better because you're looking for a specific number of bits and unsigned – Ryan Haining Dec 29 '15 at 18:10
  • @RyanHaining I didn't found nothing on Internet about that :(. If you make an answer with an example, I can +1 it! – orlow65 Dec 29 '15 at 18:11
  • 2
    @orlow65 Really? [Like this](https://stackoverflow.com/questions/7663709/convert-string-to-int-c) or [this](https://stackoverflow.com/questions/194465/how-to-parse-a-string-to-an-int-in-c)? – Cory Kramer Dec 29 '15 at 18:12
  • @CoryKramer I want to use a simple stream. `std::stoi` isn't better. – orlow65 Dec 29 '15 at 18:13
  • 1
    @orlow65 There are literally 7 examples of using streams to convert a string to an int if you bothered looking at the posts I linked. – Cory Kramer Dec 29 '15 at 18:14

2 Answers2

1

You need to convert your std::string to a std::uint16_t. I'd recommend a stringstream

std::istringstream ss(string); // maybe pick a different name
std::uint16_t port{};
ss >> port;
address.sin_port = htons(port);

Be sure to #include <sstream>

Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • I get: `error: no match for ‘operator>>’ (operand types are ‘std::ostringstream {aka std::__cxx11::basic_ostringstream}’ and ‘uint16_t {aka short unsigned int}’)` – orlow65 Dec 29 '15 at 18:40
  • Change `ostringstream` to `istringstream`. – Ilya Popov Dec 29 '15 at 18:57
0

htons requires an uint16_t
which means you have to pass the port as integer, not as string

I3ck
  • 433
  • 3
  • 10
  • `integer` type doesn't exists in C++. And `int` != `uint` – orlow65 Dec 29 '15 at 18:10
  • 3
    @orlow65 - `integer` informally means _"some integral type"_, such as uint16_t, uint32_t, int64_t, etc. etc. Don't be pedantic when you're asking a very basic question, it's not a good look. – Useless Dec 29 '15 at 18:14
  • 3
    @orlow65 I even stated uint16_6 the line before. Excuse me for not typing it again 9 words later – I3ck Dec 29 '15 at 18:17
  • @Useless his answer is misinterpretable. An `integer` formally is relevant an `int`, but `integer` doesn't exist as a type. `uint16_t` is not an integer, but an unsigned 16-bit integer. – orlow65 Dec 29 '15 at 18:25
  • 1
    uint16_t is an unsigned 16 bit integer, which still is an integer – I3ck Dec 29 '15 at 18:31
  • @l3ck not technically. – orlow65 Dec 29 '15 at 19:18
  • how not? it's a generalisation – I3ck Dec 29 '15 at 19:21
  • "Don't be pedantic" - how can you *possibly* not be pedantic, and be a programmer? Pedantry is what programming is about! – Martin Bonner supports Monica Dec 29 '15 at 21:08
  • 1
    @MartinBonner - Hah, perhaps! I feel this pedantry is misplaced though, and surely avoiding redundant repetition is also desirable? – Useless Dec 30 '15 at 00:30
  • 1
    @orlow65 - the answer is fine, so long as you understand `integer` is not a type, but a _kind of type_, which contains all of `{char, short, int, etc.}`, and their unsigned variants, and implicitly their bit-size-specific typedefs. See, for example [std::is_integral](http://en.cppreference.com/w/cpp/types/is_integral). Since the answer already specified `uint16_t` once, and you can anyway read this in the documentation, I don't see what you're complaining about. – Useless Dec 30 '15 at 00:34