Struct sockaddr, sin_family is not a member

According to this article from msdn ( http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx ), the structure depends on the selected protocol!

Now I want to use this code from http://www.intelliproject.net/articles/showArticle/index/check_tcp_udp_port to check if the port is open or not!

Now I have a struct sockaddr as follows:

struct sockaddr { ushort sa_family; char sa_data[14]; }; 

but you need this strcuture:

 struct sockaddr { short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; 

What changes are needed?

(Ws2_32.lib is bundled and includes

 #define WIN32_LEAN_AND_MEAN // sockets #include "windows.h" #include <winsock2.h> #include <ws2tcpip.h> 

thanks

+4
source share
1 answer

In the sockets API, the address structure used is protocol dependent. If you are using IPv4, you need the sockaddr_in structure.

The socket API appeared long ago when void * not standard. Pointers to sockaddr are used in all socket functions in the same way as we use void * pointers. Functions expect you to pass pointers to structures associated with the protocols you use. It’s unpleasant that you need an order when you pass a pointer to your address structure, but you can’t do anything about it.

IPv4 Example:

 struct sockaddr_in address; // ... memset( &address, 0, sizeof(address) ); address.sin_family = AF_INET; address.sin_port = htons(1100); address.sin_addr.s_addr = INADDR_ANY; if ( bind( sock, (struct sockaddr *)&address, sizeof(address) ) < 0 ) { //.... } 
+7
source

Source: https://habr.com/ru/post/1441780/


All Articles