CSCI 465 Lab 1

Socket Programming with Winsock

Windows Sockets 2 (Winsock) enables programmers to create advanced Internet, intranet, and other network-capable applications to transmit application data across the wire, independent of the network protocol being used. Winsock follows the Windows Open System Architecture (WOSA) model; it defines a standard service provider interface (SPI) between the application programming interface (API), with its exported functions and the protocol stacks. It uses the sockets paradigm that was first popularized by Berkeley Software Distribution (BSD) UNIX. See Microsoft documentation for more information.

1. To create a basic Winsock application, create a new empty project with the source file:

#include <winsock2.h>
#include <ws2tcpip.h>

#pragma comment(lib, "Ws2_32.lib")

int main() {
  return 0;
}

2. To initialize Winsock and initiate use of WS2_32.dll:

   WORD wVersion = MAKEWORD(2, 2); // Used to request version 2.2 of Windows sockets
   WSADATA wsaData;                // Data loaded by WSAStartup
   int iResult;                    // Error check if WSAStartup successful

   // Initialize Winsock
   iResult = WSAStartup(wVersion, &wsaData);
   if (iResult != 0) {
      cout << "WSAStartup failed: " << iResult << endl;
      return 1;
   }

3. To close the windows sockets, the following goes at the end of the program:

   WSACleanup();

4. To get your local hostname:

   char host_name[128];

   gethostname(host_name, sizeof(host_name));
   cout << host_name << endl;

5. To retrieve host information for a given host name:

struct hostent *remoteHost;
struct in_addr addr;
DWORD dwError;
char **pAlias;
int i;

remoteHost = gethostbyname(host_name);

if (remoteHost == NULL) {
   dwError = WSAGetLastError();
   if (dwError != 0) {
      if (dwError == WSAHOST_NOT_FOUND) {
         cout << "Host not found\n";
         return 1;
       } else if (dwError == WSANO_DATA) {
         cout << "No data record found\n";
       } else {
         cout << "Function failed with error: " << dwError << endl;
         return 1;
       }
   }
} else {
   cout << "Function returned:\n";
   cout << "Official name: " << remoteHost->h_name << endl;

   for (pAlias = remoteHost->h_aliases; *pAlias != 0; pAlias++) {
      cout << "Alternate name #" << ++i << ": " << *pAlias << endl;
   }

   cout << "\tAddress type: ";
   switch (remoteHost->h_addrtype) {
      case AF_INET:
         cout << "AF_INET\n";
         break;
      case AF_NETBIOS:
         cout << "AF_NETBIOS\n";
         break;
      default:
         cout << remoteHost->h_addrtype << endl;
         break;
   }

   cout << "\tAddress length: " << remoteHost->h_length << endl;

   i = 0;
   if (remoteHost->h_addrtype == AF_INET)
   {
       while (remoteHost->h_addr_list[i] != 0) {
           addr.s_addr = *(u_long *) remoteHost->h_addr_list[i++];
           cout << "\tIP Address #" << i << ": " << inet_ntoa(addr) << endl;
       }
   } else if (remoteHost->h_addrtype == AF_NETBIOS) {   
         cout << "NETBIOS address was returned\n";
   }   
}

Hand in

  1. Your program.
  2. Find the local host name and IP address.
  3. Find the IP addresses for juno.stfx.ca, alonso.stfx.ca, cs.stfx.ca, sites.stfx.ca, and moodle.stfx.ca.