forked from YuanDaYu002/ToolBank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp_recv.c
196 lines (159 loc) · 4.73 KB
/
tcp_recv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************
server端程序:
从设备端(client)接收一个文件到本地主机(功能和LiteOS自带的 http 命令一样)
1.主机端运行server程序在后台监听
2.设备端配好网络后,在设备端执行命令调用 client端程序
********************************************************************/
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
typedef struct _file_info_t
{
unsigned int filesize; //文件大小
unsigned char file_name[32]; //文件名
}file_info_t;
#define portnum 5555 //端口有可能端口被占用,可以改端口才能通
#define RECV_BUF_SIZE (1024*1024)
int main(int argc,char**argv)
{
char need_help;
need_help = !strcmp(argv[2],"-h")||!strcmp(argv[2],"-help");
printf("need_help = %d\n",need_help);
if(argc == 0||need_help)
{
printf("usage: tcp_send\n\
Atteion : need to be used in conjunction with server process: tcp_recv\n\
eg : \n\
<linux computer>: tcp_recv\
<board>: tcp_send /jffs0/fmp4.mp4 192.168.XXX.XXX\n\n");
return 0;
}
int sockfd;
int new_fd;//建立连接后会返回一个新的fd
struct sockaddr_in sever_addr; //服务器的IP地址
struct sockaddr_in client_addr; //设备端的IP地址
char *buffer = (char*)malloc(RECV_BUF_SIZE);
if(NULL == buffer)
{
perror("malloc failed!\n");
return -1;
}
char *p_buffer = buffer;
memset(buffer,0,RECV_BUF_SIZE);
int nbyte;
int checkListen;
file_info_t file_info = {0}; //用于接收文件描述信息
unsigned int countbytes = 0;
int tmp_file; //临时文件描述符
/***为了消除accept函数第3个参数的类型不匹配问题的警告***********/
int sockaddr_size = sizeof(struct sockaddr);
/******************************************************/
//1.创建套接字
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1)
{
printf("create socket error!\n");
return -1;
}
printf("create socket success!\n");
//2.1设置要绑定的服务器地址
bzero(&sever_addr,sizeof(struct sockaddr_in));
sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(portnum);//将主机字节序转换成网络字节序
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY);//绑定任意的IP地址,要和网络上所有的IP地址通讯
//2.2绑定地址
if(bind(sockfd,(struct sockaddr*)&sever_addr,sizeof(struct sockaddr)) < 0)
{
printf("bind socket error!\n");
return -1;
}
printf("bind socket success!\n");
//3.监听端口
printf("into listen portnum(%d)......!\n",portnum);
checkListen = listen(sockfd,5);
if(checkListen < 0)
{
perror("socket listen error!\n");
return -1;
}
do
{
//4.等待连接
new_fd = accept(sockfd,(struct sockaddr*)(&client_addr),(socklen_t *)(&sockaddr_size));
if(new_fd<0)
{
printf("accept eror!\n");
return -1;
}
//printf("sever get connection from %s\n",inet_ntoa(client_addr.sin_addr.s_addr));
//5.接收文件大小信息
recv(new_fd,&file_info,sizeof(file_info),0);
printf("receive file size = %ld bytes\n",file_info.filesize);
printf("receive file name = %s \n",file_info.file_name);
if(file_info.filesize <= 0)return -1;
//解析出文件的最终名字(如若文件名带路径分隔符“/”,则需要截取最后一段)
unsigned char*name = file_info.file_name;
unsigned char*flag = NULL;
while(1)
{
flag = strstr(name,"/");
if(NULL == flag)
{
printf("final name : %s\n",name);
memcpy(file_info.file_name,name,strlen(name)+1);
break;
}
else
{
name = flag + 1; //跳过“/”
}
}
//如果文件已经存在,则删除原来的文件
if(0 == access(file_info.file_name,F_OK))
{
if(0 == remove(file_info.file_name))
{
printf("remove old file success!\n");
}
else
{
printf("remove old file failed!\n");
return -1;
}
}
//创建本地对应的文件
tmp_file = open(file_info.file_name, O_RDWR| O_CREAT,0660);
if( tmp_file < 0)
{
printf("open [%s] error !\n",file_info.file_name);
return -1;
}
printf("open [%s] success !\n",file_info.file_name);
//5.1接收数据
while(1)
{
if(countbytes >= file_info.filesize)break;
nbyte = recv(new_fd,p_buffer,file_info.filesize - countbytes,0);
countbytes = countbytes + nbyte;
//接收到的数据及时写入到文件
unsigned int tmp_ret = write(tmp_file, p_buffer, nbyte);
if(tmp_ret < 0)
{
perror("write file error!\n");
return -1;
}
}
printf("success ! server received (%d)bytes!\n",countbytes);
}while(0);
close(tmp_file);
close(new_fd);
close(sockfd);
free(buffer);
return 0;
}