C4数据通信--网络.ppt_第1页
C4数据通信--网络.ppt_第2页
C4数据通信--网络.ppt_第3页
C4数据通信--网络.ppt_第4页
C4数据通信--网络.ppt_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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 whos 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、;/progname host,user host=argv1; user=argv2; /lookup hostname if(hp=gethostname(host)=NULL) /Get a 32bites 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 service,客户

4、机,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, /sending reques

5、t,客户机,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 No. #defin

6、e 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 localhostMAXHOSTNAME+1; myna

7、me=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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论