winsock 初體驗

發表於 四月 28, 2008. 分類: C/C++ |

以下範例為參考部份網路教學及windows platform SDK 2003 R2,使用Visual C++ 6.0,程式功能主要為使用gethostname函式得到本機端主機名稱然後用gethostbyname函式得到 IP Address,再以gethostbyaddr函式及其它函式(見範例),由 IP Address反向得到主機名稱,需declare Winsock2.h及 link Ws2_32.lib。

每一個使用winsock的應用程式,都必須進行WSAStart函式初始化,並且只有在初始化成功之後才能使用其它的winsock網路操作函式,使用完畢應將使用WSACleanup函式終止。

建立一新專案(Win32 Console Application),程式碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>    //must be declare before windows.h
#include <windows.h>

#define BUFFER_SIZE 256

int main(int arge,char *argv[])
{
    int error = 0;
    int WSA_return = 0;

    WORD wVersionRequested;
    LPWSADATA lpWSAData = (LPWSADATA)malloc(sizeof(WSADATA));

    BYTE LoVer = 0x0;
    BYTE HiVer = 0x0;
    char host_name[BUFFER_SIZE] = {NULL};
    char host_address[BUFFER_SIZE] = {NULL};
    hostent *host_entry = NULL;
    unsigned long addr = 0;

    // set Winsock version
    wVersionRequested = MAKEWORD(2, 2);
    WSA_return = WSAStartup(wVersionRequested, lpWSAData);
    if (WSA_return != 0)
    {
        printf("WSAStartup error, the error code is %d\n", WSAGetLastError());
        return 0;
    }

    LoVer = LOBYTE(lpWSAData->wVersion);
    HiVer = HIBYTE(lpWSAData->wVersion);
    printf("The call winsock version %d.%d\n", LoVer, HiVer);

    // Confirm that the WinSock DLL supports 2.2
    if(LoVer != 2 || HiVer != 2)
    {
        printf("No Version 2.2 of winsock.\n");
        WSACleanup();
        return 0;
    }

    error = gethostname(host_name, BUFFER_SIZE);
    if(error != 0)
    {
        printf("gethostname error, the error code is %d\n", WSAGetLastError());
        return 0;
    }
    printf("Host Name : %s\n", host_name);

    // by name
    host_entry = gethostbyname(host_name);
    if(host_entry == NULL)
    {
        printf("gethostbyname error, the error code is %d\n", WSAGetLastError());
        return 0;
    }
    sprintf(host_address, "%d.%d.%d.%d", host_entry->h_addr_list[0][0]&0x00ff
                                         , host_entry->h_addr_list[0][1]&0x00ff
                                         , host_entry->h_addr_list[0][2]&0x00ff
                                         , host_entry->h_addr_list[0][3]&0x00ff);

    printf("The computer %s ip address is %s\n", host_name, host_address);

    // by ip address
    addr = inet_addr(host_address);
    if(addr == INADDR_NONE)
    {
        printf("inet_addr error, the error code is %d\n", GetLastError());
        return 0;
    }
    host_entry = NULL;
    host_entry = gethostbyaddr((char *)&addr, 4, AF_INET);

    if(host_entry == NULL)
    {
        printf("gethostbyaddr error, the error code is %d\n", WSAGetLastError());
        return 0;
    }
    printf("The ip %s computer name is %s\n", host_address, host_entry->h_name);

    free(lpWSAData);
    lpWSAData = NULL;
    host_entry = NULL;

    WSACleanup();

    return 0;
}

Make a Comment

發表迴響

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

連結到 %s

Liked it here?
Why not try sites on the blogroll...

Follow

Get every new post delivered to your Inbox.