| |

VerySource

 Forgot password?
 Register
Search
View: 720|Reply: 4

How to get the IP address and port saved in the structure socakaddr_in?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-9 22:00:01
| Show all posts |Read mode
That's it! !!
Reply

Use magic Report

0

Threads

10

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-11 10:18:01
| Show all posts
struct sockaddr {
Unsigned short sa_family; / * address family, AF_xxx * /
Char sa_data [14]; / ​​* 14-byte protocol address * /};
Sa_family is generally AF_INET; sa_data contains the IP address and port number of the socket.
There is another type of structure:
Struct sockaddr_in {
Short int sin_family; / * address family * /
Unsigned short int sin_port; / * port number * /
Struct in_addr sin_addr; / * IP address * /
Unsigned char sin_zero [8]; / * pad 0 to keep the same size as struct sockaddr * /
};
This structure is more convenient to use. sin_zero (which is used to fill the sockaddr_in structure to the same length as struct sockaddr) should be set to zero using the bzero () or memset () function. The pointer to sockaddr_in and the pointer to sockaddr can be converted to each other, which means that if a function requires a parameter type of sockaddr, you can convert a pointer to sockaddr_in to a pointer to sockaddr when the function is called; or vice versa. sin_family is usually assigned AF_INET; sin_port and sin_addr should be converted to network byte order; sin_addr does not need to be converted.
We discuss several byte order conversion functions below:
Htons ()-"Host to Network Short"; htonl ()-"Host to Network Long"
Ntohs ()-"Network to Host Short"; ntohl ()-"Network to Host Long"
Here, h means "host", n means "network", s means "short", and l means "long".
Reply

Use magic Report

0

Threads

59

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 Belgium

Post time: 2020-1-13 16:54:01
| Show all posts
sockaddr_in saddr;

...

UHOSRT nPort = ntohs (saddr.sin_port);

CHAR * pszIP = inet_ntoa (saddr.s_addr);
Reply

Use magic Report

0

Threads

24

Posts

19.00

Credits

Newbie

Rank: 1

Credits
19.00

 China

Post time: 2020-1-13 18:18:02
| Show all posts
struct sockaddr_in sockAddr;

DWORD nIP = ntohl (sockAddr.sin_addr.S_un.S_addr);
WORD nPort = ntohs (sockAddr.sin_port);
Reply

Use magic Report

4

Threads

16

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-1-14 16:36:01
| Show all posts
DWORD dwIP = inet_ntoa (addrClient.sin_addr);
UINT nPort = ntohs (addrClient.sin_port);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list