这是上传文件的一个示例,可以参照自行修改成下载或者其它功能。

在上传时,需要先将文件名传到服务器端,这是采用一个结构体,包含文件名及文件名长度(可以用于校验),防止文件名乱码。

client

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include "wrap.h"
#define MAXLINE 1492
#define SERV_PORT 5555
#define FILE_NAME_LEN 64 struct fileInfo{
char fileName[FILE_NAME_LEN];
int len;
}; int main(int argc, char *argv[])
{
if(argc<){
perror("Usage:./a.out ip filename");
exit(-);
}
struct sockaddr_in servaddr;
char buf[MAXLINE];
int sockfd;
//char servip[]="123.206.59.137";
sockfd = Socket(AF_INET, SOCK_STREAM, );
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
inet_pton(AF_INET, argv[], &servaddr.sin_addr);
servaddr.sin_port = htons(SERV_PORT);
Connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); struct fileInfo file_name;
memset(&file_name,,sizeof(file_name));
strncpy(file_name.fileName,argv[],strlen(argv[]));
file_name.len=strlen(argv[]);
Write(sockfd,&file_name,sizeof(file_name));
FILE *fp=fopen(argv[],"r");
if(fp==NULL){
perror("open file failed");
Close(sockfd);
exit(-);
}
printf("open file %s successed\n",argv[]);
int len=;
while ((len=fread(buf,sizeof(char),MAXLINE,fp))>) {
Write(sockfd, buf, len);
}
fclose(fp);
Close(sockfd);
return ;
}

server

#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "wrap.h"
#define MAXLINE 1492
#define SERV_PORT 5555
#define FILE_NAME_LEN 64
struct fileInfo{
char fileName[FILE_NAME_LEN];
int len;
}; int main(int argc, char *argv[]){
struct sockaddr_in serveraddr;
int listenfd;
char str[INET_ADDRSTRLEN]; listenfd=Socket(AF_INET,SOCK_STREAM,);
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family=AF_INET;
serveraddr.sin_addr.s_addr=htonl(INADDR_ANY);
serveraddr.sin_port=htons(SERV_PORT); Bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
Listen(listenfd,); while(){
struct sockaddr_in clientaddr;
socklen_t addrLen=sizeof(clientaddr);
int confd=Accept(listenfd,(struct sockaddr *)&clientaddr,&addrLen);
printf("receive file from %s at port %d\n",
inet_ntop(AF_INET,&clientaddr.sin_addr,str,sizeof(str)),
ntohs(clientaddr.sin_port)
);
int len=;
struct fileInfo file_name;
len=Read(confd,&file_name,sizeof(file_name));
if(len==){
Close(confd);
}else {
printf("file name len=%d\n",file_name.len);
printf("file name is %s\n",file_name.fileName);
}
char buf[MAXLINE];
FILE *fp=fopen(file_name.fileName,"w");
if(!fp){
perror("open file failed");
Close(confd);
Close(listenfd);
}
while((len=Read(confd,buf,MAXLINE))>){
fwrite(buf,sizeof(char),len,fp);
}
if(len==){
fclose(fp);
printf("receive file done\n");
Close(confd);
}
}
Close(listenfd);
return ;
}

wrap.c

#include "wrap.h" 

