发信人: richwxy(风吟) 
整理人: wenbobo(2002-05-17 17:59:41), 站内信件
 | 
 
 
sizeof(local))) 
              { 
              if (len == SOCKET_ERROR) { /* if could not send bec. */ 
                  if (h_errno == WSAEWOULDBLOCK) 
                      continue; 
                  wshout_err (hOurDlg, WSAGetLastError(), "sendto()"); 
                  break; 
              } /* end: if (len == -1) */ 
          } /* end: if (len = sendto()) */ 
      } /* end for */ 
      come_here: 
      return (bytes_read); 
  } 
 /* eof */ 
 3.4.3.6 tshout.c清单 
 /* 
  * 文件名: TSHOUT.C 
  */ 
 #include "wshout.h" 
 /* MSC Include files: */ 
 #include <stdio.h> 
 #include <io.h> 
 #include <string.h> 
 #include <stdlib.h> 
 #include <time.h> 
 /* Returns the number of bytes written */ 
 long TWriteData(SOCKET hSock, HWND hOurDlg, int send_len) 
 { 
     int counter; 
     static int DataBuffer[BUF_SIZE];      /* Buffer to hold generated data  */ 
     long        total_len = 1024L*1024L;  /* Total # of bytes to generate   */ 
     long        bytes_sent = 0L;          /* Counter of bytes on connection */ 
     int         tmp = 0;                  /* holds count for bytes written  */ 
     long        write_count = 0L;         /* number of times                */ 
     time_t      start, end;               /* variables to hold read timing  */ 
     long        total_time = 0L;          /* variable to hold delta t       */ 
     long ltemp = 0L; 
     extern int run_cancelled; 
     /* What makes shout unique is that it generates data* 
      * in memory (as opposed to accessing the disk).    * 
      * This tests the 'raw' speed of the TCP connection * 
      * as the rate-limiting access time is eliminated.  * 
      * First, generate the data and place it into an    * 
      * array, data_buffer:                              */ 
     for (counter = 0; counter < BUF_SIZE; counter++) 
         DataBuffer[counter] = counter; 
     /* Write data on the descriptor like a banshee, 
      * careful to time the writes and count data 
      * transmitted: 
      */ 
     SetDlgItemText(hOurDlg, IDD_COMMENT, "...Sending TCP Data"); 
     time(&start); 
     while ( bytes_sent < total_len) {   /* while still bytes to send... */ 
         do { 
             ; 
         } while (ShoutBlockingHook()); /* Dispatch messages if any */ 
   
         if (run_cancelled) { 
             WSASetLastError(WSAEINTR); 
             break;      /* Non-blocking mode was cancelled */ 
         } 
   
         tmp = send(hSock, (char FAR*) &DataBuffer, send_len, 0); 
         if (tmp == SOCKET_ERROR) { 
             if (h_errno == WSAEWOULDBLOCK) 
                 continue; 
             else { 
                 wshout_err (hOurDlg, WSAGetLastError(), "send()"); 
             } 
   
             /* Calc. time elapsed & stats about any data sent */ 
             time(&end); 
             /* exit from the while loop */ 
             break; 
   
         } /* end if (tmp == -1) */ 
         write_count++;      /* incr. counter of times written */ 
         bytes_sent += tmp;  /* total # of bytes placed on connection*/ 
         wsprintf(prbuf,"%ld\n",write_count); 
         SetDlgItemText(hOurDlg, IDD_WRITE, (LPSTR) prbuf); 
         wsprintf(prbuf,"%ld\n",bytes_sent); 
         SetDlgItemText(hOurDlg, IDD_SENT, (LPSTR) prbuf); 
     } /* end while */ 
     time(&end); 
     if (total_time = (long) difftime(end, start)) { 
         /* Print the statistics gathered    */ 
         wsprintf((LPSTR)prbuf,"%ld\n",total_time); 
         SetDlgItemText(hOurDlg, IDD_TIME, (LPSTR) prbuf); 
   
         ltemp = write_count/total_time; 
         wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
         SetDlgItemText(hOurDlg, IDD_WRITES,(LPSTR) prbuf); 
   
         ltemp = bytes_sent/total_time; 
         wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
         SetDlgItemText(hOurDlg, IDD_BYTES, (LPSTR) prbuf); 
   
         ltemp = 8 * (bytes_sent/total_time); 
         wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
         SetDlgItemText(hOurDlg, IDD_BITS,  (LPSTR) prbuf); 
     } /* end if (total_time) */ 
   
     /* All done */ 
     SetDlgItemText(hOurDlg, IDD_COMMENT, "...TCP Shout Done"); 
     return bytes_sent; 
 } 
 /* eof */ 
 3.4.3.7 tlisten.c清单 
 /* 
  * 文件名:TLISTEN.C 
  */ 
 #include "wshout.h" 
 /* MSC Include files: */ 
 #include <stdio.h> 
 #include <io.h> 
 #include <string.h> 
 #include <stdlib.h> 
 #include <time.h> 
 /* Returns the number of bytes written */ 
 long TReadData(SOCKET hSock, HWND hOurDlg, int read_len) 
 { 
     static char ReadBuf[BUF_SIZE]; 
     SOCKET hAcceptSock; 
     struct sockaddr_in local;   /* Local machine address structure      */ 
     long total_time = 0L;       /* variable to hold delta t             */ 
     int tmp, len   = 0; 
     int num_reads  = 0; 
     long bytes_read = 0L; 
     long last_time = 0L; 
     long now = 0L; 
     long ltemp; 
     extern long blocking_option; 
     extern int run_cancelled; 
     struct linger AcceptLinger; 
     BOOL running = FALSE; 
     BOOL bTemp = TRUE; 
         SetDlgItemText(hOurDlg, IDD_COMMENT, "Awaiting the TCP Data ..."); 
         SetDlgItemText(hOurDlg, IDD_HNAME, "                      "); 
         tmp = sizeof(local); 
         if (!blocking_option) { 
         hAcceptSock = accept(hSock,(struct sockaddr FAR *)&local, 
                 (int FAR *)&tmp); 
         } 
         else { 
         for (;;) { 
                 do { 
                 ; 
                 } while (ShoutBlockingHook()); /* Dispatch messages if any */ 
                 if (run_cancelled) { 
                 WSASetLastError(WSAEINTR); 
                 break;  /* Non-blocking mode was cancelled */ 
                 } 
                 hAcceptSock = accept(hSock,(struct sockaddr FAR *)&local, 
                                                  (int FAR *)&tmp); 
                 if (hAcceptSock == INVALID_SOCKET) { 
                 if (h_errno == WSAEWOULDBLOCK) 
                         /* Try again */ 
                         ; 
                 else { 
                         /* Fatal error -- pop out. */ 
                         break; 
                 } 
                 } /* end if ((hAcceptSock = .. */ 
                 else { 
                 /* Success -- pop out. */ 
                 break; 
                 } 
         } /* end for */ 
         } /* end else */ 
         if (hAcceptSock == INVALID_SOCKET) { 
         wshout_err (hOurDlg, WSAGetLastError(), "accept()"); 
         return 0; 
         } 
     /* Now, read the data as fast as we can until no more to read */ 
     time(&last_time); 
     do { 
         do { 
             ; 
         } while (ShoutBlockingHook()); /* Dispatch messages while available */ 
   
         if (run_cancelled) { 
             WSASetLastError(WSAEINTR); 
             break;      /* Non-blocking mode was cancelled */ 
         } 
   
         len = recv(hAcceptSock, ReadBuf, read_len, 0); 
         if (len == SOCKET_ERROR) { 
             if (h_errno == WSAEWOULDBLOCK) 
                 continue; 
             else 
                 break; 
         } 
         else if (len == 0) 
             break; 
         num_reads++; 
         bytes_read += len; 
   
         wsprintf((LPSTR)prbuf,"%d\n",num_reads); 
         SetDlgItemText(hOurDlg, IDD_WRITE, (LPSTR) prbuf); 
         wsprintf((LPSTR)prbuf,"%ld\n",bytes_read); 
         SetDlgItemText(hOurDlg, IDD_SENT, (LPSTR) prbuf); 
   
         if (bTemp) {    /* To update our main display once */ 
             /* Do not use wsprintf() or you will add an extra char */ 
              _fmemcpy(prbuf, inet_ntoa(local.sin_addr), 4*sizeof(u_long)); 
              SetDlgItemText(hOurDlg, IDD_HNAME, (LPSTR) prbuf); 
              SetDlgItemText(hOurDlg, IDD_COMMENT, "Reading TCP Data ..."); 
              bTemp = FALSE; 
         } 
   
     } while ((len != 0) || (len != SOCKET_ERROR)); 
     time (&now); 
     if (len == SOCKET_ERROR) { 
         if ((h_errno == WSAESHUTDOWN) || (h_errno == WSAENOTCONN)) { 
             /* nothing available for read. */ 
             wsprintf((LPSTR)prbuf, 
                 "Connection from %s closed.\n",inet_ntoa(local.sin_addr)); 
             SetDlgItemText(hOurDlg, IDD_COMMENT, prbuf); 
         } 
         else { /* Other error */ 
             wshout_err (hOurDlg, WSAGetLastError(), "recv()"); 
         } 
     } 
     else if (len == 0) { 
         /* Other side shut down the connection */ 
         wsprintf((LPSTR)prbuf, 
             "Connection from %s closed.\n",inet_ntoa(local.sin_addr)); 
         SetDlgItemText(hOurDlg, IDD_COMMENT, prbuf); 
     } 
     AcceptLinger.l_onoff = 1; 
     AcceptLinger.l_linger = 0; 
     ret = setsockopt(hAcceptSock, SOL_SOCKET, SO_LINGER, 
                      (char FAR *) &AcceptLinger, sizeof(AcceptLinger)); 
     if (ret == SOCKET_ERROR) { 
         wshout_err (hOurDlg, WSAGetLastError(), "setsockopt()"); 
     } 
     ret = closesocket(hAcceptSock); 
     if (ret == SOCKET_ERROR) { 
         wshout_err (hOurDlg, WSAGetLastError(), "closesocket()"); 
     } 
     total_time = (long) difftime(now, last_time); 
     if (total_time == 0) 
         total_time = 1L;     /* Avoid dividing by zero */ 
     wsprintf((LPSTR)prbuf,"%ld\n",total_time); 
     SetDlgItemText(hOurDlg, IDD_TIME, (LPSTR) prbuf); 
     ltemp = num_reads/total_time; 
     wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
     SetDlgItemText(hOurDlg, IDD_WRITES,(LPSTR) prbuf); 
     ltemp = bytes_read/total_time; 
     wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
     SetDlgItemText(hOurDlg, IDD_BYTES, (LPSTR) prbuf); 
     ltemp = 8 * (bytes_read/total_time); 
     wsprintf((LPSTR)prbuf,"%ld\n", ltemp); 
     SetDlgItemText(hOurDlg, IDD_BITS,  (LPSTR) prbuf); 
     if (bytes_read) { 
         SetDlgItemText(hOurDlg, IDD_COMMENT, "...TCP Listen Done\n"); 
     } /* end: if (bytes_read) */ 
     return (bytes_read); 
 } 
 /* eof */ 
 3.4.3.8 errno.c清单 
 #include <windows.h> 
 #include <winsock.h> 
 /* 
  * 文件名: ERRNO.C 
  */ 
 /* 
  * Function: WSAsperror() 
  * 
  * Description: 
  * 
  * Copies string corresponding to the error code provided 
  * into buf, maximum length len. Returns length actually 
  * copied to buffer, or zero if error code is unknown. 
  * String resources should be present for each error code 
  * using the value of the code as the string ID (except for 
  * error = 0, which is mapped to WSABASEERR to keep it with 
  * the others). The DLL is free to use any string IDs that 
  * are less than WSABASEERR for its own use. 
  * 
  */ 
 int PASCAL FAR WSAsperror (HANDLE hInst,    /* Instance Handle */ 
                            int errorcode,   /* WSA Error Number */ 
                            char far * buf,  /* Buffer for error string */ 
                            int len)         /* Length of buffer */ 
 { 
         int err_len;   /* length of error text */ 
         if (errorcode == 0)             /* If error passed is 0, use the */ 
                 errorcode = WSABASEERR; /*  base resource file number */ 
         if (errorcode < WSABASEERR)     /* If invalid Error code */ 
                 return 0;               /*  return string length of zero */ 
   
         /* error string from the table in the Resource file into buffer */ 
         err_len = LoadString(hInst,errorcode,buf,len); 
   
         return (err_len);  /* return length of error string retrieved */ 
   
 }  /* end WSAsperror() */ 
 /* eof */ 
 3.4.3.9 resolve.c清单 
 /* 
  * 文件名: RESOLVE.C 
  */ 
 #include "wshout.h" 
 /* MSC Include files: */ 
 #include <stdio.h> 
 #include <io.h> 
 #include <string.h> 
 #include <stdlib.h> 
 #include <time.h> 
 SOCKET 
 ResolveAndConnectHost(LPSTR lpHostName,HWND hOurDlg,int iproto, int iSockPor 
 t) 
 { 
     struct      hostent FAR *host_ptr;          /* Ptr to the host name */ 
     struct      sockaddr_in dest;               /* Addr of target host  */ 
     SOCKET      hSock;                          /* The socket to create */ 
     int iSockType; 
     extern int iTCP; 
     extern int iUDP; 
     /* Internet family addressing */ 
     dest.sin_family = PF_INET; 
     if (iproto == iTCP) { 
         iSockType = SOCK_STREAM; 
     } 
     else if (iproto == iUDP) { 
         iSockType = SOCK_DGRAM; 
     } 
     else { 
         return (SOCKET) -1;             /* Unknown protocol */ 
     } 
     /* default port to connect to. Must be in network byte order        */ 
     dest.sin_port = htons((u_int) iSockPort); 
     SetDlgItemText(hOurDlg, IDD_COMMENT,"Resolving hostname..."); 
     /* Resolve the host name */ 
     host_ptr = gethostbyname(lpHostName); 
     if (host_ptr == NULL) { 
         wshout_err (hOurDlg, WSAGetLastError(), "gethostbyname()"); 
         return (SOCKET) -1; 
     } 
     /* Patch host address into struct describing conn: */ 
     bcopy(host_ptr->h_addr,&dest.sin_addr,host_ptr->h_length); 
     /* Allocate a network (socket) descriptor:      */ 
     hSock = socket(PF_INET, iSockType, 0); 
     if (hSock == INVALID_SOCKET) { 
         wshout_err (hOurDlg, WSAGetLastError(), "socket()"); 
         return (SOCKET) -1; 
     } 
     /* Start connection process to host described in 'dest'     * 
      * struct. 
      */ 
 
     SetDlgItemText(hOurDlg, IDD_COMMENT, "Connecting ..."); 
     ret=connect(hSock,(struct sockaddr FAR *)&dest,sizeof(dest)); 
     if (ret == SOCKET_ERROR) { 
         wshout_err (hOurDlg, WSAGetLastError(), "connect()"); 
         closesocket(hSock); 
   
   
 -- 
  | 
 
 
 |