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

📄 multitst.c

📁 windows 网络编程。pdf文档
💻 C
📖 第 1 页 / 共 3 页
字号:
/*---------------------------------------------------------------------
 *
 *  Program: MULTITST.EXE  Optional WinSock features test program
 *
 *  filename: multitst.c
 *
 *  copyright by Bob Quinn, 1995
 *
 *  Description: this program let's you test the optional features multicast, 
 *   raw sockets and IP_TTL socket option in a WinSock implementation.
 *
 *  Specifically, it tests whether a WinSock DLL supports Steve Deering's 
 *  multicast API, as described in his document "IP Multicast Extensions 
 *  for 4.3BSD UNIX and related systems" that acommpanied RFC-1054
 *  (subsequently obsoleted by RFC-1112 "Host Extensions for IP Multicasting")
 *  that describes the mechanics of multicasting, and in particular the
 *  role that Internet Group Management Protcol (IGMP) plays.
 *
 *  It also calls socket() with type=SOCK_RAW and protocol=IPROTO_ICMP
 *  to attempt a ping (ICMP echo request and reply).  If you select the
 *  traceroute option, it will try to call setsockopt() with the
 *  level=IPPROTO_IP and option=IP_TTL (value 4) to set the IP "time to
 *  live" field (for traceroute).
 * .
 *  This software is not subject to any  export  provision  of
 *  the  United  States  Department  of  Commerce,  and may be
 *  exported to any country or planet.
 *
 *  Permission is granted to anyone to use this  software  for any  
 *  purpose  on  any computer system, and to alter it and redistribute 
 *  it freely, subject to the following  restrictions:
 *
 *  1. The author is not responsible for the consequences of
 *     use of this software, no matter how awful, even if they
 *     arise from flaws in it.
 *
 *  2. The origin of this software must not be misrepresented,
 *     either by explicit claim or by omission.  Since few users
 *     ever read sources, credits must appear in the documentation.
 *
 *  3. Altered versions must be plainly marked as such, and
 *     must not be misrepresented as being the original software.
 *     Since few users ever read sources, credits must appear in
 *     the documentation.
 *
 *  4. This notice may not be removed or altered.
 *	 
 ---------------------------------------------------------------------*/
#define STRICT

#include <windows.h>
#include <windowsx.h>
#include <winsock.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

#include "resource.h"
#include "..\winsockx.h"
#include "..\wsa_xtra.h"

/* default multicast and destination port number to use */
#define DESTINATION_MCAST  "234.5.6.7"
#define DESTINATION_PORT  4567

#define USER_PARAMETER_VALUES 99
#define MCBUFSIZE 1024

/* display buffer (8K) */
#define DISP_LINE_LEN  128
#define DISP_NUM_LINES 64
#define DISP_BUFSIZE   DISP_LINE_LEN * DISP_NUM_LINES

/* externally defined functions */
extern SOCKET icmp_open (void);
extern int    icmp_close (SOCKET);
extern u_long icmp_sendto (SOCKET, HWND, LPSOCKADDR_IN, int, int, int);
extern u_long icmp_recvfrom (SOCKET, LPINT, LPINT, LPSOCKADDR_IN);
extern int    set_ttl (SOCKET, int);

/* internally defined public functions */
LRESULT CALLBACK MainWinProc(HWND, UINT, WPARAM, LPARAM);
BOOL FAR PASCAL BindDlgProc (HWND, UINT,  UINT, LPARAM);
BOOL FAR PASCAL SendDlgProc (HWND, UINT,  UINT, LPARAM);
BOOL FAR PASCAL OptionDlgProc (HWND, UINT,  UINT, LPARAM);
BOOL FAR PASCAL PingDlgProc (HWND, UINT, UINT, LPARAM);
void WSAErrMsg (LPSTR);

void InitWndData(HWND);
void UpdateDispBuf(HWND, LPSTR);
void UpdateWnd(HWND);

/* private data */
static HINSTANCE hInst;
static HWND      hwnd;

char  szAppName[] = "Multitst";