/*********************************************************************
* * Name : perr_exit
* * Description : exit the function
* * Input : the error string
* * Output :
* * Return :
* * Others : by jzk 2009.12.02
* **********************************************************************/
void perr_exit(const char *s)
{
perror(s);
exit();
} /*********************************************************************
* * Name : Accept
* * Description : accept a connection on a socket
* * Input : fd---a socket that has been created
* * sa---a pointer to a sockaddr structure
* * salenptr---actual size of the peer address
* * Output :
* * Return : the descriptor for the accepted socket
* * Others : by jzk 2009.12.02
* **********************************************************************/
int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n; again:
if((n = accept(fd, sa, salenptr)) < ) {
if((ECONNABORTED == errno) || (EINTR == errno))
goto again;
else
perr_exit("accept error");
} return n;
} /*********************************************************************
* * Name : Bind
* * Description : bind a name to a socket
* * Input : fd---a socket that has been created
* * sa---a pointer to a sockaddr structure
* * salen---the size of the address structure
* * Output :
* * Return :
* * Others : by jzk 2009.12.02
* **********************************************************************/
void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(bind(fd, sa, salen) < )
perr_exit("bind error");
} /*********************************************************************
* * Name : Connect
* * Description : initiate a connection on a socket
* * Input : fd---a socket that has been created
* * sa---a pointer to a sockaddr structure
* * salen---the size of the address structure
* * Output :
* * Return :
* * Others : by jzk 2009.12.02
* **********************************************************************/
void Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
if(connect(fd, sa, salen) < )
perr_exit("connect error");
} /*********************************************************************
* * Name : Listen
* * Description : listen for connections on a socket
* * Input : fd---a socket that has been created
* * backlog---the maximum length to the queue of
* * pending connections
* * Output :
* * Return :
* * Others : by jzk 2009.12.02
* **********************************************************************/
void Listen(int fd, int backlog)
{
if(listen(fd, backlog) < )
perr_exit("listen error");
} /*********************************************************************
* * Name : Socket
* * Description : create an endpoint for communication
* * Input : family---a communication domain
* * type---the communication semantics
* * protocol---a particular protocol for the socket
* * Output :
* * Return : return a descriptor of the socket
* * Others : by jzk 2009.12.02
* **********************************************************************/
int Socket(int family, int type, int protocol)
{
int n; if((n = socket(family, type, protocol)) < )
perr_exit("socket error");
return n;
} /*********************************************************************
* * Name : Read
* * Description : read from a file descriptor
* * Input : fd---a socket that has been created
* * ptr---the buffer which storage the bytes
* * nbytes---the number of bytes read
* * Output :
* * Return : return the number of bytes read
* * Others : by jzk 2009.12.02
* **********************************************************************/
ssize_t Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n; again:
if((n = read(fd, ptr, nbytes)) == -) {
if(EINTR == errno)
goto again;
else
return -;
} return n;
} /*********************************************************************
* * Name : Write
* * Description : write to a file descriptor
* * Input : fd---a socket that has been created
* * ptr---buffer of the bytes
* * nbytes---the number of bytes written
* * Output :
* * Return : return the number of bytes written
* * Others : by jzk 2009.12.02
* **********************************************************************/
ssize_t Write(int fd, const void *ptr, size_t nbytes)
{
ssize_t n; again:
if((n = write(fd, ptr, nbytes)) == -) {
if(EINTR == errno)
goto again;
else
return -;
} return n;
} /*********************************************************************
* * Name : Close
* * Description : close a file descriptor
* * Input : fd---a socket that has been created
* * Output :
* * Return :
* * Others : by jzk 2009.12.02
* **********************************************************************/
void Close(int fd)
{
if(close(fd) == -)
perr_exit("close error");
} /*********************************************************************
* * Name : Readn
* * Description : read from a file descriptor,
* * make sure read the enough bytes
* * Input : fd---a socket that has been created
* * ptr---the buffer which storage the bytes
* * nbytes---the number of bytes read
* * Output :
* * Return : return the number of bytes read
* * Others : by jzk 2009.12.02
* **********************************************************************/
ssize_t Readn(int fd, void *vptr, size_t nbytes)
{
size_t nleft;
size_t nread;
char *ptr; ptr = vptr;
nleft = nbytes; while(nleft > ) {
if((nread = read(fd, ptr, nleft)) < ) {
if(EINTR == errno)
nread = ;
else
return -;
} else if(nread == )
break; nleft -= nread;
ptr += nread;
} return (nbytes-nleft);
} /*********************************************************************
* * Name : Writen
* * Description : write to a file descriptor,
* * make sure write the enough bytes
* * Input : fd---a socket that has been created
* * ptr---the buffer which storage the bytes
* * nbytes---the number of bytes read
* * Output :
* * Return : return the number of bytes read
* * Others : by jzk 2009.12.02
* **********************************************************************/
ssize_t Writen(int fd, const void *vptr, size_t nbytes)
{
size_t nleft;
size_t nwritten;
const char *ptr; ptr = vptr;
nleft = nbytes; while(nleft > ) {
if((nwritten = write(fd, ptr, nleft)) <= ) {
if(nwritten < && EINTR == errno)
nwritten = ;
else
return -;
} nleft -= nwritten;
ptr += nwritten;
} return nbytes;
} static ssize_t my_read(int fd,char *ptr)
{
static int read_cnt;
static char *read_ptr;
static char read_buf[]; if(read_cnt<=){
again:
if((read_cnt=read(fd,read_buf,sizeof(read_buf))<)){
if(errno==EINTR)
goto again;
return -;
}else if(read_cnt==)
return ;
read_ptr=read_buf;
}
read_cnt--;
*ptr=*read_ptr++;
return ;
}
size_t Read_line(int fd,void *vptr,size_t maxlen)
{
ssize_t n,rc;
char c,*ptr; ptr=vptr;
for(n=;n<maxlen;n++){
if((rc=my_read(fd,&c))==){
*ptr++=c;
if(c=='\n')
break;
}else if(rc==){
*ptr=;
return n-;
}else
return -;
}
*ptr=;
return n;
}

