Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize sockaddr_in using addrinfo

Tags:

c++

sockets

void ClientCreate(struct addrinfo * addr, 
                  const char* port) {

  sockaddr_in service;

  service.sin_family = AF_INET;

  service.sin_addr.s_addr = inet_addr ;       /* What do I put here? */ 

  service.sin_port = (u_short)atoi(port); 

I don't have to use inet_addr() I know have tried many possibility nothing works.

Also on the port the MSDN example used htons() but the above works.

Is there a better way to do it?

like image 363
Santhosh Kumar Avatar asked Mar 29 '26 20:03

Santhosh Kumar


1 Answers

service.sin_addr.s_addr = inet_addr(What do I put here?);

It should be ip address of server,

//if server is localhost
service.sin_addr.s_addr = inet_addr("127.0.0.1");.

Also on the port the MSDN example used htons()

htons converts host byte order to network byte order.

like image 108
Pranit Kothari Avatar answered Apr 01 '26 07:04

Pranit Kothari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!