Docsity

Windows Sockets Programming: Creating a WHOIS Client, Slides of Windows Programming

Partial preview of the text

Download Windows Sockets Programming: Creating a WHOIS Client and more Slides Windows Programming in PDF only on Docsity!

LL e c te c t u ru r ee

Introduction to Windows Sockets Programming

  • WHOIS port 43
  • WHOIS Server: whois.networksolutions.com
  • Description of the functionality of this utility

Small WinSock application example

A WHOIS client with the following functions

  • A dialog-based application
  • Accepts a domain name from user
  • connects to a WHOIS server on the internet,

whose name is hard-coded in the application

  • Sends a WHOIS request to the server
  • Shows WHOIS server response in a multi-line

edit control

Application User Interface

application example

#define WHOIS_PORT 43

#define WHOIS_SERVER_NAME

"whois.networksolutions.com"

Other servers could be "whois.pknic.net.pk" or

"whois.crsnic.net" etc.

#define MAX_DOMAIN_LEN 25

#define BUFFER_SIZE 16384

BOOL CALLBACK mainDialogProc(HWND hDlg, UINT message,

WPARAM wParam, LPARAM lParam);

SOCKET clientSocket;

application example

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR

lpCmdLine, int nCmdShow)

WSADATA wsaData;

HOSTENT *ptrHostEnt;

struct sockaddr_in serverSockAddr; // the address of the socket to

connect to

int abc;

// try initialising the windows sockets library

if(WSAStartup( MAKEWORD(1,1), &wsaData)) // request WinSock ver

MessageBox(NULL, "Error initialising sockets library.", "WinSock Error",

MB_OK | MB_ICONSTOP);

return 1;

// fill out the address of the server.
serverSockAddr.sin_family = AF_INET; // fill the address structure with
appropriate values
serverSockAddr.sin_port = htons(WHOIS_PORT); // MUST convert to
network byte-order
memset(serverSockAddr.sin_zero, 0, sizeof(serverSockAddr.sin_zero));
memcpy(&serverSockAddr.sin_addr.S_un.S_addr, ptrHostEnt->h_addr_list[0],
sizeof(unsigned long));
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(clientSocket == INVALID_SOCKET)
MessageBox(NULL, "Error creating client socket.", "WinSock Error", MB_OK
| MB_ICONSTOP);
WSACleanup();
return 1;

application example

if(connect(clientSocket, (struct sockaddr *)&serverSockAddr,
sizeof(serverSockAddr)))
abc = WSAGetLastError();
MessageBox(NULL, "Error connecting to WHOIS server.", "WinSock Error",
MB_OK | MB_ICONSTOP);
WSACleanup();
return 1;
if(DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), NULL,
mainDialogProc) == 1)
MessageBox(NULL, "Error occurred while sending data to WHOIS server.",
"WinSock Error", MB_OK | MB_ICONSTOP);
WSACleanup();
return 0;

application example

case IDC_BUTTON_SEND:

EnableWindow(GetDlgItem(hDlg, IDC_BUTTON_SEND), FALSE); // disable for 2nd use GetDlgItemText(hDlg, IDC_EDIT_DOMAIN, (LPSTR)domainName, MAX_DOMAIN_LEN+1); strcpy(domainName+strlen(domainName), "\r\n"); if(send(clientSocket, (const char *)domainName, strlen(domainName), 0) == SOCKET_ERROR) EndDialog(hDlg, 1); else { bytesReceived = recv(clientSocket, startOfBuffer, BUFFER_SIZE-1, 0); // - for NULL while(bytesReceived > 0) // 0:close SOCKET_ERROR:error { startOfBuffer += bytesReceived; // move it forward bytesReceived = recv(clientSocket, startOfBuffer, BUFFER_SIZE-(startOfBuffer-result)-1, 0); // -1 for NULL }

application example

if(startOfBuffer != result) // something received *startOfBuffer = NULL; // NULL terminate else strcpy(result, "Null Response");

SetDlgItemText(hDlg, IDC_EDIT_RESULT, result); } break;

case IDCANCEL: EndDialog(hDlg, 0); break; } return TRUE; break;

default: return FALSE; } return TRUE;

}

application example