LINUX C 基于TCP/IP协议的SOCKET收发文件的小例子。
自己也弄的稀里糊涂的…
大致步骤:
————————————————————————————————————————–
SERVER——————————————–CLIENT
1. RECV FILE NAME<—————————— SEND FILE NAME
2. GET FILE SEEK
3. SEND FILE SEEK POS ———————> RECV FILE SEEK POS
4. | FILE OPEN -> FILE SEEK -> FILE READ
5. SOCKET READ <——————————- SOCKET WRITE
6. FILE WIRTE & SAVE SEEK POS
—————————————————————————————————————————-
服务器端:
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
/******** http://blog.csdn.net/robertkun ********/ /******* 服务器程序 (server.c) ************/ // linux 下读取大于2GB文件时,需指定 #define _FILE_OFFSET_BITS 64 #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <netdb.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/types.h> #include <arpa/inet.h> #include <fcntl.h> #include <pthread.h> #include<semaphore.h> // 定义包的大小为512KB #define PACK_SIZE 1024*512 #define THREAD_NUM 5 pthread_t a_thread[THREAD_NUM]; void* WorkThread(void* args); void PrintTrdID(const char* s); unsigned long GetFileSeek_long(const char* file_name); void GetFileSeek_char(const char* file_name, char* cseek_pos); void WriteFileSeek(const char* file_name, unsigned long seek_pos); struct TrdItem { int sockfd; struct sockaddr_in addr; }; int main(int argc, char *argv[]) { // 设置输出缓冲 setvbuf(stdout, NULL, _IONBF, 0); fflush(stdout); int sockfd,new_fd; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size,portnumber; char hello[]="Hello! Are You Fine?\n"; if((portnumber=atoi("8080"))<0) { fprintf(stderr,"Usage:%s portnumber\a\n",argv[0]); exit(1); } /* 服务器端开始建立socket描述符 */ if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { fprintf(stderr,"Socket error:%s\n\a",strerror(errno)); exit(1); } /* 服务器端填充 sockaddr结构 */ bzero(&server_addr,sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port=htons(portnumber); /* 捆绑sockfd描述符 */ if(bind(sockfd,(struct sockaddr *)(&server_addr),sizeof(struct sockaddr))==-1) { fprintf(stderr,"Bind error:%s\n\a",strerror(errno)); exit(1); } /* 监听sockfd描述符 */ if(listen(sockfd,5)==-1) { fprintf(stderr,"Listen error:%s\n\a",strerror(errno)); exit(1); } struct TrdItem item; item.sockfd = sockfd; item.addr = client_addr; // 创建5个线程负责监听 int i = 0; for(i=0; i<THREAD_NUM; ++i) { int err = 0; err = pthread_create(&a_thread[i], NULL, WorkThread, (void*)&item); if(err != 0) { printf("Cannot create thread ... %s\n", strerror(err)); } } // 等待5个线程结束 for(i=0; i<THREAD_NUM; ++i) { int err = 0; err = pthread_join(a_thread[i] , NULL); if(err != 0) { printf("the thread id : %d ends failes, %s\n", i, strerror(err)); } } PrintTrdID("\n\nMain thread."); sleep(5); close(sockfd); exit(0); } void* WorkThread(void* args) { while(1) { fprintf(stderr, "server is listening!\n"); /* 服务器阻塞,直到客户程序建立连接 */ int sin_size = 0; int new_fd = 0; struct TrdItem t_item = *(struct TrdItem*)args; sin_size=sizeof(struct sockaddr_in); if( ( new_fd = accept(t_item.sockfd,(struct sockaddr *)(&t_item.addr),(socklen_t*)&sin_size ) ) == -1) { fprintf(stderr,"Accept error:%s\n\a",strerror(errno)); exit(1); } fprintf(stderr,"Server get connection from %s\n", inet_ntoa(t_item.addr.sin_addr)); // 打印线程ID PrintTrdID("Child thread."); int order_id = 0; unsigned long read_size = 0; unsigned long file_len = 0; unsigned long seek_pos = 0; char file_name[128] = {'\0'}; char file_info[1024] = {'\0'}; // 读取指令 printf("\n\nWaiting for read file info!\n"); int nn = 0; if(nn = read(new_fd, file_info, 1024)) { // 指令ID int id_h = (int)file_info[0]<<8; order_id = id_h + (int)file_info[1]; // 文件长度 // 高16位 unsigned long len_hig_1 = 0; memcpy(&len_hig_1, &file_info[2], sizeof(file_info[2])); unsigned long len_hig_2 = 0; memcpy(&len_hig_2, &file_info[3], sizeof(file_info[3])); unsigned long len_hig = len_hig_1 * 256 + len_hig_2; // 低16位 unsigned long len_low_1 = 0; memcpy(&len_low_1, &file_info[4], sizeof(file_info[4])); unsigned long len_low_2 = 0; memcpy(&len_low_2, &file_info[5], sizeof(file_info[5])); int len_low = len_low_1 * 256 + len_low_2; file_len = len_hig * 256 * 256 + len_low; // 文件名称 strncpy(file_name, &file_info[6], strlen(&file_info[6])); printf("order = %d, %lu, %s\n", order_id, file_len, file_name); if((strlen(file_name) == 0) || (file_len == 0)) { printf("Read file info error!\n File_name or file_len is zero!\n"); close(new_fd); close(t_item.sockfd); continue; } // 获取文件的断点位置.(保存在根据文件名生成的.tmp文件中) char* cseek_pos = (char*)calloc(128,sizeof(char)); GetFileSeek_char(file_name, cseek_pos); seek_pos = GetFileSeek_long(file_name); if(cseek_pos != NULL) { // 获取断点成功,发送到客户端 if(write(new_fd, cseek_pos, 128)==-1) { fprintf(stderr,"Write Error:%s\n",strerror(errno)); close(new_fd); close(t_item.sockfd); continue; } } } else { printf("Read file info error!\n"); close(new_fd); close(t_item.sockfd); continue; } // 写入文件 printf("\n\nWaiting for read file content!\n"); FILE* pf = fopen(file_name, "ab+"); if(pf == NULL) { printf("Open file error!\n"); close(new_fd); } char buff[PACK_SIZE] = {'\0'}; int idx = 0; while(read_size <= file_len) { int rlen = read(new_fd, buff, PACK_SIZE); if(rlen) { int wn = fwrite(buff, sizeof(char), rlen, pf); read_size += rlen; seek_pos += rlen; // 接收一定数据后, 显示接收信息 if(idx++%5000 == 0) { printf("%d, file len = %ld, file seek = %ld, file read = %ld \n\n", idx, file_len, seek_pos, read_size); } } else { printf("Read over!...%d\n", rlen); break; } } // 客户端断开连接时, 将断点位置保存 WriteFileSeek(file_name, seek_pos); printf("File Name = %s, File len = %ld , Already read size = %ld, file seek = %ld\n", file_name, file_len, read_size, seek_pos); /* 这个通讯已经结束 */ fclose(pf); close(new_fd); } return (void*)NULL; } void PrintTrdID( const char* s ) { pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); printf("%s pid = %u tid = %u (0x%x)\n", s, pid, tid, tid); } unsigned long GetFileSeek_long(const char* file_name) { // 获取断点位置 char* file_seek = (char*)calloc(1024,sizeof(char)); strcpy(file_seek, file_name); char temp[] = {".tmp"}; strcat(file_seek, temp); FILE* pf = fopen(file_seek, "r"); if(pf == NULL) { printf("Open file error!\n"); return 0; } char cseek_pos[20] = {'\0'}; fseek(pf, 0, SEEK_SET); fread(cseek_pos, sizeof(char), sizeof(cseek_pos), pf); fclose(pf); unsigned long seek = 0; seek = strtol(cseek_pos, NULL, 10); printf("file seek position (ulong) = %ld\n\n", seek); return seek; } void GetFileSeek_char(const char* file_name, char* cseek_pos) { // 获取断点位置 char* file_seek = (char*)calloc(1024,sizeof(char)); strcpy(file_seek, file_name); char temp[] = {".tmp"}; strcat(file_seek, temp); FILE* pf = fopen(file_seek, "r"); if(pf == NULL) { printf("Open file error!\n"); return; } fseek(pf, 0, SEEK_SET); fread(cseek_pos, sizeof(char), 128, pf); fclose(pf); printf("file seek position (char) = %s %d \n\n", cseek_pos, sizeof(cseek_pos)); } void WriteFileSeek(const char* file_name, unsigned long seek_pos) { // 写入文件 char* file_seek = (char*)calloc(1024,sizeof(char)); strcpy(file_seek, file_name); char temp[] = {".tmp"}; strcat(file_seek, temp); FILE* pf = fopen(file_seek, "wb+"); if(pf == NULL) { printf("Open file error!\n"); return; } int wn = fprintf(pf, "%ld", seek_pos); fclose(pf); if(wn == -1) { printf("Write file seek position! Error! %d\n\n\n", wn); return; } } |
客户端:
1 |
//现在是基于QT的..回头再传.. |
MAKEFILE:
服务器端:
1 2 3 4 5 6 7 8 |
object=server.o server.o: gcc -g -c server.c gcc -o server $(object) clean: rm server $(object) |
客户端:
1 2 3 4 5 6 7 8 |
object=client.o client.o: gcc -g -c client.c gcc -o client $(object) clean: rm -rf client $(object) |
发表评论
要发表评论,您必须先登录。