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

📄 multitst.c

📁 windows 网络编程。pdf文档
💻 C
📖 第 1 页 / 共 3 页
字号:
            case IDR_ADD:
              nLevel = IPPROTO_IP;
              nOptName = IP_ADD_MEMBERSHIP;
              nOptLen = sizeof (struct ip_mreq);
          lpOptVal = (char FAR *)&stIpReq;
          SetDlgItemInt  (hDlg, IDT_LEVELIN, IPPROTO_IP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTNAME, IP_ADD_MEMBERSHIP, FALSE);    
          SetDlgItemInt  (hDlg, IDT_OPTVAL, 0, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTLEN, sizeof(struct ip_mreq), FALSE);
              break;
              
         case IDR_DROP:
           nLevel = IPPROTO_IP;
           nOptName = IP_DROP_MEMBERSHIP;
           nOptLen = sizeof (struct ip_mreq);
          lpOptVal = (char FAR *)&stIpReq;
          SetDlgItemInt  (hDlg, IDT_LEVELIN, IPPROTO_IP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTNAME, IP_DROP_MEMBERSHIP, FALSE);    
          SetDlgItemInt  (hDlg, IDT_OPTVAL, 0, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTLEN, sizeof(struct ip_mreq), FALSE);
              break;
              
         case IDR_LOOP:
           nLevel = IPPROTO_IP;
           nOptName = IP_MULTICAST_LOOP;
           nOptLen = sizeof(int);
           nOptVal = IP_DEFAULT_MULTICAST_LOOP;
          lpOptVal = (char FAR *)&nOptVal;
          SetDlgItemInt  (hDlg, IDT_LEVELIN, IPPROTO_IP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTNAME, IP_MULTICAST_LOOP, FALSE);    
          SetDlgItemInt  (hDlg, IDT_OPTVAL, IP_DEFAULT_MULTICAST_LOOP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTLEN, sizeof(int), FALSE);
              break;
              
         case IDR_TTL:
           nLevel = IPPROTO_IP;
           nOptName = IP_MULTICAST_TTL;
           nOptLen = sizeof(int);
           nOptVal = IP_DEFAULT_MULTICAST_TTL;
          lpOptVal = (char FAR *)&nOptVal;
          SetDlgItemInt  (hDlg, IDT_LEVELIN, IPPROTO_IP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTNAME, IP_MULTICAST_TTL, FALSE);    
          SetDlgItemInt  (hDlg, IDT_OPTVAL, IP_DEFAULT_MULTICAST_TTL, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTLEN, sizeof(int), FALSE);
              break;
              
         case IDC_IF:
           nLevel = IPPROTO_IP;
           nOptName = IP_MULTICAST_IF;
           nOptLen = sizeof(int);
           stInAddr.s_addr = INADDR_ANY;
          lpOptVal = (char FAR *)&stInAddr;
          SetDlgItemInt  (hDlg, IDT_LEVELIN, IPPROTO_IP, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTNAME, IP_MULTICAST_IF, FALSE);    
          SetDlgItemInt  (hDlg, IDT_OPTVAL, 0, FALSE);
          SetDlgItemInt  (hDlg, IDT_OPTLEN, sizeof(struct in_addr), FALSE);
              break;
              
            case IDR_OTHER:  /* at time of call, we'll use parameters */ 
              nOptName = USER_PARAMETER_VALUES;
              
            case IDC_BSD_OPTNAMES:  /* use Berkeley option names if set (or Deering if not) */
          bBSD_OptNames = !bBSD_OptNames;              
              
            case IDT_SOCKIN:
            case IDT_LEVELIN:
            case IDT_OPTVAL:
           break;
        } /* end switch(wParam) */
        
    return TRUE;
    } /* end switch(msg) */
            
    return FALSE;
} /* end OptionDlgProc() */

/*-----------s------------------------------------------------
 * Function: PingDlgProc()
 *
 * Description: procedure for the "raw" icmp and IP TTL dialog
 */
