DESKTOP

Network Programming with Windows Sockets : A Socket-Based Client

10/10/2010 3:10:42 PM
The conversion is straightforward, with several small differences.
  • Rather than locating a server using mailslots, the user enters the IP address on the command line. If the IP address is not specified, the default address is 127.0.0.1, which indicates the current machine.

  • Functions for sending and receiving messages, such as ReceiveMessage, are used but are not shown here.

  • The port number, SERVER_PORT, is defined in the header file, ClientServer.h.

Program 1. clientSK: Socket-Based Client
/* Single-threaded command line client. */
/* WINDOWS SOCKETS VERSION. */
/* Reads a sequence of commands to send to a server process */
/* over a socket connection. Wait for and display response. */

#include "Everything.h"
#include "ClientServer.h"/* Defines request and response records. */

static BOOL SendRequestMessage (REQUEST *, SOCKET);
static BOOL ReceiveResponseMessage (RESPONSE *, SOCKET);

struct sockaddr_in clientSAddr;/* Client Socket address structure */

int main (int argc, LPSTR argv[])
{
SOCKET clientSock = INVALID_SOCKET;
REQUEST request;/* See clntcrvr.h */
RESPONSE response;/* See clntcrvr.h */
WSADATA WSStartData; /* Socket library data structure */
BOOL quit = FALSE;
DWORD conVal;

/* Initialize the WS library. Ver 2.0 */
WSAStartup (MAKEWORD (2, 0), &WSStartData);
/* Connect to the server */
/* Follow the standard client socket/connect sequence */
clientSock = socket(AF_INET, SOCK_STREAM, 0);
memset (&clientSAddr, 0, sizeof(clientSAddr));
clientSAddr.sin_family = AF_INET;
if (argc >= 2)
clientSAddr.sin_addr.s_addr = inet_addr (argv[1]);
else
clientSAddr.sin_addr.s_addr = inet_addr ("127.0.0.1");

clientSAddr.sin_port = htons(SERVER_PORT);
conVal = connect (clientSock, (struct sockaddr *)&clientSAddr,
sizeof(clientSAddr));
/* Main loop to prompt user, send request, receive response */
while (!quit) {
_tprintf (_T("%s"), _T("\nEnter Command: "));
fgets (request.record, MAX_RQRS_LEN, stdin);
/* Get rid of the new line at the end */
request.record[strlen(request.record)-1] = '\0';
if (strcmp (request.record, "$Quit") == 0) quit = TRUE;
SendRequestMessage (&request, clientSock);
if (!quit) ReceiveResponseMessage (&response, clientSock);
}

shutdown (clientSock, SD_BOTH); /* Disallow sends and receives */
closesocket (clientSock);
WSACleanup();
_tprintf (_T("\n****Leaving client\n"));
return 0;
}


While the code is written for Windows, there are no Windows dependencies other than the WSA calls.

Comment: The programs in this chapter do not use generic characters. This is a simplification driven by the fact that inet_addr does not accept Unicode strings.

Running the Socket Client

The socket server is complex with long program listings. Therefore, Run 1 shows the client in operation, assuming that there is a running server.

Run 1. clientSK: Socket Client Operation


Other  
  •  Network Programming with Windows Sockets : A Socket Message Receive Function
  •  Exchange Server 2010 : Operating Without Traditional Point-in-Time Backups
  •  Exchange Server 2010 : Performing Backup and Recovery for Mailbox Server Roles
  •  Exchange Server 2010 : Performing Backup and Recovery for Non-Mailbox Server Roles
  •  Exchange Server 2010 : Backup and Disaster Recovery Planning
  •  Changes to Backup and Restore in Exchange Server 2010
  •  Programming Windows Azure : Using the SDK and Development Storage
  •  Programming Windows Azure : Building a Storage Client
  •  Working with the REST API
  •  Excel Programmer : Fix Misteakes
  •  Excel Programmer : Change Recorded Code
  •  Excel Programmer : Record and Read Code
  •  Configuring Server Roles in Windows 2008 : New Roles in 2008
  •  Windows Server 2003 : Creating and Configuring Application Directory Partitions
  •  Windows Server 2003 : Configuring Forest and Domain Functional Levels
  •  Windows Server 2003 : Installing and Configuring Domain Controllers
  •  Manage Server Core
  •  Configure Server Core Postinstallation
  •  Install Server Core
  •  Determine Your Need for Server Core
  •  
    Top 10
    Fujifilm XF1 - The Stylish Shooter
    Nikon 1 V2 - Still Fast and Handles Better
    Asustor AS-604T 4-Bay NAS Review (Part 3)
    Asustor AS-604T 4-Bay NAS Review (Part 2)
    Asustor AS-604T 4-Bay NAS Review (Part 1)
    Toshiba Satellite U925t Review (Part 3)
    Toshiba Satellite U925t Review (Part 2)
    Toshiba Satellite U925t Review (Part 1)
    iBall Andi 4.5H - Pretty In White
    The HTC Butterfly - Full HD In 5 Inches Only
    Most View
    Editions of SQL Server 2008
    Access media remotely : Step-by-step access your media remotely (part 1)
    OS X Mountain Lion - Bringing iOS features “back to the Mac” (Part 4)
    Fujifilm Finepix F800EXR – Another Excellent EXR
    Big Shot Rivals: Sony vs. Samsung
    Windows Server : Planning for Migration, Upgrade, and Restructuring
    AOC MyStage E2343Fi 23" LED Monitor
    ASP.NET 3.5 Social Networking : Messaging (part 2)
    LaCie Porsche P'9230 - Nice Big Desktop Drive
    Windows Home Server Installation and Configuration
    Optimizing for Vertical Search : Optimizing for Local Search (part 1) - Check Your Local Listings
    iPad eMail : Sending multiple e-mail attachments
    Blackberry World 2012 (Part 3) - Mobile computing platform
    Programming .NET Security : Extending the .NET Framework (part 2) - Using the AuthorMembershipCondition Membership Condition
    Windows 7 : Fine-Tuning the Settings for Windows Media Center
    Evidence Explained in .NET Security
    Programming the iPhone : Standard Control Types (part 5) - Search Bars
    10 ways to save money with your PC
    Sharepoint 2010 : BCS Architecture - Presentation & Core Components
    Maintaining Windows 7 with Backup and Restore (part 2) - Using Advanced Backup Options & Using System Protection