while((k=read(source,buf,sizeof(buf)))>0) write(ns,buf,k); printf(″
传输完毕!!! ″); close(ns); } close(source); exit(0); /*客户机源程序khj.c*/ #include >stdio.h> #include >sys/types.h> #include >sys/fcntl.h> #include >sys/socket.h> #include >sys/netinet/in.h> #include >netdb.h> #include >errno.h> #include >string.h> main() { char buf[1024],file[30]; char *strs=″
正在接收文件″; int target; register int k,s; struct sockaddr_in sin; struct hostent *hp; system(″clear″); printf(″ ″); hp=gethostbyname(″server″); if(hp==NULL){ perror(″返回服务器地址信息错!!!″); exit(1); } s=socket(AF_INET,SOCK_STREAM,0); if(s<0){ perror(″获取SOCKET号失败!!!″); exit(2); } sin.sin_family=AF_INET; sin.sin_port=htons(1500);/*端口号需与服务器程序使用的一致*/ bcopy(hp->h_addr,&sin.sin_addr,hp->h_length); printf(″
正在与服务器连接…″); if(connect(s,&sin,sizeof(sin),0)<0){ perror(″不能与服务器连接!!!″); exit(3); } while((k=read(s,file,sizeof(file)))<?0/*接收文件名*/ if((target=open(file,o_WRONLYO_CREATO_TRUNC,0644))<0){ perror(″不能打开目标文件!!″); exit(4); } strcat(strs,file); strcat(strs,″,稍候…″); write(1,strs,strlen(strs)); while((k=read(s,buf,sizeof(buf)))>0) write(tatget,buf,k); printf(″
接收文件成功!!! ″); close(s); close(target); } 上述程序在Sco Unix System v3.2及Sco TCP/IP Rumtime环境下调试通过。 (出处:清风网络学院)
上一篇:Linux 下 C++程序的异常处理技巧
下一篇:在C语言中如何处理时间和日期
|