| Tenouk.com |< Winsock Program Example 7 | Main | Winsock Program Example 9 >| Site Index | Download |


 

WINSOCK 2

WINDOWS SOCKET: PROGRAM EXAMPLES PART 8

 

 

 

Winsock Tutorial

 

Winsock: Story 1

Winsock: Story 2

Winsock: Story 3

Winsock: Example 1

Winsock: Example 2

Winsock: Example 3

Winsock: Example 4

Winsock: Example 5

Winsock: Example 6

Winsock: Example 7

Winsock: Example 8

Winsock: Example 9

Winsock: Example 10

Winsock: Example 11

Winsock: Example 12

Winsock: Reference 1

Winsock: Reference 2

Winsock: Reference 3

Winsock: Reference 4

My Training Period:          hours

 

Note:

This is a continuation from previous Module.

Machine’s OS is standalone Windows Xp Pro with SP2 except whenever mentioned.  Compiler used was Visual C++ 2003 .Net 1.1. Beware the codes that span more than one line.  Program examples have been tested for Non Destructive Test :o).  All information compiled for Windows 2000 (NT5.0) above and...

 

  1. The story was discussed at Winsock introduction story.
  2. Related functions, structures and macros used in the program examples have been dumped at Winsock structure & function 1 and Winsock structure & function 2.
  3. Other related and required information (if any) not available in no. 2 can be found at MSDN & Visual C++ online reference.

 

Abilities

 

         Able to understand more Winsock implementation and operations through the APIs and program examples.

         Able to gather, understand and use the Winsock functions, structures and macros in your programs.

         Able to build programs that use Microsoft C/Standard C programming language and Winsock APIs.

 

sockaddr

 

Item

Description

Structure

sockaddr.

Info

Contains socket address information. The sockaddr structure varies depending on the protocol selected. It is better to use SOCKADDR_STORAGE instead.

Definition

struct sockaddr_in {

        short   sin_family;

        u_short sin_port;

        struct  in_addr sin_addr;

        char    sin_zero[8];

};

struct sockaddr_in6 {

        short   sin6_family;

        u_short sin6_port;

        u_long  sin6_flowinfo;

        struct  in6_addr sin6_addr;

        u_long  sin6_scope_id;

};

struct sockaddr_in6_old {

        short   sin6_family;       

        u_short sin6_port;         

        u_long  sin6_flowinfo;     

        struct  in6_addr sin6_addr; 

};

Members

As mentioned above.

Header file

<winsock2.h> for IPv4; <ws2tcpip.h> for IPv6.

Remark

The sockaddr structure varies depending on the protocol selected. Except for the sin*_family parameter, sockaddr contents are expressed in network byte order.

Winsock functions using sockaddr are not strictly interpreted to be pointers to a sockaddr structure. The structure is interpreted differently in the context of different address families. The only requirements are that the first u_short is the address family and the total size of the memory buffer in bytes is namelen.  Other protocols use similar structures.

 

Table 1

 

Program Example

 

The following example demonstrates the use of the sockaddr structure.

 

#include <stdio.h>

#include "winsock2.h"

 

int main()

{

  // Declare variables

  WSADATA wsaData;

  SOCKET ListenSocket;

  sockaddr_in service;

 

  // Initialize Winsock, request the Winsock 2.2

  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

  if(iResult != NO_ERROR)

    printf("Error at WSAStartup().\n");

  else

    printf("WSAStartup() is OK.\n");

 

  // Create a listening socket

  ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  if (ListenSocket == INVALID_SOCKET)

  {

    printf("Error at socket().\n");

    WSACleanup();

    return 0;

  }

  else

     printf("socket() is OK.\n");

 

  // Bind the socket to the local IP address and port 55555

  hostent* thisHost;

  char* ip;

  u_short port;

  port = 55555;

  thisHost = gethostbyname("");

  ip = inet_ntoa(*(struct in_addr *)*thisHost->h_addr_list);

 

  printf("\nIP address is: %s.\n", ip);

  printf("Hostname is: %s.\n", thisHost->h_name);

  printf("Address type: %i.\n\n", thisHost->h_addrtype);

 

  service.sin_family = AF_INET;

  service.sin_addr.s_addr = inet_addr(ip);

  service.sin_port = htons(port);

 

  if (bind(ListenSocket,(SOCKADDR*) &service, sizeof(service)) == SOCKET_ERROR)

  {

    printf("bind() failed lol!\n");

    closesocket(ListenSocket);

    return 0;

  }

  else

    printf("bind() is OK.\n");

 

  WSACleanup();

  return 0;

}

 

 

 

 

 

