Windows Sockets API
lastmod 2018-08-17created 2018-08-036 edits
(Winsock/WinSock/WinSock2/WSA all link here)
In computing, the Windows Sockets API (WSA), which was later shortened to Winsock, is a technical specification that defines how Windows network software should access network services, especially TCP/IP. It defines a standard interface between a Windows TCP/IP client application (such as an FTP client or a web browser) and the underlying TCP/IP protocol stack. The nomenclature is based on the Berkeley sockets API model used in BSD for communications between programs. Initially, all the participating developers resisted the shortening of the name to Winsock for a long time, since there was much confusion among users between the API and the DLL library file (winsock.dll) which only exposed the common WSA interfaces to applications above it. Users would commonly believe that only making sure the DLL file was present on a system would provide full TCP/IP protocol support.
Header & Library
The libraries used for Win32 socket programming are as follows.
| Win32 | winsock2.h (WS2_32.lib) |
| Linux | sys/socket.h |
APIs
My IP Address
- Getting the name and IP address of your local computer
- Get the IP address of the machine
- Examples: Get the Local IP Address(es)
- Get global ip address
- Three ways to get your MAC address
- Thread: How to get the global IP address?
How to check your own IP address and computer name on Windows.
void GetMyIPAddress()
{
CString strMyIPAddress = "";
WSADATA WsaData;
char myaddr[256];
PHOSTENT pHostInfo;
struct sockaddr_in addr;
CArray<sockaddr_in, sockaddr_in&=""> myIPArray;
if(WSAStartup(0x202, &WsaData) == SOCKET_ERROR)
{
return;
}
gethostname(myaddr, sizeof(myaddr)); // 로컬 PC 이름을 가져옴
pHostInfo = gethostbyname(myaddr);
// HostInfo 구조체에서 IP 주소만 가져옴
if(pHostInfo)
{
for(int i=0 ;pHostInfo->h_addr_list[i] != NULL ;i++)
{
memcpy(&addr.sin_addr, pHostInfo->h_addr_list[i],
pHostInfo->h_length);
myIPArray.Add(addr);
}
}
for(int i=0 ;i < myIPArray.GetSize() ;i++)
{
strMyIPAddress += inet_ntoa(myIPArray.GetAt(i).sin_addr);
strMyIPAddress += "\n";
}
}
See also
Favorite site
- Wikipedia (en) Winsock
- [https://msdn.microsoft.com/ko-kr/library/windows/desktop/ms738545(v=vs.85.aspx MSDN - Getting Started with Winsock]
- Here U can get some understandings to new concept of WinSock21
References
-
Www.viper.pe.kr.zip (see docs/WinSock2-doc/winsock2.html) ↩