BOOL CALLBACK PingDlgProc (
  HWND hDlg,
  UINT msg,
  UINT wParam,
  LPARAM lParam)
{
  static int nDataLen = 64;  /* arbitrary data length (short to be safe) */
  static HANDLE hwnd, hInst;
  static int nIcmpId=1, nIcmpSeq=1;
  static int nIPTTL = 1;
  static SOCKET PingSocket = INVALID_SOCKET;
  struct sockaddr_in stDest, stSrc;
  char szHost[MAXHOSTNAME];
  u_long lRoundTripTime;
  int nRet, nOldId, nOldSeq, WSAErr, WSAEvent;
  static u_long lSendTime;
  
    switch (msg) {
    case WM_INITDIALOG:
            /* get parameters passed */
        if (lParam) {        
          hInst = (HANDLE)LOWORD(lParam);
          hwnd  = (HANDLE)HIWORD(lParam);
        }
      /* set display values */
      gethostname(szHost, MAXHOSTNAME);
      SetDlgItemText (hDlg, IDC_DEST, szHost);
      SetDlgItemInt  (hDlg, IDC_TTLVALUE, nIPTTL,  FALSE);
      SetDlgItemInt  (hDlg, IDC_DATALEN, nDataLen, FALSE);
      SetDlgItemInt  (hDlg, IDC_ICMPID,  nIcmpId,  FALSE);
      SetDlgItemInt  (hDlg, IDC_ICMPSEQ, nIcmpSeq, FALSE);
      SetDlgItemInt  (hDlg, IDC_SOCKET,PingSocket, FALSE);
      
      SetFocus (GetDlgItem (hDlg, IDC_SENDTO));

      /* center dialog box */
      CenterWnd (hDlg, hwnd, TRUE);
      
      break;
        
      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) {
          case FD_READ:
            lSendTime = icmp_recvfrom(PingSocket, 
              &nOldId, &nOldSeq, &stSrc);
            
            /* calculate round trip time, and display */ 
               lRoundTripTime = GetCurrentTime() - lSendTime;

          {
            char achErrBuf[MCBUFSIZE];
            wsprintf (achErrBuf, 
                "RoundTripTime: %ld ms from %s (ID: %d, Seq: %d)", 
                lRoundTripTime, inet_ntoa(stSrc.sin_addr), nOldId, nOldSeq);
                      
            MessageBox (hDlg, (LPSTR)achErrBuf, 
                "Ping received!", MB_OK | MB_ICONASTERISK);
		      
            UpdateDispBuf(hwnd, achErrBuf);
            InvalidateRect(hwnd, NULL, TRUE); 
            UpdateWindow(hwnd);
          }
          break;
        case FD_WRITE:
          break;
        default:
          break;
        }
        break;
    case WM_COMMAND:
        switch (wParam) {
        case IDCANCEL:        
            case IDQUIT:
              /* close our ping socket before we leave */
              closesocket(PingSocket);
              PingSocket = INVALID_SOCKET;
              
                EndDialog (hDlg, TRUE);
                break;
                
            case IDC_DATALEN:
            case IDC_DEST:
              break;
            case IDC_OPEN:
              /* Get an ICMP "raw" socket (first close the one we
               *  have open, if there is one. */
              if (PingSocket != INVALID_SOCKET) {
                icmp_close(PingSocket);
              }
              PingSocket = icmp_open();
              
              /* Register for asynchronous notification */
              nRet = WSAAsyncSelect (PingSocket, hDlg, WSA_ASYNC, FD_READ|FD_WRITE);
              if (nRet == SOCKET_ERROR) {
                WSAErrMsg("WSAAsyncSelect()");
              } else {
                SetDlgItemInt(hDlg, IDC_SOCKET, PingSocket, FALSE);
              }
              break;
              
            case IDOK:
            case IDC_SENDTO:
              if (IsDlgButtonChecked (hDlg, IDC_TTLENABLE)) {
                nIPTTL = GetDlgItemInt (hDlg, IDC_TTLVALUE, NULL, FALSE);
                set_ttl(PingSocket, nIPTTL++);
                SetDlgItemInt  (hDlg, IDC_TTLVALUE, nIPTTL, FALSE);
              }
              nIcmpId  = GetDlgItemInt (hDlg, IDC_ICMPID, NULL, FALSE);
              nIcmpSeq = GetDlgItemInt (hDlg, IDC_ICMPSEQ, NULL, FALSE);
              GetDlgItemText (hDlg, IDC_DEST, szHost, 64);
              stDest.sin_family = PF_INET;
              stDest.sin_addr.s_addr = GetAddr(szHost);
              stDest.sin_port = 0;
              icmp_sendto (PingSocket, 
                 hwnd, 
                 &stDest,
                   nIcmpId++,
                 nIcmpSeq++,
                 GetDlgItemInt (hDlg, IDC_DATALEN, NULL, FALSE));
              SetDlgItemInt (hDlg, IDC_ICMPID,  nIcmpId, FALSE);
              SetDlgItemInt (hDlg, IDC_ICMPSEQ, nIcmpSeq, FALSE); 
              break;
            default:
              return (FALSE);
        } /* end switch(wParam) */
        
    return (TRUE);
    } /* end switch(msg) */
    
    return FALSE;
} /* end PingDlgProc() */ 


