[关闭]
@Dubyoo 2014-06-13T20:47:32.000000Z 字数 2608 阅读 2181

2014-04-04 Linux网络编程 - 基于TCP的文件传输

  1. /*tcp_server.c*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #include <pthread.h>
  12. #define SIZE 5*1024*1024 //设置发送缓冲区大小
  13. void* sendfile(void * arg) //线程负责发送文件
  14. {
  15. int fd_file ;
  16. int fd_client = *(int*)arg ;
  17. char send_buf[SIZE] ;
  18. int readn ;
  19. fd_file = open("../../../../Downloads/VMware-Workstation-Full-10.0.1-1379776.x86_64.bundle", O_RDONLY) ;
  20. if(fd_file == -1)
  21. {
  22. perror("file open") ;
  23. exit(-1) ;
  24. }
  25. while(memset(send_buf, 0, SIZE) , (readn = read(fd_file, send_buf, SIZE)) > 0)
  26. {
  27. printf("read from file : %d\n", readn) ; //一次读取的文件长度
  28. int sendn = send(fd_client, send_buf, readn, 0) ; //send
  29. printf("send to client : %d\n", sendn) ; //一次发送的文件长度
  30. }
  31. close(fd_file) ;
  32. close(fd_client) ;
  33. printf("send over !\n") ; //发送完成
  34. pthread_exit(NULL) ;
  35. }
  36. int main(int argc, char * argv[]) // ---> ./exe [port]
  37. {
  38. int fd_server ;
  39. pthread_t thd_send ;
  40. fd_server = socket(AF_INET, SOCK_STREAM, 0) ; //socket
  41. if(fd_server == -1)
  42. {
  43. perror("socket") ;
  44. exit(-1) ;
  45. }
  46. struct sockaddr_in server_addr ;
  47. memset(&server_addr, 0, sizeof(server_addr));
  48. server_addr.sin_family = AF_INET ;
  49. server_addr.sin_port = htons(atoi(argv[1])) ; //设置服务器端口号
  50. server_addr.sin_addr.s_addr = INADDR_ANY;
  51. if(-1 == bind(fd_server, (struct sockaddr*)&server_addr, sizeof(server_addr)))
  52. {
  53. perror("bind") ;
  54. close(fd_server) ;
  55. exit(-1) ;
  56. }
  57. listen(fd_server, 5) ; //监听端口是否有新的访问
  58. while(1)
  59. {
  60. int fd_client = accept(fd_server, NULL, NULL) ; //accept
  61. printf("a client connect !\n") ;
  62. pthread_create(&thd_send, NULL, sendfile, (void*)&fd_client) ; //创建线程发送文件
  63. }
  64. close(fd_server) ;
  65. return 0 ;
  66. }
  1. /*tcp_client.c*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <unistd.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9. #include <string.h>
  10. #include <fcntl.h>
  11. #define SIZE 5*1024*1024 //设置接收缓冲区大小
  12. int main(int argc, char * argv[]) // ---> ./exe [ip] [port]
  13. {
  14. char recv_buf[SIZE] ;
  15. int fd_client ;
  16. fd_client = socket(AF_INET, SOCK_STREAM, 0) ; //socket
  17. if(fd_client == -1)
  18. {
  19. perror("socket") ;
  20. exit(-1) ;
  21. }
  22. struct sockaddr_in server_addr ;
  23. memset(&server_addr, 0, sizeof(server_addr)) ;
  24. server_addr.sin_family = AF_INET ;
  25. server_addr.sin_port = htons(atoi(argv[2])) ;
  26. server_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
  27. int iret = connect(fd_client, (struct sockaddr*)&server_addr, sizeof(server_addr)) ; //connect
  28. if(iret == -1)
  29. {
  30. perror("connect") ;
  31. close(fd_client) ;
  32. exit(-1) ;
  33. }
  34. int fd_file = open("./a.out", O_WRONLY | O_CREAT, 0755) ;
  35. if(fd_file == -1)
  36. {
  37. perror("file open") ;
  38. exit(-1) ;
  39. }
  40. int nrecv ;
  41. while(memset(recv_buf, 0, SIZE), (nrecv = recv(fd_client, recv_buf, SIZE, 0)) > 0) //recv
  42. {
  43. printf("recv from server : %d\n", nrecv) ;
  44. int nwrite = write(fd_file, recv_buf, nrecv) ; //接收到的信息写入文件
  45. printf("write to file : %d\n", nwrite) ;
  46. }
  47. printf("Download over~\n") ; //下载完成
  48. close(fd_file) ;
  49. close(fd_client) ;
  50. return 0 ;
  51. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注