Figure 1

 

 

hostent

 

Item

Description

Structure

hostent.

Info

Used by functions to store information about a given host, such as host name, IP address, and so forth. An application should never attempt to modify this structure or to free any of its components. Furthermore, only one copy of the hostent structure is allocated per thread, and an application should therefore copy any information that it needs before issuing any other Windows Sockets API calls.

Definition

typedef struct hostent {
  char FAR* h_name;
  char FAR  FAR** h_aliases;
  short h_addrtype;
  short h_length;
  char FAR  FAR** h_addr_list;

} hostent;

Members

h_name

Official name of the host (PC). If using the DNS or similar resolution system, it is the Fully Qualified Domain Name (FQDN) that caused the server to return a reply. If using a local hosts file, it is the first entry after the IP address.

 

h_aliases

Null-terminated array of alternate names.

 

h_addrtype

Type of address being returned.

 

h_length

Length of each address, in bytes.

 

h_addr_list

Null-terminated list of addresses for the host. Addresses are returned in network byte order. The macro h_addr is defined to be h_addr_list[0] for compatibility with older software.

Header file

<winsock2.h>

Remark

-

 

Table 2

 

gethostbyaddr()

 

Item

Description

Function

gethostbyaddr().

Use

Retrieves the host information corresponding to a network address. The gethostbyaddr function has been deprecated by the introduction of the getnameinfo() function. Developers creating Windows Sockets 2 applications are urged to use the getnameinfo function instead of the gethostbyaddr() function.

Prototype

struct HOSTENT* FAR gethostbyaddr(

  const char* addr,

  int len,

  int type);

Parameters

addr

[in] Pointer to an address in network byte order.

 

len

[in] Length of the address, in bytes.

 

type

[in] Type of the address, such as the AF_INET address family type (defined as TCP, UDP, and other associated Internet protocols). Address family types and their corresponding values are defined in the winsock2.h header file.

Return value

See below.

Include file

<winsock2.h>

Library

ws2_32.lib.

Remark

The gethostbyaddr() function returns a pointer to the hostent structure that contains the name and address corresponding to the given network address. Although gethostbyaddr is deprecated by the getnameinfo() function, gethostbyaddr is capable of returning a NetBIOS name; getnameinfo is not. Developers requiring NetBIOS name resolution may need to use gethostbyaddr() until their applications are completely independent of NetBIOS names. Note   The capability to perform reverse lookups using the gethostbyaddr function is convenient, but such lookups are considered inherently unreliable, and should be used only as a hint.

 

Table 3

 

Return Values

 

If no error occurs, gethostbyaddr() returns a pointer to the hostent structure. Otherwise, it returns a null pointer, and a specific error code can be retrieved by calling WSAGetLastError().

 

Error code

Meaning

WSANOTINITIALISED

A successful WSAStartup() call must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAHOST_NOT_FOUND

Authoritative answer host not found.

WSATRY_AGAIN

Non-authoritative host not found, or server failed.

WSANO_RECOVERY

A non-recoverable error occurred.

WSANO_DATA

Valid name, no data record of requested type.

WSAEINPROGRESS

A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEAFNOSUPPORT

The type specified is not supported by the Windows Sockets implementation.

WSAEFAULT

The addr parameter is not a valid part of the user address space, or the len parameter is too small.

WSAEINTR

A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall().

 

Table 4

 

For program sample, see the following example.

 

gethostbyname()

 

Item

Description

Function

gethostbyname().

Use

Retrieves host information corresponding to a host name from a host database. The gethostbyname() function has been deprecated by the introduction of the getaddrinfo() function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo() function instead of gethostbyname().

Prototype

struct hostent* FAR gethostbyname(const char* name);

Parameters

name

[in] Pointer to the null-terminated name of the host to resolve.

Return value

See below.

Include file

<winsock2.h>

Library

ws2_32.lib.

Remark

The gethostbyname() function returns a pointer to a hostent structure - a structure allocated by Windows Sockets. The hostent structure contains the results of a successful search for the host specified in the name parameter.

