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
    A Look At Truecrypt The Open Source Security Tool
    Price Of Piracy
    Acer Aspire 5600U 23" Touchscreen All-in-One PC
    Zalman FX100-Cube Fanless Cooler
    Devolo dLAN LiveCam Starter Kit
    Has Apple Lost It? (Part 2)
    Has Apple Lost It? (Part 1)
    Sony Computer Entertainment (Part 3)
    Sony Computer Entertainment (Part 2)
    Sony Computer Entertainment (Part 1)
    Most View
    Database Availability Group Replication in Exchange Server 2010 : Understanding Database Availability Groups
    Optimizing for Vertical Search : Optimizing for Product Search
    Corsair Dominator Platinum Dual-Channel DDR3 Memory Kits (Part 1)
    Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
    The biggest TOS offenders (part 2)
    Asus Zenbook Prime UX51Vz – An Attractive And Sophisticated Ultrabook (Part 1)
    Visual Studio Team System 2008 : Deploying and Running Tests (part 1) - Remote deployment, Deploy additional files
    Systems for All Budgets (Part 3) - WS 1000, Silent 1000
    Crestron Speakers - First-Class Performance And Excellent Reliability
    Windows Sever 2003 : Troubleshooting Name Resolution
    Picking Up Last Generation Bargains (Part 2)
    SharePoint Administration with PowerShell (part 1)
    Tips & Tricks Of November 2012 (Part 3)
    ASP.NET AJAX : Progress Notification
    ASUS GTX 680 - Green Power Rises Again
    iPhone Programming : Simplifying the Template Classes
    Run Android Apps on Windows
    Sony Computer Entertainment (Part 3)
    Outlining AD DS Changes in Windows Server 2008 R2 (part 1)
    Meet “The New iPAD” : Retina Display, A5X CPU, iSight Camera