Skip to content

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

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

References


  1. Www.viper.pe.kr.zip (docs/WinSock2-doc/winsock2.html 참조)