The application must never attempt to modify this structure or to free any of its components. Furthermore, only one copy of this structure is allocated per thread, so the application should copy any information it needs before issuing any other Windows Sockets function calls.

The gethostbyname() function cannot resolve IP address strings passed to it. Such a request is treated exactly as if an unknown host name were passed. Use inet_addr to convert an IP address string the string to an actual IP address, then use another function, gethostbyaddr(), to obtain the contents of the hostent structure. If null is provided in the name parameter, the returned string is the same as the string returned by a successful gethostname() function call. The gethostbyname() function does not check the size of the name parameter before passing the buffer. In improperly sized name parameters, heap corruption can occur.

 

Table 5

 

Return Values

 

If no error occurs, gethostbyname() returns a pointer to the hostent structure described above. Otherwise, it returns a null pointer and a specific error number can be retrieved by calling WSAGetLastError().

 

Error code

Meaning

WSANOTINITIALISED

A successful WSAStartup() call must occur before using this function.

WSAENETDOWN

The network subsystem has failed.

WSAHOST_NOT_FOUND

Authoritative answer host not found.

WSATRY_AGAIN

Non-authoritative host not found, or server failure.

WSANO_RECOVERY

A non-recoverable error occurred.

WSANO_DATA

Valid name, no data record of requested type.

WSAEINPROGRESS

A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.

WSAEFAULT

The name parameter is not a valid part of the user address space.

WSAEINTR

A blocking Windows Socket 1.1 call was canceled through WSACancelBlockingCall().

 

Table 6

 

Program Example

 

The following examples demonstrates the use of the gethostbyname() and  gethostbyaddr() functions.

 

#include <stdio.h>

#include <winsock2.h>

 

int main()

{

  // Declare variables

  WSADATA wsaData;

 

  // Initialize Winsock, request the Winsock 2.2

  int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);

  if(iResult != NO_ERROR)

    printf("Error at WSAStartup().\n");

  else

    printf("WSAStartup() is OK.\n");

 

// Declare and initialize variables

hostent* remoteHost;

char* ip;

char* host_name;

unsigned int addr;

 

// User inputs name of host

printf("Input name/IP of host: ");

host_name = (char*) malloc(sizeof(char*)*128);

fgets(host_name, 128, stdin);

 

// If the user input is an alpha name for the host, use gethostbyname()

// else, gethostbyaddr() (assume IPv4)

// host address is a name

if (isalpha(host_name[0]))

{

  printf("\nUsing gethostbyname()...\n");

  // NULL terminated

  host_name[strlen(host_name)-1] = '\0';

  remoteHost = gethostbyname(host_name);

  ip = inet_ntoa(*(struct in_addr *)*remoteHost->h_addr_list);

  printf("IP address is: %s.\n", ip);

  printf("Address type: %i.\n\n", remoteHost->h_addrtype);

 }

else

{

  printf("\nUsing gethostbyaddr()...\n");

  addr = inet_addr(host_name);

  remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);

  printf("Hostname is: %s.\n", remoteHost->h_name);

  printf("Address type: %i.\n\n", remoteHost->h_addrtype);

}

 

if (WSAGetLastError() != 0)

{

  if (WSAGetLastError() == 11001)

  printf("Host not found...\nExiting.\n");

}

else

  printf("error #: %ld\n", WSAGetLastError());

 

// The remoteHost structure can now be used to

// access information about the host

 

  WSACleanup();

  return 0;

}

 

Figure 2

 

 

Figure 3

 

 

 

CONTINUE ON NEXT MODULE...MORE PROGRAM EXAMPLES

 

Further reading and digging:

 

  1. Linux Sockets: Story and program examples.
  2. Linux and TCP/IP.
  3. Structure, enum, union and typedef story can be found struct, enum, union & typedef tutorial.
  4. For Multibytes, Unicode characters and Localization please refer to Multibyte, Unicode and wide characters (Story) and Win32 Windows & Users tutorial (Implementation).
  5. Windows data type information is Win32 - Windows data types.
  6. Microsoft MSDN: Visual C++.
  7. Check the best selling C / C++ and Windows books at Amazon.com.

 

www.tenouk.com

 

| Tenouk.com |< Winsock Program Example 7 | Main | Winsock Program Example 9 >| Site Index | Download |


2003-2006 © Tenouk. All rights reserved.