




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第三章WindowsSockets1.1WindowsSockets1.1编程的具体OSforPC2.0(BSDUNIX微机版)环境下调试通过。socket(socket(bind(readfrom(
close(close(3-1
WindowsSockets异步选择函数WSAAsyncSelect()允许应用程序提名一个或多个感的网络事类函数.这些函数是对BSD标准函数的扩充.函数WSACancelAsyncRequest()允许用户中由于WindowsSockets的服务是以动态连接库WINSOCK.DLL形式实现的,所以必须要先调用WSAStartup()函数对WindowsSocketsDLL进行初始化,协商WINSOCK的版本WindowsSocketsDLL的使用,并释放资源,以备下一次使用.的使用.用法及详细说明参见第5.3.7.WindowsSocketsUNIXUNIX下直接运行,客户机在Windows下运行简单,只有一段程序,下面简要解释一下.相关"的三元组(协议,本机地址,本地端)来描述,而一个完整的进程通信实例则需要一个被称为"相关"的五元组(协议,本机地址,本地端,远端主机地址,远端端)来bind(s,(structsockaddr*)&server,ns=accept(s,(structsockaddr*)&,相同属性的新的套接口.也是一个sockaddr_in结构,连接建立时填入请求连接的套接口MSG_DONTROUTE和MSG_OOB,0表示缺省.客户端是在Windows上运行的,使用了一些WindowsSockets的扩展函数,稍微复杂一首先是Wii)中建好窗口即向窗口函自义的WMUR消息,WURWrp()WiwscksD:hostaddr=gethostbyname(server_address);if((s=socket(AF_INET,SOCK_STREAM,0))=={AlertUser(hWnd,"SocketFailed");return(FALSE);}if(!SetSelect(hWnd,FD_CONNECT))returnBOOLSetSelect(HWNDhWnd,long{if(WSAAsyncSelect(s,hWnd,UM_SOCK,lEvent)=={AlertUser(hWnd,"WSAAsyncSelectFailure.");return(FALSE);}return}便是非阻塞调用.进程发出连接请求后就不管了,当连接建立好后,WINSOCKDLL自动发一接引起的,这样,就会执行相应的程序段,同样调用SetSelect()来提名FD_WRITE.希用SetSelect()来提名FD_READ,希望在套接口可接收数据是接到消息.在收到中的close())来关闭套接口,再调用WSACleanup()终止WindowsSocketsDLL,并释放资源.程序程序
ITEM #include<alloc.h>#include<mem.h> HANDLEcharbuffer[1024];SOCKETs=0;structhostentfar*hostaddr;structhostenthostnm;structserventfar*sp;intcount=longFARPASCALProc(HWNDhWnd,unsignedmessage,UINTram,LONGlParam);voidAlertUser(HWNDhWnd,char*message);BOOL(HWNDBOOLReceivePacket(HWNDhWnd);BOOLSetSelect(HWNDhWnd,longlEvent);BOOLSendPacket(HWNDhWnd,intlen);{MSGif(!hPrevInstance)return(FALSE);hInst=hWnd=CreateWindow(" Class","WindowsECHO if(!hWnd)return(FALSE);PostMessage(hWnd,WM_USER,( while(GetMessage(&msg,NULL,NULL,NULL)){}}{char*szAppName="//fillinwindowclass =hInstance; =Proc; =LoadCursor(NULL,IDC_ARROW); =LoadIcon(hInstance,NULL); ="";WndClass.hbrBackground=GetStockObject(WHITE_BRUSH); =CS_HREDRAW|CS_VREDRAW; =0; =//registerthe} ram,LONG{intStatus;{case{ Status=WSAStartup(VersionReqd,lpmyWSAData);if(Status!={}if(hostaddr==NULL){}}switch {caseif(!{}case }caseUM_SOCK:{casecaseif{}casefor(i=0;i<1024;ibuffer[i]=(char)'A'+i%26;length=1024;{}caseif(WSAAsyncSelect(s,hWnd,0,0)==SOCKET_ERROR)AlertUser(hWnd,"WSAAsyncSelectFailed.\n");{}}caseWM_DESTROY: ram,}}{} (HWND{ FAR dst_addr.sin_port=htons(USERPORT);if((s=socket(AF_INET,SOCK_STREAM,0))=={return(FALSE);}return(FALSE);connect(s,(structsockaddrFAR*)&dst_addr,sizeof(dst_addr));return(TRUE);}{HDCintlength;intif((length=recv(s,lpBuffer,1024,0))==SOCKET_ERROR)return(FALSE);if(length==if(hDc={i1=wsprintf((LPSTR)line1,"TCPEchoNo.%d",count);i2=wsprintf((LPSTR)line2,"Receive%dbytes",length);TextOut(hDc,10,2,(LPSTR)line1,i1);TextOut(hDc,10,42,(LPSTR)line3,i3);ReleaseDC(hWnd,hDc);}}{{}}{intif((length=send(s,lpBuffer,len,0))==SOCKET_ERROR)return(FALSE);if(length!={return(FALSE);}}程序#include<sys/types.h>#include<sys/mntent.h>#include<netinet/in.h>#include<sys/socket.h>#include<arpa/inet.h>{charstructsockaddr_in;ints;intpktlen;{}bzero((char*)&server,sizeof(server));server.sin_family=AF_INET;server.sin_port=htons(USERPORT);{}if{} {}for{if((pktlen={}
if(pktlen=={}
if{}}
}WdwsSes编程TP或D无连接服务(数据报套接口),如何进行阻塞或非阻塞方式的套接口操作等等,这些都是经wsoutWnoses该程序在FTP公司的PCTCPresolve.c客户机/在编译本程序时,笔者用的是BC3.1,只需做一个PRJ工程文件,将上述.c文件及winsock.libwinsock.hinclude或当前中,winsock.libwinsock.dllimplib工具来建立。如果读者使用其他的编译器,可自行作相应的wwCUDP源程序及注wshout.c文件名/*MSCIncludefiles:*/#include<stdio.h>#include<io.h>#include<string.h>#include<stdlib.h>#include<time.h>#defineMAJOR_VERSION#defineMINOR_VERSION#defineWSA_MAKEWORD(x,y)((y)*256+(x))/*HI:Minor,LO:Major /*进程实例句柄*/HWNDhOurWnd; /*主窗口句柄*/HWND /*主框句柄int /*char /*SOCKETsd; /*用于连接的套接口描述字*/longtemporary_option =0L; /*缺省为阻塞模式*/longblocking_option =0L; /*阻塞模式的全局标识*/intrun_cancelled =0;/*指示何时按下了取消按钮*/intlen=1024; /*一次写的字节数*/ running=FALSE;/*程序的运行状态*/constintiTCP=1; /*指定为TCPShout*/constintiUDP=2; /*指定为UDPShout*/intiProto=1; /*缺省为TCPShout*/int =inttemporary_protocol 1;*Settings()*/intiShout=1;intiListen=inti OrServer=1;/*缺省为Shout(客户机)*/intt OrServer=1;/*在Settings()中使用*/charHostModeBuf[20];/**/WORDVersionReqd;int{MSGBOOLifreturn(NULL);hInst="WindowsShout",WS_OVERLAPPEDWINDOW|WS_SYS|WS_MINIMIZEBOX,if(!hWnd)hOurWnd=TranslateMessage(&msg)**/}}{HANDLEhMemory;BOOLbSuccess;hMemory=LocalAlloc(LPTR,sizeof(WNDCLASS));pWndClass=(PWNDCLASS)LocalLock(hMemory);pWndClass->hCursor=LoadCursor(NULL,IDC_ARROW);pWndClass->hIcon=LoadIcon(hInstance,(LPSTR)"SHOUT");pWndClass->lpszName=(LPSTR)"Main";pWndClass->lpszClassName=(LPSTR)"Main";pWndClass->hInstance=hInstance;bSuccess=RegisterClass(pWndClass); return(bSuccess);}longFARPASCALShoutWndProc(HWNDhWnd,WORD ram,{(message){case/*PutupthedialogboxDialogBox(hInst,(LPSTR)"Ma PostMessage(hWnd,WM_DESTROY,0,0L);caseWM_DESTROY: ram,}}BOOLFARPASCALDialogProc(HWNDhOurDlg,WORDmessage,WORDram,LONG{FARPROClpProcAbout;longlret;char /*case/*选择缺省主机*/ /*框句柄*/ /*向何处发送msg*/ /*选择字符*/ /*附加信息*/MAKELONG(0,0x7fff)); /*全部内容*//*hMainDlg=hOurDlg;/*保存自己的窗口句柄*/wMajorVersion=MAJOR_VERSION;wMinorVersion=MINOR_VERSION;lpmyWSAData=(LPWSADATA)_calloc(1,sizeof(WSADATA));ret=WSAStartup(VersionReqd,lpmyWSAData);if(ret!=}caseWM_CLOSE: switch(ram)caseIDD_CONNECT: /**/caseIDM_START:/*Start*/run_cancelled=FALSE;/**/if(running){}running=TRUE;/*running=FALSE;}ifsdSOCKET_ERROR)**/running=FALSE;}}elseif(sd==SOCKET_ERROR){}}/*SettheI/Omodeofthesocket*/if(blocking_option){lret /*}elselret0L;*} /*SHOUT/**/if(iProto==iTCP)lret=TWriteData(sd,hOurDlg,len);else/*UDP*/lret=UWriteData(sd,hOurDlg,}else{ /*LISTEN*/if(iProto==else/*UDP*/}case/*run_cancelled=TRUE;if(ret==SOCKET_ERROR)if(h_errno==WSAENETDOWN)/*closewhatisleftoftheconnection*/}}}caseret=if(ret==SOCKET_ERROR&&h_errno=="Datatransferinprogress.\nStoptransferfirst.","WndProc()",MB_OK|break;*}_((charNEAR*)lpmyWSAData);EndDialog(hOurDlgTRUE**/caselpProcAbout=MakeProcInstance(About,hInst);caselpProcSettings=MakeProcInstance(Settings,hInst);}/*switch( }/*switch(message)*/returnFALSE;}/*此函数处理About框 ram,LONG{charcaseSetDlgItemText(hDlg,IDA_APP_VERSION,(LPSTR)tempBuf);SetDlgItemText(hDlg,IDA_DLL_VERSION,(LPSTR)tempBuf);if(ram=={EndDialog(hDlg,TRUE);return(TRUE);}}}/*此函数处理Settings框 {intbuffer_len=int =case/*Selectadefaultsend()bufferlength*//*Selectadefaultportnumber*/if(i OrServer==iShout) /**/CheckThisProgBoxOn(hDlg,IDS_);if(iProto==iTCP) /**/,if(!blocking_option) /**/CheckThisBoxOn(hDlg,IDS_BLOCK); /*dialoghandle /*wheretosendmsg /*selectcharacters*/ /*additionalinfo MAKELONG(0,0x7fff)); /*entirecontents /*dialoghandle /*wheretosendmsg /*selectcharacters*/ /*additionalinfo MAKELONG(0,0x7fff)); /*entirecontents return(TRUE); ram){case/*USerhassettoShout*/tOrServer=SetDlgItemText(hMainDlg,IDD_HNAME,"");case/*USerhassettoListen*/ OrServer=iListen;SetDlgItemText(hMainDlg,IDD_HNAME,"[Hit'Start']");case/*USerhassettoTCP*/temporary_protocol=iTCP;/*USerhassettoUDP*/temporary_protocol=iUDP;case/*Userhassettoblockingmode*/temporary_option=0L;case/*Userhassettononblockingmode*/temporary_option=1L;case/*buffer_len=GetDlgItemInt(hDlg,IDS_BUFFLEN,NULL,0);if(buffer_len==0||buffer_len>8192){MessageBox(hOurWnd,"Bufferlengthmustbebetween1and8K",}if(port_no==0){} iPortNo=port_no;blocking_option=temporary_option; iOrServer=tOrServer;case/**/EndDialog(hDlg,TRUE);return(TRUE);}}}{}
{caseIDS_BLOCK:caseIDS_NOBLOCK:}{{caseIDS_TCP:caseIDS_UDP:}}{caseIDS_:/*Shout*/CheckDlgButton(hDlg,IDS_,1);caseIDS_SERVER:/*Listen*/CheckDlgButton(hDlg,IDS_,0);}}{MSGmsg; /*letsuspullmessagesviaPeekMessage*/intret=PeekMessage(&msg,NULL,0,0,PM_REMOVE);if(ret)}}char_calloc(nelem,elsize){HANDLEif((hMem=LocalAlloc(LPTR,size))==NULL)return(char*)0;{Local return(char*)0;}return(char*)}_(void{}{ SetDlgItemText(hOurDlg,IDD_SENT,(LPSTR)prbuf);SetDlgItemText(hOurDlg,IDD_TIME,(LPSTR)prbuf);SetDlgItemText(hOurDlg,IDD_BYTES,(LPSTR)prbuf);SetDlgItemText(hOurDlg,IDD_BITS,(LPSTR)prbuf);}描述voidwshout_err(HWNDhOurDlg,/*框的窗口句柄*/intwsa_err, /*WinSock错误代码*/charfar*err_prefix)/*错误前缀字符串*/{char /*/*/*合并错误描述字符串与用户错误前缀字符串*//*在框中显示错误文本 /*eofwshout.h文件名/*Windows3.0*/#include<windows.h>#define_INC_WINDOWS#include<winsock.h>#ifdefcplusplusextern"C"{#endif cplusplus/*WSHOUT.C /*Instancehandle*/ HWNDhOurWnd; /*MainWindowHandle*/ /*workvariable*/ char /*菜单 #defineIDM_ABOUT #defineIDM_STOP103#defineIDM_EXIT104 /*框控制IDs*/#defineIDD_HNAME #defineIDD_CONNECT #defineIDD_CANCELIDCANCEL #defineIDD_BITS #defineIDD_COHOST/*“Settings”框控制IDs*/#defineIDS_BUFFLEN300#defineIDS_PORTNO#defineIDS_BLOCK302#defineIDS_NOBLOCK304 #defineIDS_#defineIDS_SERVER/*“About”框控制IDs*/#defineIDA_COPYRIGHT /*程序控制 /*全局变量 SOCK_DISCARD /*usetheUDPttytstsourceportfortest#defineSOCK_SHOUT32766 /*TCPportusedforSHOUT&LISTEN #defineBUF_SIZE8192/*longFARPASCALShoutWndProc(HWND,WORD,WORD,LONG);BOOLFARPASCALAbout(HWND,WORD,WORD,LONG);BOOLFARPASCALDialogProc(HWND,WORD,WORD,LONG);BOOLFARPASCALSettings(HWND,WORD,WORD,LONG);BOOLInitApp(HANDLE);voidCheckThisProgBoxOn(HWND,int);voidClearBoxes(HWND);SOCKETGetSocketAndBind(HWND,int,int);longUWriteData(SOCKET,HWND,int);longUReadData(SOCKET,HWND,int);longTWriteData(SOCKET,HWND,int);longTReadData(SOCKET,HWND,int);intShoutBlockingHook(void);intPASCALFARWSAsperror(HANDLE,int,charfar*,int);voidwshout_err(HWND,int,charfar*);#definebcopy(a,b,c)_fmemcpy(b,a,c)char*_calloc(unsigned,unsigned);}#endif cplusplus/*eofwshout.rc文件名#include<winsock.h>#include"wshout.h"
ITEM"Sto&p",IDM_STOPITEMSEPARATORITEM"E&xit",ITEMSEPARATORABOUTBOXDIALOG22,17,144,STYLEDS_MODALFRAME|WS_OVERLAPPED|WS_CAPTION|WS_SYSCTEXT"WindowsShout",-1,29,5,85,CTEXT"Version",-1,46,13,33,8,SS_CENTER|CTEXT"WINSOCK.DLL\nFTPSoftware,Inc.\nCopyright1993",IDA_COPYRIGHT,38,40,68,25,SS_CENTER|WS_GROUPCTEXT"Version",-1,46,67,33,8,SS_CENTER|CTEXT"num",IDA_DLL_VERSION,79,67,18,8,SS_CENTER|WS_GROUPCONTROL"OK",1,"BUTTON",BS_DEFPUSHBUTTON|WS_GROUP|WS_TABSTOP,56,82,32,ICON"SHOUT",-1,11,8,16,CONTROL"num",IDA_APP_VERSION,"STATIC",SS_LEFT|WS_GROUP,79,13,8
CONTROL"using",-1,"STATIC",SS_CENTER|WS_GROUP,55,26,30,SettingsDialogDIALOG9,16,172,117CAPTION"Settings"STYLEDS_MODALFRAME|WS_POPUP|WS_CAPTION|WS_SYSCONTROL" send/recv\nBuffer&length",-1,"STATIC",SS_LEFT|WS_GROUP,84,8,48,20CONTROL"&Portnumber",-1,"STATIC",SS_LEFT|WS_GROUP,84,31,48,10 WS_TABSTOP,100,61,56,CONTROL"&TCP",IDS_TCP,"BUTTON",BS_AUTOCHECKBOX|WS_TABSTOP,20,60,41,12 WS_TABSTOP,19,15,35,12 WS_TABSTOP,19,26,35,12CONTROL"&UDP",IDS_UDP,"BUTTON",BS_AUTOCHECKBOX|WS_TABSTOP,20,72,41,12CONTROL"&Nonblocking",IDS_NOBLOCK,"BUTTON",BS_AUTOCHECKBOX|WS_TABSTOP,100,73,56,12CONTROL"O.K.",IDOK,"BUTTON",BS_PUSHBUTTON|WS_TABSTOP,40,95,CONTROL"Cancel",IDCANCEL,"BUTTON",BS_PUSHBUTTON|WS_TABSTOP,95,37,CONTROL"",IDS_BUFFLEN,"EDIT",ES_CENTER|WS_BORDER|WS_TABSTOP,130,11,36,12CONTROL"",IDS_PORTNO,"EDIT",ES_CENTER|WS_BORDER|WS_TABSTOP,130,29,36,12CONTROL"I/OMode",239,"button",BS_GROUPBOX,90,49,70,CONTROL"ProgramMode",241,"button",BS_GROUPBOX,6,7,70,MalogDIALOG17,32,163,MainSTYLEDS_ABSALIGN|WS_OVERLAPPED|WS_CAPTION|WS_SYS|CONTROL"",IDD_HNAME,"EDIT",ES_CENTER|WS_BORDER|WS_GROUP|WS_TABSTOP,62,9,93,12CONTROL"",IDD_WRITE,"STATIC",SS_CENTER|SS_NOPREFIX|WS_BORDER,7,95,45,11CONTROL"",IDD_SENT,"STATIC",SS_CENTER|WS_BORDER,59,95,45,CONTROL"",IDD_TIME,"STATIC",SS_CENTER|WS_BORDER,111,95,45,CONTROL"",IDD_WRITES,"STATIC",SS_CENTER|WS_BORDER,7,120,45,CONTROL"",IDD_BYTES,"STATIC",SS_CENTER|WS_BORDER,59,120,45,CONTROL"",IDD_BITS,"STATIC",SS_CENTER|WS_BORDER,111,120,45,CONTROL"writes[reads]",105,"STATIC",SS_CENTER|WS_GROUP,3,85,52,CONTROL"bytes",105,"STATIC",SS_CENTER|WS_GROUP,61,85,42,CONTROL"time(sec)",105,"STATIC",SS_CENTER|WS_GROUP,111,85,45,CONTROL"bits/sec",105,"STATIC",SS_CENTER|WS_GROUP,113,111,42,CONTROL"", MENT,"STATIC",SS_CENTER|WS_BORDER|WS_GROUP,9,68,146,11CONTROL"&Start",IDOK,"BUTTON",BS_PUSHBUTTON|WS_TABSTOP,6,32,CONTROL"Sto&p",IDCANCEL,"BUTTON",BS_PUSHBUTTON|WS_TABSTOP,32,32,CONTROL"E&xit",IDM_EXIT,"BUTTON",BS_PUSHBUTTON|WS_TABSTOP,125,32,32,20CONTROL"",-1,"static",SS_BLACKFRAME,0,60,163,SHOUTICON "[0]No "[10009]Badfile "[10014]Bad "[10024]Toomanyopenfiles" "[10035]Operationwouldblock" "[10036]Operationnowinprogress" "[10037]Operationalreadyinprogress" "[10038]Socketoperationonnon-socket" "[10039]Destinationaddressrequired" "[10040]Messagetoolong" "[10041]Protocolwrongtypeforsocket" "[10042]Badprotocoloption"WSAEPROTONOSUPPORT,"[10043]Protocolnotsupported"WSAESOCKTNOSUPPORT,"[10044]Sockettypenotsupported" "[10045]Operationnotsupportedonsocket" "[10046]Protocolfamilynotsupported" "[10047]Addressfamilynotsupportedbyprotocolfamily" "[10048]Addressalreadyinuse" "[10049]Can'tassignrequestedaddress" "[10050]Networkisdown" "[10051]Networkisunreachable" "[10052]Netdroppedconnectionorreset" "[10053]Softwarecausedconnectionabort" "[10054]Connectionresetbypeer" "[10055]Nobufferspaceavailable" "[10056]Socketisalreadyconnected" "[10057]Socketisnotconnected" "[10058]Can'tsendaftersocketshutdown" "[10059]Toomanyreferences,can'tsplice" "[10060]Connectiontimedout" "[10061]Connectionrefused" "[10062]Toomanylevelsofsymboliclinks" "[10063]Filenametoolong" "[10064]Hostisdown" "[10065]NoRoutetoHost" "[10066]Directorynotempty" "[10067]Toomanyprocesses" "[10068]Toomanyusers" "[10069]DiscQuota "[10070]StaleNFSfilehandle" "[10071]Toomanylevelsofremoteinpath" "[10091]NetworkSubSystemisunavailable" "[11001]Hostnotfound" "[11002]Non-AuthoritativeHostnotfound" "[11003]Non-Recoverableerrors:FORMERR,REFUSED, "[11004]Validname,nodatarecordofrequested/*eofushout.c文件名/*MSCIncludefiles:*/#include<stdio.h>#include<io.h>#include<string.h>#include<stdlib.h>#include<time.h>{intstaticintDataBuffer[BUF_SIZE]; /*Buffertoholdgenerateddata charReplyBuffer[512]; /*Buffertoholdanyreplyrcvd*/longbytes_sent=0L; /*Counterofbytesonconnection longtotal_len=1024L*1024L; /*Total#ofbytestogenerate time_tstart,end; /*variablestoholdreadtiming*//*variabletoholddeltaint First,generatethedataandceitintoanarray, for(counter=0;counter<BUF_SIZE;counter++)DataBuffer[counter]=counter;/*Writedataonthedescriptorlikea time(&start);while(bytes_sent<total_len){/*whilestillbytestosend*/do{;if{ }tmp=send(hSock,(charFAR*)&DataBuffer,send_len,0);if(tmp==SOCKET_ERROR){if(h_errno==WSAEWOULDBLOCK)/*ifnodata,readagain*/else}/*Calc.timeelapsed&statsaboutanydatasent*//*Printthestatisticsgathered ltemp=write_count/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=bytes_sent/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=8*(bytes_sent/total_time);wsprintf((LPSTR)prbuf,"%ld\n",ltemp);SetDlgItemText(hOurDlg,IDD_BITS,(LPSTR)prbuf);/*exitfromthewhileloop*/}/*endif(total_time) /*incr.counteroftimeswritten*/bytes_sent+=tmp; /*#ofbytescedonconnection*/SetDlgItemText(hOurDlg,IDD_SENT,(LPSTR)prbuf);}/*endif(tmp==-1) /*incr.counteroftimeswritten*/bytes_sent+=tmp; /*#ofbytescedonconnection*/SetDlgItemText(hOurDlg,IDD_WRITE,(LPSTR)prbuf);}/*endwhile/*Lookforareply...NOTE:mosthostswon't MENT,"Waitingforreplyfromserver..\n");while(1){tmp=0,(structsockaddr*)&dest,(intFAR*)&tmp);if(h_errno==WSAEWOULDBLOCK)/*ifnodata,readagain*/else/*anyerrorbesidesthese.justpunt}}/*endif(i_temp==SOCKET_ERROR)/*elsegotareply MENT,prbuf);}/*endwhile(1)/*Alldone*/}/*eofulisten.c文件名/*MSCIncludefiles:*/#include<stdio.h>#include<io.h>#include<string.h>#include<stdlib.h>#include<time.h>{staticchar staticchar structsockaddr_inlocal;/*Localmachineaddressstructure*/inti; /*Generalpurposereturncode longtotal_time=0L; /*variabletoholddeltat tmp,len =0; longbytes_read=longlast_time,now,timeout=15L;longltemp;BOOLbTemp= SetDlgItemText(hOurDlg,IDD_HNAME," time(&now);time(&last_time);while(last_time+timeout>now){time(&now);do{;if{ }if(len==SOCKET_ERROR){if(h_errno==WSAEWOULDBLOCK){/*ifnodata,readagain*/}/*end:if(errno==WSAEWOULDBLOCK)*/else{}}/*endelse*/}/*end:if(len==SOCKET_ERROR)if(bTemp){/*Toupdateourmaindisyonce/*Donotusewsprintf()oryouwilladdanextrachar_fmemcpy(prbuf,inet_ntoa(local.sin_addr),4*sizeof(u_long));SetDlgItemText(hOurDlg,IDD_HNAME,(LPSTR)prbuf); MENT,"ReadingUDPData...");bTemp=FALSE;}if(len!=bytes_read+=/*Printthestatisticsgathered }/*end:while*/ltemp=num_reads/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=bytes_read/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=8*(bytes_read/total_time);wsprintf((LPSTR)prbuf,"%ld\n",ltemp);SetDlgItemText(hOurDlg,IDD_BITS,(LPSTR)prbuf); }/*end:if(bytes_read)*/else{ MENT,prbuf);gotocome_here;}/*end:else/*sendreplyto''*/"Repliedto%s\n",inet_ntoa(local.sin_addr)); for(i=0;i<10;++i)sprintf(SendBuf,"Rec'd%ldbytes.\n",bytes_read);{if(len==SOCKET_ERROR){/*ifcouldnotsendbec.*/if(h_errno==WSAEWOULDBLOCK)wshout_err(hOurDlg,WSAGetLastError(),"sendto()");}/*end:if(len==-1)}/*end:if(len=sendto())}/*endfor*/}/*eoftshout.c文件名/*MSCIncludefiles:*/#include<stdio.h>#include<io.h>#include<string.h>#include<stdlib.h>#include<time.h>{intstaticintDataBuffer[BUF_SIZE];/*Buffertoholdgenerateddata*/longtotal_len=1024L*1024L;/*Total#ofbytestogenerate*/longbytes_sent=0L;/*Counterofbytesonconnection*/inttmp= /*holdscountforbyteswrittenlongwrite_count=0L;/*numberoftimes time_tstart,end; /*variablestoholdreadtiming*/longtotal_time= /*variabletoholddelta First,generatethedataandceitintoanarray, for(counter=0;counter<BUF_SIZE;counter++)DataBuffer[counter]=counter;/*Writedataonthedescriptorlikea while(bytes_sent<total_len){ /*whilestillbytestosend...*/do{;if{ }if(tmp==SOCKET_ERROR){if(h_errno==WSAEWOULDBLOCK)else}/*Calc.timeelapsed&statsaboutanydatasent*//*exitfromthewhileloop*/}/*endif(tmp==-1) /*incr.counteroftimeswritten*/bytes_sent+=tmp;/*total#ofbytescedonconnection*/SetDlgItemText(hOurDlg,IDD_WRITE,(LPSTR)prbuf);}/*endwhile*//*Printthestatisticsgathered ltemp=write_count/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=bytes_sent/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=8*(bytes_sent/total_time);wsprintf((LPSTR)prbuf,"%ld\n",ltemp);SetDlgItemText(hOurDlg,IDD_BITS,(LPSTR)prbuf);}/*endif(total_time) returnbytes_sent;}/*eoftlisten.c/*MSCIncludefiles:*/#include<stdio.h>#include<io.h>#include<string.h>#include<stdlib.h>#include<time.h>{staticchar SOCKEThAcceptSock;structsockaddr_inlocal;/*Localmachineaddressstructure*/longtotal_time=0L; /*variabletoholddeltat*/inttmp,len=0; longbytes_read=0L;longlast_time=0L;longnow=0L;externintrun_cancelled;structlingerAcceptLinger;BOOLbTemp=TRUE; SetDlgItemText(hOurDlg,IDD_HNAME," tmp=(intFAR*)&tmp);}{for{do;if{ }(intFAR*)&tmp);{if(h_errno==/*Tryagain;else/*Fatalerror--popout.*/}}/*endif((hAcceptSock=..*/else{/*Success--popout.*/}}/*endfor}/*endelsewshout_err(hOurDlg,WSAGetLastError(),"accept()");return0;}/*Now,readthedataasfastaswecanuntilnomoretoread*/dodo;if{ }len=recv(hAcceptSock,ReadBuf,read_len,0);if(len==SOCKET_ERROR){if(h_errno==WSAEWOULDBLOCK)}elseif(len==0)bytes_read+=len; /*Donotusewsprintf()oryouwilladdanextrachar_fmemcpy(prbuf,inet_ntoa(local.sin_addr),4*sizeof(u_long));SetDlgItemText(hOurDlg,IDD_HNAME,(LPSTR)prbuf); MENT,"ReadingTCPData...");bTemp=FALSE;}}while((len!=0)||(len!=SOCKET_ERROR));time(&now);if(len==SOCKET_ERROR)if((h_errno==WSAESHUTDOWN)||(h_errno==WSAENOTCONN)) MENT,prbuf);}else{/*Othererror}}elseif(len==0)/*Othersideshutdowntheconnection*/ MENT,prbuf);}if(ret==SOCKET_ERROR){}if(ret==SOCKET_ERROR)}if(total_time==0) ltemp=num_reads/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);ltemp=bytes_read/total_time;wsprintf((LPSTR)prbuf,"%ld\n",ltemp);l
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 加氢稳定装置操作工职业考核试卷及答案
- 感光材料乳剂熔化工上岗考核试卷及答案
- 环氧乙烷(乙二醇)装置操作工三级安全教育(班组级)考核试卷及答案
- 信息科学技术试题及答案
- 应用会计面试题及答案解析
- 银行信贷测试面试题及答案解析
- 保密专业试题及答案
- 小学语文人教部编版六年级下册《石灰吟》课件
- 畜牧考研专业试题及答案
- 测绘专业试题及答案
- 系统测量msa培训课件
- 幼儿园中国传统文化培训
- 2025年中国冷冻熟虾数据监测研究报告
- 护理陪检课件
- 幼儿园开学卫生消毒培训
- 医院信息化建设中长期规划(十五五规划2025年)
- 2024年全国导游资格考试《全国导游基础知识》真题和解析
- 国家中医药管理局《中医药事业发展“十五五”规划》全文
- 2025年4月自考00831英语语法试题及答案含评分参考
- 中式面点课件
- 抖店内衣考试题库及答案
评论
0/150
提交评论