Linux socket编程学习初步(3)--客户端向服务器请求文件
2012-03-14 20:23
796 查看
服务器端:
客户端:
给出了客户端由服务器获取一个文件的例子。
获取的文件将直接显示在终端界面上。
#include <stdio.h> #include <stdlib.h> #include <unistd.h>//read,write #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <sys/fcntl.h>//open #include <strings.h> #define SERVER_PORT 12345 #define BUF_SIZE 4096 /* block transfer size */ #define QUEUE_SIZE 10 /* how many client can connect at the same time */ void fatal(char *string) { printf("%s\n",string); exit(1); } int main(/*int argc,char * argv[]*/) { int s,b,l,fd,sa,bytes,on=1; char buf[BUF_SIZE]; /*buf for outgoing files*/ struct sockaddr_in channel; /* hold IP address*/ /* Build address structure to bind to socket. */ bzero(&channel,sizeof(channel)); channel.sin_family=AF_INET; channel.sin_addr.s_addr=htonl(INADDR_ANY); channel.sin_port=htons(SERVER_PORT); /* Passive open.Wait for connection. */ s=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); /* Creat socket */ if(s<0) fatal("socket failed"); setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on)); /* use the socket for many time (up word) */ b=bind(s,(struct sockaddr *)&channel,sizeof(channel)); if(b<0) fatal("blind failed"); l=listen(s,QUEUE_SIZE); if(l<0) fatal("listen failed"); /* Socket is now set up and bound. Wait for connection and process it. */ while(1) { sa=accept(s,0,0); if(sa<0) fatal("accept failed"); recv(sa,buf,BUF_SIZE,0); /* Get and return the file. */ fd=open(buf,O_RDONLY); if(fd<0) fatal("open failed"); while(1) { bytes=read(fd,buf,BUF_SIZE); if(bytes<=0) break; write(sa,buf,bytes); } close(fd); close(sa); } return 0; }
客户端:
/* *This page contains a client program that can request a flie from the server program. */ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <arpa/inet.h> #include <string.h> #include <stdlib.h> #include <unistd.h>//read,write #include <stdio.h> #define SERVER_PORT 12345 #define BUF_SIZE 4096 #define PATH "/home/fupeng/Desktop/test.txt" #define DEST_IP "127.0.0.1" void fatal(char *s) { printf("%s\n",s); exit(1); } int main(/*int argc,char * *argv*/) { int c,s,bytes; char buf[BUF_SIZE]; /*buf for incoming files*/ struct sockaddr_in channel; /* hold IP address*/ // if (argc!=3)fatal("Usage:client IPaddress file-name"); s=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); if(s<0)fatal("socket"); bzero(&channel,sizeof(channel)); channel.sin_family=AF_INET; channel.sin_port=htons(SERVER_PORT); if(inet_aton(DEST_IP,&channel.sin_addr)==0) { /* use the ASCII ip for the server's sin_addr. */ fprintf(stderr,"Inet_aton erro\n"); exit(1); } c=connect(s,(struct sockaddr *)&channel,sizeof(channel)); if(c<0)fatal("connetct failed"); // send(s,argv[2],strlen(argv[2]),0); send(s,PATH,strlen(PATH),0);//BY FUPENG /* Go get the file and write it to standard output.*/ while(1) { bytes=read(s,buf,BUF_SIZE); if(bytes<=0) exit(0); write(1,buf,bytes); } }
给出了客户端由服务器获取一个文件的例子。
获取的文件将直接显示在终端界面上。
相关文章推荐
- Linux socket编程学习初步(5)--服务器多线程
- android下socket编程问题:服务器关闭时,客户端发送请求的异常处理
- Linux socket编程-最简单的服务器和客户端程序
- socket编程-客户端向服务器发送字符串,传文件
- android下socket编程问题:服务器关闭时,客户端发送请求的异常处理
- linux下socket编程实现一个服务器连接多个客户端
- linux socket 编程一:简单的服务器和客户端通信
- Linux socket编程(二) 服务器与客户端的通信
- 网络编程(5)—— 基于Linux系统的UDP协议socket服务器和客户端
- Androin学习笔记四十四:android下socket编程问题:服务器关闭时,客户端发送请求的异常处理
- Linux socket编程 服务器 客户端
- linux下socket编程实现一个服务器连接多个客户端
- Linux socket编程 服务器 客户端
- 粤嵌-Android学习之Socket编程 手机为服务器 模拟器为客户端
- (Android)JavaSocket编程,文件上传到服务器,客户端Android,服务器端J2SE
- Linux socket编程-最简单的服务器和客户端程序
- linux下socket编程 select实现非阻塞模式多台客户端与服务器通信
- Linux下网络socket编程——实现服务器(select)与多个客户端通信
- Linux socket编程(二) 服务器与客户端的通信
- TCP/TP编程 - 一个简单的Linux下C写的socket服务器客户端程序