wrap.h

#ifndef WRAP_H
#define WRAP_H #include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h> void perr_exit(const char *s); int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);
void Bind(int fd, const struct sockaddr *sa, socklen_t salen);
void Connect(int fd, const struct sockaddr *sa, socklen_t salen);
void Listen(int fd, int backlog); int Socket(int family, int type, int protocol);
void Close(int fd); ssize_t Read(int fd, void *ptr, size_t nbytes);
ssize_t Write(int fd, const void *ptr, size_t nbytes); ssize_t Readn(int fd, void *vptr, size_t n);
ssize_t Writen(int fd, const void *vptr, size_t n); ssize_t Readline(int fd, void *vptr, size_t maxlen); #endif

Makefile

######################################
#
#######################################
#source file
#源文件,自动找所有.c和.cpp文件,并将目标定义为同名.o文件
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) #target you can change test to what you want
#目标文件名,输入任意你想要的执行文件名
TARGET := client server
APP1 := client
APP2 := server MAINS :=$(APP1).o $(APP2).o
#compile and lib parameter
#编译参数
CC := gcc
LIBS := -lpthread -lrt
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H #i think you should do anything here
#下面的基本上不需要做任何改动了
.PHONY : everything objs clean veryclean rebuild everything : $(TARGET) all : $(TARGET) objs : $(OBJS) rebuild: veryclean everything clean :
rm -fr *.so
rm -fr *.o veryclean : clean
rm -fr $(TARGET) $(APP1) :$(APP1).o $(filter-out $(MAINS), $(OBJS))
$(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)
$(APP2) :$(APP2).o $(filter-out $(MAINS), $(OBJS))
$(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) $(LIBS)

最新文章

  1. Jenkins+Maven+SVN快速搭建持续集成环境
  2. Get和Post区别
  3. POI 设置EXCEL单元格格式(日期数字文本等)
  4. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
  5. 收集的冻结table 标题和左侧(完美)
  6. 如何使用SQL Server链接服务器访问DB2 Server
  7. windows MySQL 安装
  8. JAVA中事物以及连接池
  9. [0] C#异常种类
  10. c语言贪吃蛇详解-2.画出蛇
  11. android 集成微博常见问题
  12. tp5入门
  13. Python 第八阶段 学习记录之---算法
  14. Windows10 搭建JAVA环境变量
  15. 基于C/S 结构的IM即时通讯软件--上篇
  16. react-eslintrc
  17. PL/SQL中复制中文再粘贴出现乱码问题的解决【转】
  18. JDBC连接方式有哪几种
  19. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏
  20. 自动化运维工具saltstack04 -- 之jinja模板

热门文章

  1. Qt.5.9.6移植
  2. lintcode_69_二叉树的层次遍历
  3. python 计算提成代码
  4. Navicat Premium Mac 12 破解
  5. MySQL5.6基于GTID的主从复制配置
  6. pwn的一些环境搭建
  7. FullCalendar日历插件(中文API)
  8. Java中的二进制运算出错问题
  9. PAT (Basic Level) Practice 1040 有几个PAT
  10. No module named appium