客户端程序源代码
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define rec_length20
main(int argc, char **argv )
fprintf(stderr,"Note: server_name is defined in file /etc/hosts\n");
fprintf(stderr,"and: lservice dservice are defined in file /etc/services\n");
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:%s\n",argv[1]);
// 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");
fprintf(stderr," and, send it back to client: %s\n",buf);
if(write(ns,f(stderr,"Write socket ns error!\n");
close(ns); exit(-4);
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){
if(read(ns,buf,rec_length)==0){
fprintf(stderr,"Read nothing from socket:ns\n");
close(ns); exit(-3);
}
printf("%s get message from ns: %s\n",argv[0],buf);
if(argc==4){ service=argv[1];sservice=argv[2]; sdest=argv[3];}
else {
fprintf(stderr,"Parameter assigned Error!\nUsage:\n");
fprintf(stderr,"\t%s lservice dservice server_name!\n",argv[0]);
//structure defined for request as a server
struct sockaddr_in sin;
struct servent *sp;
int s,ns,pid;
charbuf[50];
char*service;
//test the environment parameter: lservice, dservice, server_name
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);
fprintf(stderr,"Error: listen");
close(s); exit(-6);
}
while(1){
fprintf(stderr,"\n\t\t\t Now wait connect ...\n");
if((ns=accept(s,0,0))==-1){ // wait a connection */
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,"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);
{
//structure defined for request as a client
structhostent *hp1;
structsockaddr_in sin1;
structservent *sp1;
charsbuf[50];
intss,spid;
char*sservice,*sdest;
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((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");
fprintf(stderr,"%s is sending request message: %s s1\n",argv[0],sbuf);
if(write(ss,sbuf,rec_length)!=rec_length){ //to server
fprintf(stderr,"Write Socket ss ERROR! s1\n");
close(ss); exit(-1);
}
if(read(ss,sbuf,rec_length)==0){//from server
fprintf(stderr,"Read Socket ss Error! s1\n");
close(ss); exit(-2);
}
printf("%s get reply: %s\n",argv[0],sbuf);
}
close(ns); exit(0);
}
}
}
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){
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define rec_length20
main(int argc, char **argv )
fprintf(stderr,"Note: server_name is defined in file /etc/hosts\n");
fprintf(stderr,"and: lservice dservice are defined in file /etc/services\n");
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:%s\n",argv[1]);
// 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");
fprintf(stderr," and, send it back to client: %s\n",buf);
if(write(ns,f(stderr,"Write socket ns error!\n");
close(ns); exit(-4);
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){
if(read(ns,buf,rec_length)==0){
fprintf(stderr,"Read nothing from socket:ns\n");
close(ns); exit(-3);
}
printf("%s get message from ns: %s\n",argv[0],buf);
if(argc==4){ service=argv[1];sservice=argv[2]; sdest=argv[3];}
else {
fprintf(stderr,"Parameter assigned Error!\nUsage:\n");
fprintf(stderr,"\t%s lservice dservice server_name!\n",argv[0]);
//structure defined for request as a server
struct sockaddr_in sin;
struct servent *sp;
int s,ns,pid;
charbuf[50];
char*service;
//test the environment parameter: lservice, dservice, server_name
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);
fprintf(stderr,"Error: listen");
close(s); exit(-6);
}
while(1){
fprintf(stderr,"\n\t\t\t Now wait connect ...\n");
if((ns=accept(s,0,0))==-1){ // wait a connection */
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,"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);
{
//structure defined for request as a client
structhostent *hp1;
structsockaddr_in sin1;
structservent *sp1;
charsbuf[50];
intss,spid;
char*sservice,*sdest;
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((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");
fprintf(stderr,"%s is sending request message: %s s1\n",argv[0],sbuf);
if(write(ss,sbuf,rec_length)!=rec_length){ //to server
fprintf(stderr,"Write Socket ss ERROR! s1\n");
close(ss); exit(-1);
}
if(read(ss,sbuf,rec_length)==0){//from server
fprintf(stderr,"Read Socket ss Error! s1\n");
close(ss); exit(-2);
}
printf("%s get reply: %s\n",argv[0],sbuf);
}
close(ns); exit(0);
}
}
}
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){