实验9UNIX Socket编程_Password_Removed.pdf
实验 9:UNIX Socket 编程 1、实验目的、实验目的(1)掌握 UNIX Socket 工作机制和程序设计方法;(2)掌握 UINIX 环境 C/S、C/M/S 结构与工作模式;(3)掌握服务器程序的设计方法;(4)掌握 C/S 结构程序的设计方法;(5)了解与通过相关文件/etc/hosts,/etc/services,/etc/network。2、实现设备、实现设备 一台装有 Windows 操作系统和 Linux 机系统的微机或服务器。3、实验方法与注意事项、实验方法与注意事项 实验室内的实验环境与系统是共用设施,请不要在系统内做对系统或对其他用户不安全的事情。要求每个同学登录后系统后,要在自己的家目录内容以自己(拼音)名字或学号,创建一个子目录(已有者可以不再创建)。以后所有工作都要在自己的目录内进行。建议以后的实验都在同台计算机上做,这样可以保持连续性。用户要按通常实验要认真书写实验报告。4、实验过程、实验过程 示例 1:客户端程序设计#include#include#include#include#include#define rec_length 20 main(int argc,char*argv)struct hostent*hp;struct sockaddr_in sin;struct servent*sp;char buf50;int s,pid;char*service,*dest;if(argc!=1)service=argv1;dest=argv2;else fprintf(stderr,NO service assigned!nUsage:);fprintf(stderr,%s service_name destination!n,argv0);fprintf(stderr,Note:service_name is defined in/etc/servicesn);fprintf(stderr,destination is defined in/etc/hostsn);exit(-1);if(sp=getservbyname(service,tcp)=NULL)fprintf(stderr,Error:getservbyname);exit(-5);if(hp=gethostbyname(dest)=0)fprintf(stderr,Error:gethostbyname);exit(-6);bzero(&sin,sizeof(sin);bcopy(hp-h_addr,&sin.sin_addr,hp-h_length);sin.sin_family=hp-h_addrtype;sin.sin_port=sp-s_port;if(s=socket(AF_INET,SOCK_STREAM,0)=-1)fprintf(stderr,Error:socket);exit(-6);if(connect(s,&sin,sizeof(sin)=-1)fprintf(stderr,Error:connect);close(s);exit(-6);/next 2 Line build request message pid=getpid();sprintf(buf,%5.5d%5.5d%5.5d%5.5dn,pid,pid*2,pid*3,pid*4);fprintf(stderr,%s is sending request message:%sn,argv0,buf);if(write(s,buf,rec_length)!=rec_length)/to server fprintf(stderr,Write Socket s ERRORn!);close(s);exit(-1);if(read(s,buf,rec_length)=0)/get replay from server fprintf(stderr,Read Socket s Errorn);close(s);exit(-2);printf(%s get reply:%sn,argv0,buf);close(s);exit(0);示例 2:服务器程序设计。#include#include#include#include#include#define rec_length 20 main(int argc,char*argv)struct sockaddr_in sin;struct servent*sp;int s,ns,pid;char buf50;char*service;if(argc!=1)service=argv1;else fprintf(stderr,NO service assigned!nUsage:n);fprintf(stderr,t%s server_name!n,argv0);fprintf(stderr,Note:server_name is 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 proc fprintf(stderr,server:fork error!);close(s);exit(-1);if(pid!=0)/parent proce,nothing to do,and to the next loop close(ns);wait(0);continue;if(pid=0)/entered child proccedure close(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./I may do some Server work here./I can also ask another server serve me from a different Socket /Because I am a demo Server,I will not do that./Next 2 Line,build a replay Message buf,and write it back pid=getpid();sprintf(buf,%5.5d%5.5d%5.5d%5.5dn,pid,pid*2,pid*3,pid*4);fprintf(stderr,And,%s replay to client:%sn,argv0,buf);if(write(ns,buf,rec_length)!=rec_length)fprintf(stderr,Write socket ns error!n);close(ns);exit(-4);close(ns);exit(0);示例 3:中间件程序设计。#include#include#include#include#include#define rec_length 20 main(int argc,char*argv)/structure defined for request as a client struct hostent*hp1;struct sockaddr_in sin1;struct servent*sp1;char sbuf50;int ss,spid;char*sservice,*sdest;/structure defined for request as a server struct sockaddr_in sin;struct servent*sp;int s,ns,pid;char buf50;char*service;/test the environment parameter:lservice,dservice,server_name if(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 proc fprintf(stderr,server:fork error!);close(s);exit(-1);if(pid!=0)/parent proce,nothing to do,and to the next loop close(ns);wait(0);continue;if(pid=0)/entered child proccedure close(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 Client if(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 message spid=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 it back to client:%sn,buf);if(write(ns,buf,rec_length)!=rec_length)fprintf(stderr,Write socket ns error!n);close(ns);exit(-4);close(ns);exit(0);4、实验报告的内容与书写 以书面形式记录下你的每一步过程,包括输入、输出信息,遇到的问题和解决的办法。