Linux socket编程学习初步(3)--客户端向服务器请求文件
来源:互联网 发布:stp 查看环路端口 编辑:程序博客网 时间:2024/06/05 03:41
服务器端:
#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编程学习初步(3)--客户端向服务器请求文件
- Linux socket编程学习初步(5)--服务器多线程
- linux socket TCP 服务器向客户端传文件(任意格式)
- Linux socket编程 服务器 客户端
- java之--------socket编程(TCP)服务器请求与客户端
- Test_Java_网络编程(模拟客户端向服务器上传文件)
- 网络编程客户端向服务器上传文件
- Linux下通过socket通信实现客户端向服务器发送文件
- Linux socket编程(二) 服务器与客户端的通信
- Linux socket编程(二) 服务器与客户端的通信
- Linux socket编程(二) 服务器与客户端的通信
- Linux socket编程(二) 服务器与客户端的通信
- Linux下Socket服务器与客户端编程
- Linux socket编程学习初步(4)--服务器端多进程
- Linux-(C)网络编程学习socket(初步)
- linux socket编程初步(2)
- java socket编程(服务器与客户端)
- socket编程(TCP多线程客户端服务器)
- sql plus操作oracel(windows平台)基础之简易新手篇
- poj-2406kmp中next指针
- SOJ1802 Atomic Nucleus Investigation
- Linux根目录下的四类文件夹
- DLL导出变量
- Linux socket编程学习初步(3)--客户端向服务器请求文件
- 对strcpy优化的一个核心要点
- JDBC高级应用之数据源(连接池)
- 使用linux过程中使用频率最高的命令
- sql 多表查询中,算出某些字段的总和
- HDU 3535 混合背包
- java 代理上网
- Linux定义别名
- robot