/* global data */ 
HWND hDeskTop;
RECT rcDeskTop;
int nDeskTopWidth, nXCenter, nDeskTopHeight, nYCenter;
int nMenuHeight, nCaptionHeight, nScrollWidth, nScrollHeight;
int nCharWidth, nCharHeight;
int nLinesToDisplay;
HGLOBAL hDispBuf;
LPSTR  lpDispBuf;
int nFirstLine=0;
int nLastLine=0;
int WSAInitFailed;
BOOL bBSD_OptNames=TRUE;  
char strDestMulti[MAXHOSTNAME] = {DESTINATION_MCAST}; 
char strSrcMulti[MAXHOSTNAME] = {DESTINATION_MCAST}; 
u_short nDestPort = DESTINATION_PORT;
u_short nSrcPort  = DESTINATION_PORT;
SOCKET hSock = INVALID_SOCKET;
struct sockaddr_in stDestAddr, stSrcAddr;
BOOL   bSocketOpen;
char achOutBuf[MCBUFSIZE];
char achInBuf [MCBUFSIZE];

/*-----------------------------------------------------------
 * Function: WinMain()
 *
 * Description: entry point
 */
int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInst,
        LPSTR lpszCmdLine, int nShow)  {
  WNDCLASS  wc;
  MSG       msg;

  lpszCmdLine = lpszCmdLine;  /* avoid warning */ 
 
  hInst = hInstance;
  hwnd = NULL;
      
  if (!hPrevInst) {
    wc.style        = (unsigned int)NULL;
    wc.lpfnWndProc  = MainWinProc;
    wc.cbClsExtra   = 0;
    wc.cbWndExtra   = 0;
    wc.hInstance    = hInst;
    wc.hIcon        = LoadIcon(hInst,MAKEINTRESOURCE(MULTITST));
    wc.hCursor      = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground= GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = MAKEINTRESOURCE(MULTITST);
    wc.lpszClassName= szAppName;                         

    if (!RegisterClass(&wc)) {
      MessageBox (NULL, "Couldn't register Window Class",
          NULL, MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);
      return (0);
    }
  }
        
  InitWndData(hwnd);		/* for window dressing */

  hwnd = CreateWindow (szAppName,
    "Multitst",
    WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL,
    CW_USEDEFAULT, CW_USEDEFAULT,
    ((rcDeskTop.right-rcDeskTop.left)/2),  /* Window about half screen */ 
    ((rcDeskTop.bottom-rcDeskTop.top)/2),
    NULL,NULL,hInst,NULL);  
 
  if (hwnd == NULL) {
    MessageBox (NULL, "Couldn't create main window",
      NULL, MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);
    return (0);
  }

  ShowWindow (hwnd, nShow);
  UpdateWindow(hwnd);
    	
  while ( GetMessage(&msg, NULL, 0, 0) ) {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
    
  if (hDispBuf)
    GlobalFree(hDispBuf);

  return (msg.wParam);
} /* end WndMain() */

/*-----------------------------------------------------------
 * Function: MainWndProc()
 *
 * Description: procedure for our main window.
 */
LRESULT CALLBACK MainWinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  static short cxChar, cyCaps, cyChar, cxClient, cyClient, nMaxWidth,
               nVscrollPos, nVscrollMax, nHscrollPos, nHscrollMax;
  FARPROC lpfnProc;
  HDC hdc;
  TEXTMETRIC tm;
  int WSAErr, WSAEvent, nRet;
 
  switch (msg) {
    case WM_CREATE:
      /*------------- Register with WinSock DLL -----------*/
      WSAInitFailed = 
        WSAStartup(WSA_VERSION, &stWSAData); 
      if (WSAInitFailed != 0)  {
        WSAperror(WSAInitFailed, "WSAStartup()", hInst);
        PostMessage(hwnd, WM_CLOSE, 0, 0L);
      }
      /* Initialize destination address structure */
      stDestAddr.sin_family       = PF_INET;
      stDestAddr.sin_addr.s_addr  = inet_addr (strDestMulti);
      stDestAddr.sin_port         = htons (nDestPort);
      
      /* get window vitals */
      hdc = GetDC(hwnd);
      GetTextMetrics(hdc, &tm);
      nCharWidth  = tm.tmAveCharWidth;
      nCharHeight = tm.tmHeight + tm.tmExternalLeading;
      ReleaseDC(hwnd, hdc);
  
      /* center window */
      CenterWnd (hwnd, NULL, TRUE);
      
      break;
    
    case WM_COMMAND:
      switch(wParam) {
        case WSA_ASYNC:
          /* We received a WSAAsyncSelect() FD_ notification message 
           *  Parse the message to extract FD_ event value and error
           *  value (if there is one).
           */
          WSAEvent = WSAGETSELECTEVENT (lParam);
          WSAErr   = WSAGETSELECTERROR (lParam);
          if (WSAErr) {
            /* Error in asynch notification message: display to user */
            int i,j;
            for (i=0, j=WSAEvent; j; i++, j>>=1); /* convert bit to index */
            WSAperror(WSAErr, aszWSAEvent[i], hInst);
            /* fall-through to call reenabling function for this event */
          }
          switch (WSAEvent) {
            int tmp;
            case FD_READ:
              /* Receive the available data */
              tmp = sizeof(struct sockaddr);
              nRet = recvfrom (hSock, (char FAR *)achInBuf, MCBUFSIZE, 0,
                (struct sockaddr *) &stDestAddr, &tmp);
              if (nRet == SOCKET_ERROR) {
                WSAErrMsg ("recvfrom()");
                break;
              }
              /* Display the data received.*/
              MessageBox (hwnd, (LPSTR)achInBuf, 
		      "Multicast received!", MB_SYSTEMMODAL);
              break;
          } /* end switch(WSAEvent) */
          break;
        case IDM_NEWSOCKET:
          /* close current socket (if there is one) */
          if (hSock != INVALID_SOCKET) {        
            nRet = closesocket (hSock);
            if (nRet == SOCKET_ERROR) {
              WSAErrMsg("closesocket()");
            }
          }
          hSock = socket (PF_INET, SOCK_DGRAM, 0);
          if (hSock == INVALID_SOCKET) {
            WSAErrMsg ("socket()");
          } else {
            /*
             * Register for FD_READ events and post a WSA_ASYNC message
             */
            nRet = WSAAsyncSelect (hSock, hwnd, WSA_ASYNC, FD_READ);
            if (nRet == SOCKET_ERROR) {
              WSAErrMsg("WSAAsyncSelect(FD_READ)");
            } else {
               wsprintf ((LPSTR)achOutBuf, 
                 "Socket %d registered for FD_READ", hSock);
               MessageBox (hwnd, (LPSTR)achOutBuf, 
                 "Ready to Send", MB_OK | MB_ICONASTERISK);
            }
          }
          break;
        case IDM_BIND:
          lpfnProc = MakeProcInstance((FARPROC)BindDlgProc, hInst);
          DialogBoxParam (hInst, 
            "BindDlg", 
            hwnd, 
            (DLGPROC)lpfnProc,
            MAKELPARAM(hInst, hwnd));
          FreeProcInstance((FARPROC) lpfnProc);
          break;
        case IDM_SOCKOPTS:
          lpfnProc = MakeProcInstance((FARPROC)OptionDlgProc, hInst);
          DialogBoxParam (hInst, 
            "MulticstDlg", 
            hwnd, 
            (DLGPROC)lpfnProc,
            MAKELPARAM(hInst, hwnd));
          FreeProcInstance((FARPROC) lpfnProc);
          break;
        case IDM_SENDTO:
          lpfnProc = MakeProcInstance((FARPROC)SendDlgProc, hInst);
          DialogBoxParam (hInst, 
            "SendDlg", 
            hwnd, 
            (DLGPROC)lpfnProc,
            MAKELPARAM(hInst, hwnd));
          FreeProcInstance((FARPROC) lpfnProc);
          break;
        case IDM_PING:
          lpfnProc = MakeProcInstance((FARPROC)PingDlgProc, hInst);
          DialogBoxParam (hInst, 
            "PingDlg", 
            hwnd, 
            (DLGPROC)lpfnProc,
            MAKELPARAM(hInst, hwnd));
          FreeProcInstance((FARPROC) lpfnProc);
          break;
        case IDM_EXIT:
          PostMessage (hwnd, WM_CLOSE, 0, 0L);
          break;
        case IDM_ABOUT:
          DialogBox (hInst, MAKEINTRESOURCE(IDD_ABOUT), hwnd, Dlg_About);
          break;
      } /* end switch(wParam) */
      break;
    case WM_DESTROY:
      if (!WSAInitFailed) {
        if (hSock != INVALID_SOCKET)
          closesocket (hSock);
        WSACleanup();
      }
      PostQuitMessage (0);

⌨️ 快捷键说明

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