客户端程序源代码.doc_第1页
客户端程序源代码.doc_第2页
客户端程序源代码.doc_第3页
全文预览已结束

下载本文档

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

文档简介

#include #include #include #include #include #define rec_length20main(int argc, char *argv )/structure defined for request as a clientstructhostent *hp1;structsockaddr_in sin1;structservent *sp1;charsbuf50;intss,spid;char*sservice,*sdest;/structure defined for request as a serverstruct sockaddr_in sin;struct servent *sp;int s,ns,pid;charbuf50;char*service;/test the environment parameter: lservice, dservice, server_nameif(argc=4) service=argv1;sservice=argv2; sdest=argv3;else fprintf(stderr,Parameter assigned Error!nUsage:n);fprintf(stderr,t%s lservice dservice server_name!n,argv0);fprintf(stderr,Note: server_name is defined in file /etc/hostsn);fprintf(stderr,and: lservice dservice are defined in file /etc/servicesn);exit(-1);if(sp=getservbyname(service,tcp)=NULL)fprintf(stderr,Error: getservbyname);exit(-5);if(s=socket(AF_INET,SOCK_STREAM,0)=-1)fprintf(stderr,Error: socket create);exit(-6);bzero(&sin,sizeof(sin);sin.sin_port=sp-s_port;if(bind(s,&sin,sizeof(sin)=-1)fprintf(stderr,Error: bind);close(s); exit(-6);if(listen(s,5)=-1)fprintf(stderr,Error: listen);close(s); exit(-6);while(1) fprintf(stderr,nttt Now wait connect .n);if(ns=accept(s,0,0)=-1) / wait a connection */fprintf(stderr,error: accept); continue;if(pid=fork()=-1) / conneted, and then create child procfprintf(stderr,server: fork error!);close(s); exit(-1);if(pid!=0) /parent proce, nothing to do, and to the next loopclose(ns);wait(0);continue;if(pid=0) / entered child proccedureclose(s);printf(Receive a client connect:%sn,argv1);if(read(ns,buf,rec_length)=0)fprintf(stderr,Read nothing from socket:nsn);close(ns); exit(-3); printf(%s get message from ns: %sn,argv0,buf);/ request message got from the Client./ Begine a Connection to Server as a Clientif(sp1=getservbyname(sservice,tcp)=NULL)fprintf(stderr,Error: getservbyname s1);exit(-5);if(hp1=gethostbyname(sdest)=0)fprintf(stderr,Error: gethostbyname s1);exit(-6);bzero(&sin1,sizeof(sin1);bcopy(hp1-h_addr,&sin1.sin_addr,hp1-h_length);sin1.sin_family=hp1-h_addrtype;sin1.sin_port=sp1-s_port;if(ss=socket(AF_INET,SOCK_STREAM,0)=-1)fprintf(stderr,Error: socket s1);exit(-6);if(connect(ss,&sin1,sizeof(sin1)=-1)fprintf(stderr,Error: connect s1);close(ss); exit(-6);/ next 2 Line build request messagespid=getpid();sprintf(sbuf,%5.5d%5.5d%5.5d%5.5d,spid,spid*2,spid*3,spid*4);fprintf(stderr,%s is sending request message: %s s1n,argv0,sbuf);if(write(ss,sbuf,rec_length)!=rec_length) /to server fprintf(stderr,Write Socket ss ERROR! s1n); close(ss); exit(-1); if(read(ss,sbuf,rec_length)=0)/from server fprintf(stderr,Read Socket ss Error! s1n); close(ss); exit(-2); printf(%s get reply: %sn,argv0,sbuf);close(ss);/ End the Communication with my Server/ Next 2 Line, build a replay Message buf, and write it back pid=getpid(); sprintf(buf,%5.5d%5.5d%5.5d%5.5d,pid,pid*2,pid*3,pid*4); fprintf(stderr, and, send i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论