⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winsockclient.cpp

📁 This zip file contain tow TDI client samples.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//
//*                                                                           //
//* Copyright (C) 2003, James Antognini, antognini@mindspring.com.            //
//*                                                                           //
//****************************************************************************//

/**************************************************************************************************/      
/*                                                                                                */      
/* Adapted from                                                                                   */
/* ms-help://MS.MSDNQTR.2002JUL.1033/wcewinsk/htm/_wcecomm_TCP_Stream_Socket_Client.htm.          */
/*                                                                                                */      
/**************************************************************************************************/      

#define JAProgNm      "WinsockClient"
#define JAProgVersion "1.50"

#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
#include <commctrl.h>
#include <winioctl.h>

#include "TDIClient.h"

int main(
         IN int    nbrArgs,
         IN char * pArgv[]
        )
{
  #define rcOK         0
  #define rcError      8

  #define CmdUnknown   0
  #define CmdStart     1
  #define CmdStop      2
  #define CmdTest      3
  #define CmdTest2     4

  static struct
    {
     DWORD CmdCode;
     char * pInStr;
    }
     InStrArr[] =
       {                                              // Except that CmdUnknown has to be first, the following can be in any order.
        {CmdUnknown, ""},
        {CmdTest,   "RmtServer"},
        {CmdTest2,  "."},
       };

  char             static dfltIPAddr[] =              // Default IP address.
                            charDfltServerAddr,
                          dfltPort[] = charDfltPort,  // Default port.
                          DateCompiledBase[] = __DATE__,
                          TimeCompiledBase[] = " "__TIME__,
                          JAProgInfo[] = JAProgNm " v" JAProgVersion " (compiled ";

  #define CompDateTimeStr "dd mmm yyyy hh:mm:ss"      

  char                    DateCompiled[12] =          // Build date in preferred (dd mmm yyyy) format.
                            {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                             DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                             DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                             0x0
                            },
                          szClientA[TDIClientRecvBfrLen],
                          PgmCompiledInfo[sizeof(CompDateTimeStr)+1];
  char           static * pServerBoundMessage[] = {
                                                   "This is a first (1) message from the Winsock client v" JAProgVersion " (compiled ",
                                                   "This is a second (2) message from the Winsock client.",
                                                   "This is a third (3) message from the Winsock client.",
                                                   "This is a fourth (4) message from the Winsock client.",
                                                   "This is a fifth (5) message from the Winsock client."
                                                  };
  char                  * pIPAddr,     
                        * pPort;       
  int rcRecv,                         // Return value of recv function
      port,                           // Port to use.
      i,
      rc = rcOK,
      lnInStrArr =                    // Get number of instances.          
        sizeof InStrArr / sizeof InStrArr[0],                           
      TotalRecvd = 0,
      CmdNbr = CmdUnknown;
  BOOL bFirst = TRUE;
  ULONG ulTotalSend;                                  // Total number of bytes that will be sent.
//int TimeOutVal,
//    szVal,
//    statusSockCall;
//ULONG valDontBlock;

  SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
  SOCKADDR_IN destination_sin;        // Server socket address
  PHOSTENT phostent = NULL;           // Points to the HOSTENT structure of the server
  WSADATA WSAData;                    // Contains details of the Winsocket implementation

  if (' '==DateCompiled[0])
   strcpy(PgmCompiledInfo, DateCompiled+1);
  else
   strcpy(PgmCompiledInfo, DateCompiled+0);

  strcat(PgmCompiledInfo, TimeCompiledBase);

  printf("%s%s)\n", JAProgInfo, PgmCompiledInfo);

  if (nbrArgs>=2)
    {
     for (i = 1; i < lnInStrArr; i ++)                // Go through expected parameters.
       if (0==_stricmp(pArgv[1], InStrArr[i].pInStr)) // Does the second parm match?
         break;                                       // If yes, break.
    }
  else
    i = lnInStrArr;                                   // Ensure next conditional yields supported parameter information.

  if (i<lnInStrArr)                                   // Found a supported command?
    {
     CmdNbr = InStrArr[i].CmdCode;
     if (CmdTest2==CmdNbr)                            // Dot given?
       CmdNbr = CmdTest;                              // Use full command.
    }
  else
    {
     printf("\nUnsupported command\n\n");
     printf("Usage: " JAProgNm " <operand>\n\n");
     printf("       where <operand> is one of these:\n\n");
     for (i = 1; i < lnInStrArr; i ++)
       printf("         %s\n", InStrArr[i].pInStr);
     printf("\n       Eg,\n\n         <RmtServer  | . <IP address | IP name | .> <port | .>>\n");
     printf("\n       (Capitalization is ignored.)\n");
     printf("\n       (Defaults:  IP addr = %s, port = %s.)\n", dfltIPAddr, dfltPort);

     rc = 1;
     goto ExitPoint;
    }

  if (nbrArgs>=3)
    {
     pIPAddr = pArgv[2];                              // Get IP address.
     if (0==strcmp(pIPAddr, "."))                     // Dot given?
       pIPAddr = dfltIPAddr;                          // Point to default IP address.
    }
  else
    pIPAddr = dfltIPAddr;                             // Get IP address.

  if (4==nbrArgs)
    {
     pPort = pArgv[3];                                // Get port.
     if (0==strcmp(pPort, "."))                       // Dot given?
       pPort = dfltPort;                              // Point to default port.
    }
  else
    pPort = dfltPort;                                 // Get port.

  port = atoi(pPort);                                 // Convert ascii to integer.

  printf("  Using IP address = %s, port = %d\n", pIPAddr, port);

  // Initialize Winsocket. 
  if (WSAStartup(MAKEWORD(2,0), &WSAData) != 0)       // Accept up to Winsock 2.0.
  {
    printf(JAProgNm ":  WSAStartup failed.  Error = %d\n", WSAGetLastError());
    return 1;
  }

  // Create a TCP/IP socket that is bound to the server.
  if ((ServerSock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
  {
    printf(JAProgNm ":  Allocating socket failed.  Error = %d\n", WSAGetLastError());
    return 1;
  }

//statusSockCall = ioctlsocket(ServerSock,            // Make socket non-blocking.
//                             FIONBIO,
//                             &valDontBlock
//                            );
//
//if (0!=statusSockCall)
//  {
//   printf(JAProgNm ":  Failed in ioctlsocket(), rc = %d\n", WSAGetLastError());
//  }

//TimeOutVal = -1;
//szVal = sizeof(TimeOutVal);
//
//statusSockCall = getsockopt(ServerSock,
//                            SOL_SOCKET,
//                            SO_SNDTIMEO,
//                            (char*)&TimeOutVal,
//                            &szVal
//                           );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -