还剩17页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
电子科技大学实验报告学生姓名学号指导教师实验地点清水河主楼实验时间2016
一、实验室名称Linux环境高级编程实验室
二、实验项目名称基本TCP套接口通信库封装
三、实验学时8学时
四、实验目的学会对TCP套接口的通信机智进行封装,尝试使用不同的方式完成
五、实验内容对基本TCP套接口通信机制进行封装要求使用以下五种编程范式,封装通信库;并使用五种封装后通信库,实现echo服务器和客户端•传统C的结构化程序设计思想•面向对象的程序设计思想
(三)基于接口的程序设计思想程序服务端代码如图
3.1和客户端代码如图
3.2所示:server.cppXnettnet/tn.harpa/tnet.hmemory.hstrtng.hunistd.h//getptdstdto.h//sprtntfiostream8classCTCPSe「ve「Obse「ve「〃服务器观察者{public:CTCPServerObserver141516virtual-CTCPServerObserver17{181926public:21virtualvoidServerFunctiontntnConnectedSockettntnLtstenSocket=0;22};〃socket是套接字的意思用于描述IP地址和端口23classCTCPServer//tCp服务器public:public:CTCPServerCTCPServerObserver*pObservertntnServerPorttntnLengthOfQueueOfListen=160constchar*strBoundIP=NULLm_pObserver=pObserver;m_nServerPort=nServerPort;m_nLengthOfQueueOfListen=nLengthOfQueueOfListen;tfNULL==strBoundIPm_strBoundIP=NULL;elsetntlength=st「lenst「BoundIP;〃返回字符串长度m_strBoundIP=newchar[length+1];memcpym_strBoundIPstrBoundIPlength+1;〃复制内存,长度为length+1virtual-CTCPServertfm_strBoundIP!=NULL49delete[]m_strBoundIP;50}5152RuntntnLtstenSocket=::socketAF_INETSOCK_STREAM;〃创建套接字if-1==nLtstenSocketIstd::cout«socketerror«std::endl;return-1;sockaddr_inServerAddress;memsetServerAddress0stzeofsockaddjtn;〃清空ServecAddressServerAddress.sin_family=AF_INET;ifNULL==m_strBoundIPServerAddress.stn_addr.s_addr=htonl1NADDR_ANY;〃把主机字节顺序转化为网络字节顺序707071else72{73if::inet_ptonAF_INETm_strBoundIPServerAddress.sin_addr!=1〃将参数二的点分十迸制转化为二进制整数74{75std::cout«tnet_ptonerror«std::endl;76::closenListenSocket;77return-1;78798081ServerAddress.stn_port=htonsm_nSe「ve「Po「t;〃主机字节顺序转换为网络字节顺序8283if::bindnListenSocketsockaddr*ServerAddresssizeofsockaddrin==-1〃绑定套接口字符到计算机端口8485std::cout«binderror«std::endl;86::closenListenSocket;87return-1;88}8990tf::ltstennListenSocketm_nLengthOfQueueOfListen==-l//listen监听来自客户端的tcpsocket的连接请求一9192std::cout«listenerror«std::endl;93::closenListenSocket;94return-1;959697sockaddr_tnCltentAddress;98socklen_tLengthOfClientAddress=sizeofsockaddr_in;99intnConnectedSocket=::acceptnListenSocketsockaddr*CltentAddressLengthOfCllentAddress;〃接受一个连接100if-l==nConnectedSocket101{102std::cout«accepterror«std::endl;103::closenListenSocket;104return-1;105}106107ifm_p0bserver!=NULL108109m_pObserver-ServerFunctionnConnectedSocketnListenSocket;110}111112::closenConnectedSocket;113::closenListenSocket;114115return0;116117118private:119intm_nServerPort;120char*m_strBoundIP;121intm_nLengthOfQueueOfListen;122CTCPServerObserver*m_pObserver;〃服务器历程123;124125classCMyTCPServer:publicCTCPServerObserver126127public:128CMyTCPServer129{130131132virtual-CMyTCPServer133134135136private:137virtualvoidServerFunctionintnConnectedSocketintnListenSocket138{139chars
[30];140intptd=getptd;〃获取进程号141sprintfs%dpid;//int转化为char142std::cout«server:myprocessIDis«pid«std::endl;143::wrttenConnectedSocketsstzeofs;144〃将数据写入已打开的文件内参数一要进行写操作的文件描述词参数二需要输出的缓冲区,参数三最大输出字节计数145}146;147148intmatn149{156CMyTCPServermyserver;151CTCPServertcpservermyserver4000;152tcpserver.Run;153return0;154}图
3.1server程序dient.cppXsys/socket.hnettnet/tn.hmemory.harpa/tnet.hstrtng.h7172ifm_pObserver!=NULL73{74m_pObserver-CltentFunctionnClientSocket;757677::closenClientSocket;7879return0;8081private:tntm_nServerPort;char*m_strServerIP;CTCPClientObserver*m_pObserver;;8788classCMyTCPCHent:publicCTCPCltentObse「ve「〃继承89{90public:91CMyTCPCltentO92{939495virtual-CMyTCPClient96979899private:100virtualvoidClientFunctionintnConnectedSocket101102char「ectve[3];〃用于寄存从服务端收到的ID号103::readnConnectedSocketrectvesizeofrectve;〃读文件服务器[D104std::cout«client:server*sprocessIDis«recive«std::endl;105106}107;108109tntmatn110{111CMyTCPClientclient;112CTCPCltenttcpcitentcltent
4000127.
0.
0.1H;113tcpclient.Run;|114115return0;116图
3.2client程序经过编程,然后make再运行服务器端结果如图
3.3的(a)客户端结果如图(a)rooteVan¥uMin zhone/yanyumin/c-or-cpp/Linux-trai1/3TCPMakefileclient.cppseruer.cpprooteYanYuMin/hone/yanyunin/c-or-cpp/Linux-trai1/3TCPh++-oseruerseruer.cpp-g♦+-oclientclient.cpp-grootean¥uMin zhone/yanyunin/c-or-cpp/Linux-trai1/3TCPMakefileclientclient.cppseruerseruer.cpprootOYanYuMin zhome/yanyunin/c-or-cpp/Linux-trai1/3TCPseruer myprocessIDis4465root@YanYuMin:/hofne/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/3jiekou#./clientclient serversprocessIDis4465图
3.3运行结果
(四)静态的面向对象的程序设计思想程序代码如图所示
4.1和图
4.2所示server.cppX1#incl.ude2#tnclude3/Include4#include-5#include6#include7fftnclude8netinet/in.harpa/inet.hmemory.hstring.hunistd.h//getptdstdto.h//sprintftostream9templatetypenameT10classCTCPServer1213141516171819202122232425262728293031323334353637public:CTCPServertntnServerPortintnLengthOfQueueOfListen=100constchar*strBoundIP=NULLm_nServerPort=nSe「ve「Po「t;//端口m_nLength0fQueue0fListen=nLengthOfQueueOfListen;〃长度ifNULL==strBoundIPm_strBoundIP=NULL;elseintlength=st「lenstfBoundIP;〃返回字符串长度m_strBoundIP=newchar[length+1];mencpym_strBoundIPstrBoundIPlength+1;〃复制内存virtual{~CTCPServerifm_strBoundIP!=NULLdelete[]m_strBoundIP;38public:39404142434445464748495051525354intRunintnListenSocket=::socketAF_INETSOCK_STREAM;〃创建套接字if-1==nListenSocket{std::cout«socketerror«std::endl;return-1:sockaddr_inServerAddress;memsetServerAddress0stzeofsockadd〃清空Serve「AddressServerAddress.sin_family=AF_INET;tfNULL==m_strBoundIP{ServerAddress.stn_addr.s_addr=htonlINADDR_ANY;〃把主机字节顺序转化为网络字节顺序)else(55565758=15960616263646566if::inet_ptonAF_INETm_strBoundIPServerAddress.sin_addr!std::cout«Minet_ptonerror«std:zendl;::closenLlstenSocket;〃关闭文件return-1:ServerAddress.sin_port=htons(m_nSe「ve「Po「t);〃主机字节顺序转换为网络字节顺序6768tf::btndnLtstenSocketsockaddr*ServerAddressstzeofsockaddr_in==-1〃绑定套接口字符到计算机端口std::cout«binderror«std::endl;::closenLtstenSocket;return-1;if::listennListenSocketm_nLengthOfQueueOfListen==-l//listen监听来官客户端的tcpsocket的连接请求{std::cout«listenerror«std::endl;::closenListenSocket;return-1;8681sockaddr_tnCltentAddress;socklen_tLengthOfClientAddress=sizeofsockaddr_in;intnConnectedSocket=::acceptnListenSocketsockaddr*CltentAddressLengthOfCltentAddress;〃接受一个连接tf-l==nConnectedSocketstd::cout«accepterror«std::endl;::closenListenSocket;return-1;T*pT=stattc_castT*thts;〃重点,强制转换pT-ServerFunctionnConnectedSocketnListenSocket;::closenConnectedSocket;::closenLtstenSocket;return0;100101private:102voidServerFunctionintnConnectedSocketintnListenSocket103{104105106private:intm_nServerPort;char*m_strBoundIP;109intm_nLengthOfQueueOfListen;110;111112classCMyTCPServer:public113{114public:115CMyTCPServerintnServerPortintnLengthOfQueueOfListen=100constchar*strBoundIP=NULL:CTCPServerCMyTCPServernServerPortnLengthOfQueueOfListenstrBoundIP116{117118119virtual~CMyTCPServer120{121122123voidServerFuncttontntnConnectedSocketintnListenSocket124125chars
[30];126intptd=getptd;〃获取进程号127sprintfs%dpid;//int转化为Char128std::cout«Mserver:myprocessIDis«pid«std::endl;129::writenConnectedSocketssizeofs;136〃将数据写入已打开的文件内参数一要进行写操作的文件描述词参数二需要输出的缓冲区,参数三最大输出字节计数131132;133matnCMyTCPServermyserver4O00;myserver.Run;return0;图
4.1server程序2345678910111213141516171819202122232425262728293031323334353637383940414243444546474849495051525354555657585960616263646566676869707172client.cppX#includesys/socket.h#includenettnet/tn.h#includememory.h#tncludearpa/tnet.h#includestrtng.h#includetostreamtenplatetypenameTclassCTCPCllent〃监听程序封装Ipublic:CTCPCltenttntnServerPortconstchar*strServerIP{m_nServerPort=nServerPort;intnlength=5七「1一5{「582”;〃返回字符串长度m_strServerIP=newchar[nlength+1];strcpym_strServerIPst「Se「ve「IP;〃复制字符串virtual-CTCPClientdelete[]m_st「Se「ve「IP;〃删除public:intRunintnClientSocket=::socketAF_INETSOCK_STREAM;〃创建套接字if-1==nClientSocket{std::cout«socketerror«std::endl;return-1;sockaddr_inServerAddress;memsetServerAddress0slzeofsockaddjln;〃清空内存ServerAddress.sin_family=AF_INET;if::inet_ptonAF_INETm_strServerIPServerAddress.sin_addr!=1{〃将参数三的点分+进制转化为二进制整数std::cout«inet_ptonerror«std::endl;::cl.osenCltentSocket;return-1;ServerAddress.sin_port=htonsm_nServerPort;if::connectnCltentSocketsockaddr*ServerAddressstzeofif::connectnClientSocketsockaddr*ServerAddressstzeofServerAddress==•1〃判断是否连接上服务器端std::cout«connecterror«std::endl;::closenClientSocket;return-1;T*pT=st-〃强制转换pT-ClientFunctionnClientSocket;::closenClientSocket;return0;voidCltentFuncttontntnConnectedSocketprivate:intm_nServerPort;char*m_strServerIP;);73classCMyTCPClient:publicCTCPCltentCMyTCPCltent〃继承于上面的封装74{75public:76CMyTCPClientintnServerPortconstchar*strServerIP:CTCPClientCMyTCPClientnServerPortstrServerIP77{787980virtual-CMyTCPClient81{828384voidCltentFuncttonintnConnectedSocket85{8687charrectve
[30];88::readnConnectedSocketrecivesizeof「eclve;〃读文件服务器[D89std::cout«client:serversprocessIDis«recive«std::endl;9091;9293intmain94{
95、CMyTCPClientclient4O
00127.
0.
0.1;96client.Run;9798return0;99}图
4.2client程序通过makefile再运行后的结果如图所示:rooteYanYuMin zhone/yanyumin/c-or-cpp/Linux-trai1/3TCPSocket/4jingtaittmakeg++-oseruerseruer.cpp-gg++-oclientclient.cpp-groot¥anuMin zhone/yanyumin/c-or-cpp/Linux-trai1/3TCPSocket/4jingtai./seruerseruer myprocessiDis4517IrootOYanYuMin/home/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/4jingtai#IsMakefileclientclient.cppserverserver.cpproot@YanYuMin:/home/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/4jingtai#./clientclient serversprocessIDis4517图
4.3运行结果图
七、总结及心得体会:我发现其实vi环境下编程不怎么好用,再getdit下反而速度还提升了,可能是习惯使然吧创新思维有限,然后就把程序弄懂了再改变一下
八、对本实验过程及方法、手段的改进建议:没有建议,喜欢李老师的完美的课程报告评分指导教师签字•基于接口的程序设计思想•静态的面向对象的程序设计思想•面向方面的程序设计思想
六、实验步骤:一般而言,客户端的流程如下:1创建套接字socketo2向服务器发出连接请求connect3和服务器端进行通信send/recvo4关闭套接字而服务器端的流程如下:1创建套接字socketo2将套接字绑定到一个本地地址和端口上bindo3将套接字设为监听模式,准备接收客户端请求listeno4等待客户请求到来;当请求到来后,接受连接请求,返回一个新的对应于此次连接的套接字accepto5用返回的套接字和客户端进行通信send/recv6返回,等待另一个客户请求7关闭套接字-传统C的结构化程序设计思想程序部分代码如图
1.1所示,其功能是服务器端把自己的进程号传给客户端图是我在图形界面下getdit软件中截取的server.cppXcnettnet八n.h〃包含sockaddjin函数arpa/tnet.hmemory.huntstd.h//getptdstdio.h//sprintftostream78typedefvoid*TCPServertntnConnectedSockettntnLtstenSocket;〃定义一个函数指针指向函数的指针TCPSeze「,它返回votd类型,有两个tnt型参数9intRunTCPServerTCPServerServerFunctionintnPortintnLengthOfQueueOfListen=100constchar*strBoundIP=NULL〃该函数负责创建套接字,绑定端口,监听以及与客户端建立连接queue队列bound限制,范围intnLtstenSocket=::socketAF_INETSOCK_STREAMe;〃AF_INET:代表tcp/tp协议族定义于数据结构sockaddjtn中,stream流《::socket值返回一个套接口描述符,接下来就该把这个描述符绑定在系统的一个端口上如果只想用connect则不需要绑定tf-l==nLtstenSocketdstd::cout«socketerror«std::endl;return-1;19sockaddr_tn$030摩11「0$5;〃$0018^「_是一种数据结构,包含了地址族、网络数据端口号、工口地址等,定义于头文件中20memsetServerAddress0stzeofsockadd「_tn;〃nenset函数在socket中常用于清空一个结构类型变量或者数组O非常方便,可以直接清空数组的所有数据参数1是对应内存的起始地址,参数2指将内存置为,参数3为往后置的空间大小ServerAddress.sin_family=AF_1NET;〃sin_famtly指协议族,在socket编程中只能是AF_INETtfNULL==strBoundIP//strBoundIPServerAddress.sin_addr.s_addr=htonlINADDR_ANY;〃sln_add「.s_addr是sockaddjtn结构中按网络字节顺序存宿IP地址而函数htonl,函数的功宵维把主机字节顺序转化为网络字节顺序host-to-net-ustgndlong=htonl.〃所谓网络字节顺序大尾顺序就是指一个数在内存中存储的时候“高对低,低对高”即一个数的高位字节存放于低地址单元,低位字节存放在高地址单元中但是计算机的内存存储数据时有可能是大尾顺序或者小尾顺序else{if::inet_ptonAF_INETstrBoundIPServerAddress.sin_addr!=1//Inejpton将参数二酌点分十注制转化为二进制整数,存在第三个参数里{std::cout«inet_ptonerror«std::endl;::closenLlstenSocket;〃参数是要关闭的文件描述符return-1;1ServerAddress.stn_port=htonsnPort;〃主机字节顺序转换为网络字节顺序tf::btndnLtstenSocketsockaddr*ServerAddressstzeofsockaddr_tn==-1〃绑定套接口字符到计算机端口,参数一是套接口文件字符shocket产生,参数三是指向数据结构sockadd「的一个只正,参数三就这么设置就对了{std::cout«binderror«std::endl;::closenLlstenSocket;〃关闭文件return-1;}if::listennListenSocketnLengthOfQueueOfListen==-l〃ltsten监听来自客户端的tcpsocket的连接请求,参数一是套接字,参数二是侦听队列长度上限值用于多个用户请求的记录std::cout«listenerror«std::endl;::closenLtstenSocket;return-1;sockaddr_inClientAddress;〃定义一个叫做CltentAddeess的数据结构sockaddr_tn包含网络地址等socklen_tLengthOfClientAddress=stzeofsockadd「_ln;〃socklen_t^[Hnt相同的尺寸大小intnConnectedSocket=::acceptnListenSocketsockaddr*CltentAddressaLengthOfCltentAddress;//accept在一个套接口接受一个连接参数一是套接字描述符,参数二可选是指针,指向一缓冲区,其中接收为通讯层所知的连接实体的地址Add「参数的实际格式由套接口创建时所产生的地址族确定参数三可选为指针,输入参数,配合add「一起使用,指向存有adde地址长度的整型数if-1==nConnectedSocketstd::cout«accepterror«std::endl;::closenLtstenSocket;return-1;ServerFuncttonnConnectedSocketnLtstenSocket;〃ServerFunctlon是含有两个整形变量的数据结构,类型为TCPSe「ve「,在第九行有定义::closenConnectedSocket;::closenListenSocket;return6;voidMyServertntnConnectedSockettntnLtstenSocket{chars
[36];tntptd=getptd;〃获取进程号sprintfs%dpid;//int转化为charstd::cout«server:myprocessIDis«pid«std::endl;::writenConnectedSocketssizeofs;〃将数据写入已打开的文件内参数一要进行写操作的文件描述词参数二需要输出的缰冲区,参数三最大输出字节计数Otntmain{RunTCPServerMyServer4000;return0;图
1.1C版本的server程序客户端程序如下:client.cppX78typedefvoid*TCPCltenttntnConnectedSocket;//包含一个参数的函数指针910tntRunTCPCl.ientTCPClientClientFuncttonintnServerPortconstchar*st「Se「verIP〃该函数负责运行客户端1112intnCltentSocket=::socketAF_INETSOCK_STREAM8;〃AF_INET:代表tcp/tp协议族定义于数据结构sockadd「_tn中,stream流《::socket值返回一个套接口描述符,接下来就该把这个描述符绑定在系统的一个端口上如果只想用connect则不需要绑定13if-1==nCltentSocket1415std::cout«socketerror1«std::endl;16return-1;171819sockaddr_inServerAddress;〃sockadd「_ln是一种数据结构,包含了地址族、网络数据端口号、工口地址等,定义于头文件中20memsetServerAddress0slzeofsockadd「_ln;〃memset函数在socket中常用于清空一个结构类型变量或者数组o非常方便,可以直接清空数组的所有数据参数1是对应内21ServerAddress.sin_fanily=AF_INET;22tf::inet_ptonAF_INETstrServerIPServerAddress.sin_addr!=123{24std::cout«Mtnet_ptonerror«std::endl;25::closenCltentSocket;〃参数是要关闭的文件描述符26return-1;272829ServerAddress.sin_port=htonsnServerPort;3031tf::connectnCltentSocketsockaddr*ServerAddressstzeofServerAddress==-1〃判断是否连接上服务器端32{33std::cout«connecterror«std::endl;34::d.osenClientSocket;35return-1;363738ClientFunctionnClientSocket;3940::closenClientSocket;4142return0;43}4445voidMyCltenttntnConnectedSocket46{47charrecive
[30];48::readnConnectedSocketrectvestzeofreclve;〃读文件服务器[D49std::cout«client:server*sprocessIDis«recive«std::endl;505152intmatn53{54RunTCPClxentMyCXient
4900127.
0.
0.1;5556return0;57图
1.2C版本的client程序经过g++编译,运行结果如图L3所示,完成了对服务器进程的ID获取:rootPVanVuMin zhome/yanyumin/c-or-cpp/Linux-trai1/3TCPSocket/Ichengxu#maken++-oseruerseruer.cpp-g♦+-oclientclient.cpp-grootPVanVuMin zhome/yanyumin/c-or-cpp/Linux-trai1/3TCPSocket/Ichengxu#IsMakefileclientclient.cppseruerseruer.cpprootVanYuMin zhome/yanyunin/c-or-cpp/Linux-trai1/3TCPSocket/lchengxutt./seruerseruer myprocessIDis4216rootPYanYuMin zhone/yanuunin/c-or-cpp/Linux-trai1/3TCPSocket/lchengxuttttyl下运行服务器端root@YanYuMin/home/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/lchengxu#./clientclient:serversprocessIDis4216tty2下运行客户端图L3运行结果显示二面向对象的程序设计思想程序代码如图
2.1所不:server.cppXfftncludevnettnet八n.h〃包含sockadd「.函数|/includearpa/inet.hmemory.hstring.huntstd.h//getpidstdto.h//sprtntfiostreamclassCTCPServer//TCP封装{public:CTCPServertntnServerPortintnLengthOfQueueOfListen=100constchar♦strBoundIP=NULL{m_nServerPort=nSe「ve「Po「t;〃端口m_nLengthOfQueueOfListen=nLengthOfQueueOfListen;〃长度ifNULL==strBoundIPm_strBoundIP=NULL;else{intlength=st「lenst「BoundIP;〃返回字符串长度m_strBoundIP=newcharTlenath+11_:memcpym_strBoundIPstrBoundIPlength+i;〃复制内存virtual-CTCPServer{ifm_strBoundIP1=NULL{delete[]m_strBoundIP;}}37public:3839404142434445464748intRunintnListenSocket=::socketAF_INETSOCK_STREAM0;〃创建套接字tf-1==nListenSocketstd::cout«socketerror«std::endl;return-1;sockaddr_tnServerAddress;memsetServerAddress6sizeofsockaddr_in;〃清空Se「ve「Add「ess49ServerAddress.sin_family=AF_INET;56tfNULL==m_strBoundIP{ServerAddress.sin_addr.s_addr=htonlINADDR_ANY;〃把主机字节顺序转化为网络字节顺序elseif::tnet_ptonAF_INETm_strBoundIPServerAddress.sin_addr!=1{std::cout«tnet_ptonerror*«std::endl;::closenLlstenSocket;〃关闭文件return-1;}ServerAddress.sin_port=htonsm_nSe「ve「Po「t;〃主机字节顺序转换为网络字节顺序if::bindnListenSocketsockaddr*ServerAddressstzeofsockaddr_in==-i〃绑定套接口字符到计算机端口{std::cout«binderror«std::endl;::closenListenSocket;return-1;if::listennListenSocketm_nLengthOfQueueOfListen==-l//listen监听来自客户端的tcpsocket的连接请录std::cout«listenerror«std::endl;::closenLtstenSocket;return-1;sockaddr_tnCltentAddress;socklen_tLengthOfClientAddress=stzeofsockaddr_tn;intnConnectedSocket=::acceptnListenSocketsockaddr*ClientAddressLengthOfClientAddress;〃接受一个连接if-1==nConnectedSocket{std::cout«accepterror«std::endl;::closenListenSocket;return-1;ServerFuncttonnConnectedSocketnLtstenSocket;::closenConnectedSocket;::closenListenSocket;return0;99private:100virtualvoidServerFunctionintnConnectedSocketintnListenSocket101{102103104private:110classCMyTCPServer:publicCTCPServerHl112public:117virtual-CMyTCPServer118{119120121private:122virtualvoidServerFunctiontntnConnectedSockettntnLtstenSocket123{124chars
[30];125tntptd=getptd;〃获取进程号126sprintfs%dpid;//int转化为char127std::cout«server:nyprocessIDIs«pid«std::endl;128::writenConnectedSocketssizeofs;129〃将数据写入已打开的文件内参数一要进行写操作的文件描述词参数二需要输出的缓冲区,参数三最大输出字节计数130}131;132133tntmain134135CMyTCPServermyserver4O0O;136myserver.Run;137return0;138}ServerAddress==-1〃〃判断是否连接上服务器端{std::cout«connecterror«std::endl;::closenCHentSocket;return-1;}ClientFunctionnClientSocket;::closenClientSocket;return0;virtualvoidClientFunctionintnConnectedSocket{private:tntm_nServerPort;char*mstrServerIP;;classCMyTCPClient:publicCTCPClient〃继承于上面的封装{public:CMyTCPClienttntnServerPortconstchar*strServerIP:CTCPCltentnServerPortstrServerIPvirtual-CMyTCPClient{private:virtualvoidClientFunctionintnConnectedSocket{charrecive
[30];::readnConnectedSocketrecivestzeof「ecive;〃读文件服务器IDstd::cout«”client:serversprocessIDisH«rectve«std::endl;;tntmain{96CMyTCPClientclient400e
127.
0.
0.1;client.Run;return0;1061图
2.2C++版本的client代码经过g++编译,运行结果如图
2.3所示,完成了对服务器进程的ID获取:rooteVanYutlin/hone/yanyunin/c-or-cpp/Liniix-trai1/3TCPSocket/2duixiangltmakeg++-oseruerseruer.cpp-g-oclientclient.cpp-grooteVanYuMin zhone/yanyunin/c-or-cpp/Linux-trai1/3TCPSocket/Zduixiangtt./seruerseruer nyprocessIDis4Z66server端rootQYanYuMin:/home/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/Zduixiang#./clientclient:serversprocessIDis4266root9YanYuMin:/home/yanyumin/c-or-cpp/Linux-trail/3TCPSocket/Zduixiang#client端图
2.3运行结果105intm_nServerPort;106char*m_strBoundIP;107intm_nLengthOfQueueOfLtsten;108};109。