Windows Sockets API
(Winsock/WinSock/WinSock2/WSA 모두 이 곳으로 링크된다)
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
Win32 소켓프로그래밍을 진행하기 위해 사용되는 라이브러리는 아래와 같다.
Win32 | winsock2.h (WS2_32.lib) |
Linux | sys/socket.h |
APIs
My IP Address
- 자신의 로컬 컴퓨터 이름과 IP 주소 얻기
- 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?
Windows 에서 자신의 IP주소와 컴퓨터의 이름을 확인하는 방법.
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 (docs/WinSock2-doc/winsock2.html 참조) ↩