linux TCP文件传输

  1. #include <netinet/in.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7. #include <string.h>
  8. #define SERVER_PORT 6666
  9. #define LISTEN_QUEUE 20
  10. #define BUFFER_SIZE 1024
  11. int main(int argc,char **argv)
  12. {
  13. struct sockaddr_in server_addr;
  14. bzero(&server_addr,sizeof(server_addr));//全部置零
  15. //设置地址相关的属性
  16. server_addr.sin_family=AF_INET;
  17. server_addr.sin_addr.s_addr=htons(INADDR_ANY);
  18. server_addr.sin_port=htons(SERVER_PORT);
  19. //创建套接字
  20. int server_socket=socket(AF_INET,SOCK_STREAM,0);
  21. if(server_socket<0)
  22. {
  23. printf("socket create error\n");
  24. exit(1);
  25. }
  26. //绑定端口
  27. if(bind(server_socket,(struct sockaddr*)&server_addr,sizeof(server_addr)))
  28. {
  29. printf("bind error\n");
  30. exit(1);
  31. }
  32. //服务器端监听
  33. if(listen(server_socket,LISTEN_QUEUE))
  34. {
  35. printf("Server listen error\n");
  36. exit(1);
  37. }
  38. //服务器端一直运行
  39. while(1)
  40. {
  41. pid_t pid;
  42. struct sockaddr_in client_addr;
  43. socklen_t length=sizeof(client_addr);
  44. //accept返回一个新的套接字与客户端进行通信
  45. int new_server_socket=accept(server_socket,(struct sockaddr*)&client_addr,&length);
  46. //1*begin******************************************************************************
  47. if(new_server_socket==-1)
  48. {
  49. printf("accept error\n");
  50. continue;
  51. }
  52. else
  53. {
  54. printf("客户端%s连接成功\n",inet_ntoa(client_addr.sin_addr));
  55. pid=fork();
  56. //3*begin**运行子进程*************************************************************
  57. if(pid==0)
  58. {
  59. int data_len,flag=0;
  60. char buffer[BUFFER_SIZE];
  61. // 定义文件流
  62. FILE *stream;
  63. bzero(buffer,BUFFER_SIZE);
  64. strcpy(buffer,"请输入要传输的文件的完整路径:");
  65. strcat(buffer,"\n");
  66. send(new_server_socket,buffer,BUFFER_SIZE,0);
  67. bzero(buffer,BUFFER_SIZE);
  68. //2*begin**服务器接受数据*********************************************
  69. if((stream=fopen("data","w"))==NULL)
  70. {
  71. printf("file open error\n");
  72. exit(1);
  73. }
  74. else
  75. {
  76. bzero(buffer,BUFFER_SIZE);
  77. }
  78. // printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr));
  79. //先将数据接受到缓冲区buffer中,再写入到新建的文件中
  80. while(data_len=recv(new_server_socket,buffer,BUFFER_SIZE,0))
  81. {
  82. flag++;
  83. if(flag==1)
  84. {
  85. printf("正在接收来自%s的文件....\n",inet_ntoa(client_addr.sin_addr));
  86. }
  87. if(data_len<0)
  88. {
  89. printf("接收错误\n");
  90. exit(1);
  91. }
  92. //向文件中写入数据
  93. int write_len=fwrite(buffer,sizeof(char),data_len,stream);
  94. if(write_len>data_len)
  95. {
  96. printf("file write failed\n");
  97. exit(1);
  98. }
  99. bzero(buffer,BUFFER_SIZE);
  100. }
  101. if(flag>0)
  102. printf("%s的文件传送完毕\n",inet_ntoa(client_addr.sin_addr));
  103. if(flag==0)
  104. printf("%s的文件传输失败\n",inet_ntoa(client_addr.sin_addr));
  105. //2*end**服务器接受数据****************************************************
  106. // rename("data",inet_ntoa(client_addr.sin_addr));
  107. fclose(stream);
  108. rename("data",inet_ntoa(client_addr.sin_addr));
  109. exit(1);
  110. }
  111. //3*end**运行子进程**********************************************************
  112. else
  113. {
  114. close(new_server_socket);
  115. }
  116. }
  117. //1*end**************************************************************************************
  118. close(new_server_socket);
  119. }
  120. return 0;
  121.  
  1. 客户端:
 
  1. #include <netinet/in.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <arpa/inet.h>
  9. #define SERVER_PORT 6666
  10. #define BUFFER_SIZE 1024
  11. int main(int argc,char **argv)
  12. {
  13. if(argc!=2)
  14. {
  15. printf("参数错误,清输入两个参数\n");
  16. exit(1);
  17. }
  18. FILE *stream;
  19. struct sockaddr_in server_addr;
  20. bzero(&server_addr,sizeof(server_addr)); //把一段内存区的内容全部设置为0
  21. server_addr.sin_family = AF_INET; //internet协议族
  22. server_addr.sin_addr.s_addr = inet_addr(argv[1]);
  23. server_addr.sin_port = htons(SERVER_PORT);
  24. int sfd;
  25. sfd=socket(AF_INET,SOCK_STREAM,0);
  26. if(sfd<0)
  27. {
  28. printf("socket error\n");
  29. exit(0);
  30. }
  31. if(connect(sfd,(struct sockaddr*)&server_addr,sizeof(server_addr)) < 0)
  32. {
  33. printf("Can Not Connect To %s\n",argv[1]);
  34. exit(1);
  35. }
  36. char buffer[BUFFER_SIZE];
  37. bzero(buffer,BUFFER_SIZE);
  38. int length = recv(sfd,buffer,BUFFER_SIZE,0);
  39. if(length < 0)
  40. {
  41. printf("Recieve Data From Server %s Failed!\n", argv[1]);
  42. exit(1);
  43. }
  44. printf("\n%s\n",buffer);
  45. bzero(buffer,BUFFER_SIZE);
  46. // fgets(buffer,1024,stdin);
  47. // char filename[100];
  48. gets(buffer);
  49. //打开文件流
  50. if((stream=fopen(buffer,"r"))==NULL)
  51. {
  52. printf("the file was not opened\n");
  53. exit(1);
  54. }
  55. bzero(buffer,BUFFER_SIZE);
  56. printf("正在传输...\n");
  57. int len=0;
  58. //不断读取并发送数据
  59. while((len=fread(buffer,1,1024,stream))>0)
  60. {
  61. // printf("len=%d\n",len);
  62. if(send(sfd,buffer,len,0)<0)
  63. {
  64. printf("send file error\n");
  65. break;
  66. }
  67. bzero(buffer,BUFFER_SIZE);
  68. }
  69. if(fclose(stream))
  70. {
  71. printf("file close error\n");
  72. exit(1);
  73. }
  74. close(sfd);
  75. return 0;
  76. }

 

在服务器端的程序中,当一个客户端连接,就创建一个独立的进程

你可能感兴趣的:(linux,TCP文件传输)