/*-----------------------------------------------------------
 * Function: WSAErrMsg()
 *
 * Description: wrapper to simplify our generic error function
 */
void WSAErrMsg(LPSTR szFuncName) {
  WSAperror(WSAGetLastError(), szFuncName, hInst);
} /* end WSAErrMsg() */

/*
 * Function: InitWndData()
 *
 * Description: get info necessary to center windows, display text in the
 *  window and do scrolling.
 */
void InitWndData (HWND hwnd) {

  hDeskTop = GetDesktopWindow();

  GetWindowRect (hDeskTop, &rcDeskTop);
  
  nDeskTopWidth  = GetSystemMetrics (SM_CXSCREEN);
    nXCenter = nDeskTopWidth/2;
  nDeskTopHeight = GetSystemMetrics (SM_CYSCREEN);
    nYCenter = nDeskTopHeight/2;
     
  nMenuHeight    = GetSystemMetrics (SM_CYMENU);
  nCaptionHeight = GetSystemMetrics (SM_CYCAPTION);
  nScrollWidth   = GetSystemMetrics (SM_CXVSCROLL);
  nScrollHeight  = GetSystemMetrics (SM_CYHSCROLL);

  /* get display buffer */
  hDispBuf = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, 
                         (DWORD) DISP_BUFSIZE);
  if (!hDispBuf) {
    MessageBox (hwnd, "Unable to allocate display buffer", "Error", 
               MB_OK | MB_ICONHAND);
  } else {
    lpDispBuf = GlobalLock(hDispBuf);
  }
  
} /* end InitWndData() */

/*
 * Function: UpdateDispBuf() 
 *
 * Description: Add a line to our display buffer and display it
 *  non rocket science kinda circular buffer in fixed line length
 *  two-demensional text buffer.  lots of corners cut, space wasted.
 */
void UpdateDispBuf(HWND hwnd, LPSTR szTextOut) {
	int i;
	LPSTR szOut;

    /* copy string to our display buffer */
    if (_fstrlen(szTextOut) > DISP_LINE_LEN)  /* truncate if too long */
    	*(szTextOut+DISP_LINE_LEN) = 0;
    szOut = lpDispBuf+(DISP_LINE_LEN*nLastLine);
    _fstrcpy (szOut, szTextOut);
    nLastLine++;
    if (nLastLine >= DISP_NUM_LINES) {
    	/* if we've reached the end of the buffer, we dump half the
    	 *  buffer by copying the bottom half over the top half.
    	 *  This ugly hack is due to laziness; we don't want to
    	 *  maintain circular buffer pointers. */
    	 nLastLine = DISP_NUM_LINES/2;
    	 i = nLastLine * DISP_LINE_LEN;
    	 szOut = lpDispBuf+i;
    	 _fmemcpy (lpDispBuf, szOut, i);
    	 _fmemset (szOut, 0, i);
    }
} /* end UpdateDispBuf() */  


/*
 * Function: UpdateWnd() 
 *
 * Description: WM_PAINT procedure
 */
void UpdateWnd(HWND hwnd) {
	HDC hdc;
	PAINTSTRUCT ps;
	int i, j;
	LPSTR szOut;
    
    /* point to first line in buffer to display */
    if (nLinesToDisplay < nLastLine) {
        /* point at last buffer line, then move it up to first display line */
    	szOut = (lpDispBuf+(DISP_LINE_LEN * nLastLine)) 
    	                  -(DISP_LINE_LEN * nLinesToDisplay);
    } else {
    	/* start display with first line in buffer */
    	szOut = lpDispBuf;
    }
                          
    /* display the text */
	hdc = BeginPaint(hwnd, &ps);

	for (i=0, j=nFirstLine; 
	     *szOut && i<nLinesToDisplay && j<DISP_NUM_LINES; 
	     i++, j++) {
	  TextOut (hdc, 10, (i*nCharHeight), szOut, _fstrlen(szOut));
	  szOut += DISP_LINE_LEN;
	} /* end for() */
	EndPaint(hwnd, &ps);
} /* end UpdateWnd() */

⌨️ 快捷键说明

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