TReadData;TWriteData;UReadData;UWriteData

来源:互联网 发布:java视频网站开发 编辑:程序博客网 时间:2024/06/06 15:58
long COnlyClientDlg::TReadData(SOCKET hSock, int read_len){int ret;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;struct linger AcceptLinger;BOOL running = FALSE;BOOL bTemp = TRUE;SetDlgItemText( IDD_COMMENT, "Awaiting the TCP Data ...");SetDlgItemText( 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 {//. 69 ./* Success -- pop out. */break;}} /* end for */} /* end else */if (hAcceptSock == INVALID_SOCKET) {wshout_err ( 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;elsebreak;}else if (len == 0)break;num_reads++;bytes_read += len;wsprintf((LPSTR)prbuf,"%d\n",num_reads);SetDlgItemText( IDD_WRITE, (LPSTR) prbuf);wsprintf((LPSTR)prbuf,"%ld\n",bytes_read);SetDlgItemText( IDD_SENT, (LPSTR) prbuf);//. 70 .if (bTemp) { /* To update our main display once *//* Do not use wsprintf() or you will add an extra char */memcpy(prbuf, inet_ntoa(local.sin_addr), 4*sizeof(u_long));SetDlgItemText( IDD_HNAME, (LPSTR) prbuf);SetDlgItemText( 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( IDD_COMMENT, prbuf);}else { /* Other error */wshout_err ( 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( 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 (WSAGetLastError(), "setsockopt()");}ret = closesocket(hAcceptSock);if (ret == SOCKET_ERROR) {wshout_err ( WSAGetLastError(), "closesocket()");}//. 71 .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( IDD_TIME, (LPSTR) prbuf);ltemp = num_reads/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_WRITES,(LPSTR) prbuf);ltemp = bytes_read/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BYTES, (LPSTR) prbuf);ltemp = 8 * (bytes_read/total_time);wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BITS, (LPSTR) prbuf);if (bytes_read) {SetDlgItemText( IDD_COMMENT, "...TCP Listen Done\n");} /* end: if (bytes_read) */return (bytes_read);}long COnlyClientDlg::TWriteData(SOCKET hSock, 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;/* 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(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 ( 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( IDD_WRITE, (LPSTR) prbuf);wsprintf(prbuf,"%ld\n",bytes_sent);SetDlgItemText( 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( IDD_TIME, (LPSTR) prbuf);ltemp = write_count/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_WRITES,(LPSTR) prbuf);ltemp = bytes_sent/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BYTES, (LPSTR) prbuf);ltemp = 8 * (bytes_sent/total_time);wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText(IDD_BITS, (LPSTR) prbuf);} /* end if (total_time) *//* All done */SetDlgItemText( IDD_COMMENT, "...TCP Shout Done");return bytes_sent;}long COnlyClientDlg::UReadData(SOCKET hSock, int read_len){static char ReadBuf[BUF_SIZE];static char SendBuf[512];//. 62 .struct sockaddr_in local; /* Local machine address structure */int i; /* General purpose return code */long total_time = 0L; /* variable to hold delta t */int tmp, len = 0;int num_reads = 0;long bytes_read = 0L;long last_time, now, timeout = 15L;long ltemp;BOOL bTemp = TRUE;SetDlgItemText( IDD_COMMENT, "Awaiting the UDP Data ...");SetDlgItemText( IDD_HNAME, " ");time(&now); time(&last_time);while (last_time + timeout > now) {time(&now);tmp = sizeof(local);do {;} while (ShoutBlockingHook()); /* Dispatch messages while available */if (run_cancelled) {WSASetLastError(WSAEINTR);break; /* Non-blocking mode was cancelled */}len = recvfrom(hSock, ReadBuf, read_len, 0,(struct sockaddr *) &local, &tmp);if (len == SOCKET_ERROR) {if (h_errno == WSAEWOULDBLOCK) {/* if no data, read again */continue;} /* end: if (errno == WSAEWOULDBLOCK) */else {if (bytes_read) {wshout_err ( WSAGetLastError(), "recvfrom()");}} /* end else */break;} /* end: if (len == SOCKET_ERROR) *///. 63 .if (bTemp) { /* To update our main display once *//* Do not use wsprintf() or you will add an extra char */memcpy(prbuf, inet_ntoa(local.sin_addr), 4*sizeof(u_long));SetDlgItemText( IDD_HNAME, (LPSTR) prbuf);SetDlgItemText( IDD_COMMENT, "Reading UDP Data ...");bTemp = FALSE;}num_reads++;if (len != SOCKET_ERROR)bytes_read += len;/* Print the statistics gathered */wsprintf((LPSTR)prbuf,"%d\n",num_reads);SetDlgItemText( IDD_WRITE, (LPSTR) prbuf);wsprintf((LPSTR)prbuf,"%ld\n",bytes_read);SetDlgItemText( IDD_SENT, (LPSTR) prbuf);time(&last_time);} /* end: while */total_time = timeout;wsprintf((LPSTR)prbuf,"%ld\n",total_time);SetDlgItemText(IDD_TIME, (LPSTR) prbuf);ltemp = num_reads/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_WRITES,(LPSTR) prbuf);ltemp = bytes_read/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BYTES, (LPSTR) prbuf);ltemp = 8 * (bytes_read/total_time);wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BITS, (LPSTR) prbuf);if (bytes_read) {SetDlgItemText( IDD_COMMENT, "...UDP Listen Done\n");} /* end: if (bytes_read) *///. 64 .else {wsprintf((LPSTR)prbuf, "Timed out. No data received.\n");SetDlgItemText( IDD_COMMENT, prbuf);goto come_here;} /* end: else *//* send reply to 'client' */wsprintf((LPSTR)prbuf,"Replied to %s\n", inet_ntoa(local.sin_addr));SetDlgItemText( IDD_COMMENT, prbuf);for (i=0; i<10; ++i) {sprintf(SendBuf, "Rec'd %ld bytes.\n", bytes_read);if(len = sendto(hSock, SendBuf, sizeof(SendBuf), 0,(struct sockaddr FAR *)&local,sizeof(local))){if (len == SOCKET_ERROR) { /* if could not send bec. */if (h_errno == WSAEWOULDBLOCK)continue;wshout_err ( WSAGetLastError(), "sendto()");break;} /* end: if (len == -1) */} /* end: if (len = sendto()) */} /* end for */come_here:return (bytes_read);}long COnlyClientDlg::UWriteData(SOCKET hSock, int send_len){int counter;static int DataBuffer[BUF_SIZE]; /* Buffer to hold generated data */static char ReplyBuffer[512]; /* Buffer to hold any reply rcvd */long bytes_sent = 0L; /* Counter of bytes on connection */long total_len = 1024L*1024L; /* Total # of bytes to generate */time_t start, end; /* variables to hold read timing */long total_time = 0L; /* variable to hold delta t */long write_count = 0L; /* number of times */long tmp = 0L; /* holds count for bytes written */long ltemp = 0L;int i_temp;struct sockaddr_in dest; /* Destination machine address structure *//* 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( IDD_COMMENT, "Sending UDP 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) /* if no data, read again */continue;else {wshout_err (WSAGetLastError(), "send()");}/* Calc. time elapsed & stats about any data sent */time(&end);if (total_time = (long) difftime(end, start)) {/* Print the statistics gathered */wsprintf((LPSTR)prbuf,"%ld\n",write_count);SetDlgItemText( IDD_WRITE, (LPSTR) prbuf);wsprintf((LPSTR)prbuf,"%ld\n",bytes_sent);SetDlgItemText(IDD_SENT, (LPSTR) prbuf);wsprintf((LPSTR)prbuf,"%ld\n",total_time);SetDlgItemText( IDD_TIME, (LPSTR) prbuf);ltemp = write_count/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText(IDD_WRITES,(LPSTR) prbuf);ltemp = bytes_sent/total_time;wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BYTES, (LPSTR) prbuf);ltemp = 8 * (bytes_sent/total_time);wsprintf((LPSTR)prbuf,"%ld\n", ltemp);SetDlgItemText( IDD_BITS, (LPSTR) prbuf);/* exit from the while loop */break;} /* end if (total_time) */write_count++; /* incr. counter of times written */bytes_sent += tmp; /* # of bytes placed on connection */wsprintf((LPSTR)prbuf,"%ld\n",bytes_sent);SetDlgItemText( IDD_SENT, (LPSTR) prbuf);} /* end if (tmp == -1) */write_count++; /* incr. counter of times written */bytes_sent += tmp; /* # of bytes placed on connection */wsprintf((LPSTR)prbuf,"%ld\n",write_count);SetDlgItemText( IDD_WRITE, (LPSTR) prbuf);wsprintf((LPSTR)prbuf,"%ld\n",bytes_sent);SetDlgItemText( IDD_SENT, (LPSTR) prbuf);} /* end while *//* Look for a reply ... NOTE: most hosts won't give* a 'reply', done to illustrate communication between* sockets. Our ulisten example will give a reply though.*/SetDlgItemText( IDD_COMMENT, "Waiting for reply from server..\n");while (1) {tmp = sizeof(dest);i_temp = recvfrom(hSock,(char FAR *) &ReplyBuffer,sizeof(ReplyBuffer),0, (struct sockaddr *) &dest, (int FAR *) &tmp);if (i_temp == SOCKET_ERROR) {if (h_errno == WSAEWOULDBLOCK) /* if no data, read again */continue;else {/* any error besides these. just punt */wshout_err ( WSAGetLastError(), "recvfrom()");}break;} /* end if (i_temp == SOCKET_ERROR) *//* else got a reply ...*/wsprintf((LPSTR)prbuf, "Server: %s\n", (LPSTR) ReplyBuffer);SetDlgItemText( IDD_COMMENT, prbuf);break;} /* end while(1) *//* All done */return bytes_sent;}