版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、数字通信(实训)之,客户机与服务器 的实用程序,程序功能,Client acquires user information from Remote server Client is a program that can be called on by user with Remote hostname and username info wanted,客户机,/client.cpp #include # include # include /A application client prog supporting whois service main(int argc, char*argv);
2、 int s;/socket descriptor int len;/received data length struct sockaddr_in sa;/internet socket address struct hostent *hp;/host name lookup result; struct servent *sp;/service lookup,客户机,char bufBUFSIZ+1; char *myname,*host,*user;/prog name,remote user and host name myname=argv0; if (argc!=3) exit(1
3、);/usage:progname host,user host=argv1; user=argv2; /lookup hostname if(hp=gethostname(host)=NULL) /Get a 32bites IP address of host devided by points and decimal numbers exit(1);/no result /hosts address and type is put into socket struct bcopy(char *)hp-h_addr, (char *) /look up socket number for
4、service,客户机,if (sp=getservbyname(“whois”,”tcp”)=NULL) /get port number exit(1);/no whois service on the host /put the whois socket No. into socket structure sa.sin_port=sp-s_port; /allocate socket if(s=socket(hp-h_addesstype,SOCK_STREAM,0)0) exit(1); /connect to the remote server if(connect(s, /send
5、ing request,客户机,if(write(s,user,strlen(user)!=strlen(user) exit(3); /reading reply while(len=read(s,buf,BUFSIZ)0) write(s,buf,len); close(s);exit(0); ,服务器,在端口上监听并返回信息 /server.cpp #include # include # include #include # include # include /use:whois hostname,username,port43 #define BACKLOC 5 /queuing
6、No. #define MAXHOSTNAME 32 /maximum name len main(argc,argv),服务器, int argc; char*argv; int s,t;/socket descriptors int i; struct sockaddr_in sa,isa;/internet socket address struct hostent *hp;/host name lookup result; struct servent *sp;/service lookup char *myname;/prog name char localhostMAXHOSTNA
7、ME+1; myname=argv0; /look up service entry if(sp=getservbyname(“whois”,”TCP”)=NULL) exit(1);/no this service,服务器,/get host info if(hp=gethostname(localhost, MAXHOSTNAME)=NULL) exit(1); /put socket no and addr info into socket structure sa.sin_port=sp-s_port; bcopy(char *)hp-h_addr, (char *) /setting maximum connections ,s:service port listen(s,BACKLOG);/ BACKLOG is the maxmum queue len received while(1) /waiting for connection,服务器
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 好奇心保护专项训练方案
- 2026-2027学年人教版一年级语文上册10月月考全真模拟卷原卷版
- T/TFZX 54-2025医院行风建设规范 五书一函制度建设
- 《初级财务会计》-第一章
- 2025-2026年养老护理员实操技能考核题库
- 2026年工业机器人应用技术综合测试题库
- 2025-2026年银行从业资格考试风险管理专项训练题库
- 2025-2026年重庆市高三英语一轮复习听力第四章测试卷
- 2025-2026年浙江省人教版高一语文上册第4单元文言文阅读理解测试卷
- 2025-2026年天津市人教版初中体育健康标准测试题
- 江苏南京市2027届高三上学期9月学情调研政治试卷(含解析)
- 2.8 圆的面积(一) 课件(内嵌视频)2026-2027学年北师大版六年级数学上册
- 2026国中康健集团限公司社会招聘(15人)易考易错模拟试题(共500题)试卷后附参考答案
- 新版人教版四年级上册数学全册教案(完整版)教学设计含教学反思
- 第4课《科技力量大》(课件)
- 2025年9月20日云南省直机关遴选公务员笔试真题及答案解析
- 2025年山东初级注安师考试题库及答案
- 人体毒垢与健康
- (完整版)新视野大学英语1单词表
- T/CECS 10240-2022绿色建材评价组合式空调机组
- 中医健脑养生课件
评论
0/150
提交评论