今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作!

在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中,如果我们使用TCP传输,会造成传输速度较慢的情况,所以我们在进行文件传输的过程中,最好要使用UDP传输。

在其中,我们需要写两个程序,一个客户端,一个服务端,在一个终端中,先运行服务端,在运行客户端,在服务端和客户端都输入IP地址和端口号,注意服务端和客户端的端口号要相同,然后选择功能,在linux网络编程中,使用UDP进行传输属于比较简单的操作,所以直接上代码吧,详细的讲解我将会在之后上传!

client(客户端):

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include "protocol.h"
  10.  
  11. /*--------------------------*/
  12. int socketfd ;
  13. int addrlen;
  14. struct sockaddr_in server;
  15. struct protocol sentbuf;
  16. struct protocol recvbuf;
  17. int num;
  18. char ip[];
  19. int port;
  20. int choice ;
  21. char filename[];
  22.  
  23. /*--------------------------*/
  24. void ShowMenu();
  25. void DownLoad();
  26. void UpLoad();
  27. void ShutDown();
  28.  
  29. int main(){
  30. /*---------------------------*/
  31. socketfd = socket(AF_INET , SOCK_DGRAM , );
  32. if(socketfd == -){
  33. perror("socket failed!\n");
  34. exit();
  35. }
  36. /*---------------------------*/
  37. printf("please input ip of server:\n");
  38. scanf("%s",ip);
  39. printf("please input port of server:\n");
  40. scanf("%d",&port);
  41. /*---------------------------*/
  42. bzero(&server , sizeof(server));
  43. server.sin_family = AF_INET;
  44. server.sin_port = htons(port);
  45. server.sin_addr.s_addr = inet_addr(ip);
  46. addrlen = sizeof(server);
  47. /*---------------------------*/
  48. while(){
  49. ShowMenu();
  50. scanf("%d",&choice);
  51. if(choice == DOWNLOAD){
  52. printf("client download!\n");
  53. DownLoad();
  54. }
  55. else if(choice == UPLOAD){
  56. UpLoad();
  57. }
  58. else if(choice == SHUTDOWN){
  59. printf("client shutdown!\n");
  60. ShutDown();
  61. break;
  62. }
  63. else{
  64. printf("please input the right choice!\n");
  65. }
  66. }
  67. close(socketfd);
  68. return ;
  69. }
  70.  
  71. void ShowMenu(){
  72. printf("please make a choice:\n");
  73. printf("0:shutdown!\n");
  74. printf("1:download!\n");
  75. printf("2:upload!\n");
  76. }
  77.  
  78. void DownLoad(){
  79. bzero(&recvbuf , sizeof(recvbuf));
  80. bzero(&sentbuf , sizeof(sentbuf));
  81. bzero(filename , sizeof(filename));
  82. printf("please input the filename:\n");
  83. scanf("%s",sentbuf.buf);
  84. sentbuf.command = DOWNLOAD;
  85. sendto(socketfd , &sentbuf , sizeof(sentbuf), , (struct sockaddr*)&server , sizeof(server));
  86. bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
  87. recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&server , &addrlen);
  88. printf("recvbuf:%d\n",recvbuf.command);
  89. if(recvbuf.command == YES){
  90. printf("YES!\n");
  91. int choice_1;
  92. printf("if you input a 5 , the file transmission start!\n");
  93. scanf("%d",&choice_1);
  94. if(choice_1 == START){
  95. sentbuf.command = START;
  96. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
  97. int no = ;
  98. int fd = open(filename , O_CREAT | O_TRUNC | O_WRONLY , );
  99. if(fd < ){
  100. perror("creat file is failed!\n");
  101. exit();
  102. }
  103. bzero(&recvbuf , sizeof(recvbuf));
  104. while( ( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&server , &addrlen)) > ){
  105. if( recvbuf.command == CONTENT ){
  106. if(no == recvbuf.no){
  107. write(fd , recvbuf.buf , recvbuf.len);
  108. bzero(&recvbuf , sizeof(recvbuf));
  109. }
  110. else{
  111. perror("The file no is not same, Some massage is missed!error occured!\n");
  112. break;
  113. }
  114. }
  115. if( recvbuf.command == END){
  116. close(fd);
  117. printf("transmission is successful!\n");
  118. break;
  119. }
  120. }
  121. }
  122. }
  123. else if(recvbuf.command == NO){
  124. perror("No such file on server!\n");
  125. }
  126. else{
  127. perror("recvbuf.command error!\n");
  128. exit();
  129. }
  130. }
  131.  
  132. void ShutDown(){
  133. sentbuf.command = SHUTDOWN;
  134. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
  135. printf("client is end!\n");
  136. }
  137.  
  138. void UpLoad(){
  139. bzero(&recvbuf , sizeof(recvbuf));
  140. bzero(&sentbuf , sizeof(sentbuf));
  141. bzero(filename , sizeof(filename));
  142. printf("please input you want to upload filename:\n");
  143. scanf("%s",sentbuf.buf);
  144. sentbuf.command = UPLOAD;
  145. sendto(socketfd , &sentbuf , sizeof(sentbuf), , (struct sockaddr*)&server , sizeof(server));
  146. bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
  147. int fd ;
  148. fd = open(filename , O_RDONLY);
  149. if(fd < ){
  150. perror("The file is not exist!\n");
  151. exit();
  152. }
  153. recvfrom(socketfd , &recvbuf , sizeof(recvbuf), , (struct sockaddr*)&server , &addrlen);
  154. if( recvbuf.command == START ){
  155. int no = ;
  156. while( ( num = read(fd , sentbuf.buf , INFOLEN)) > ){
  157. sentbuf.no = no ;
  158. sentbuf.command = CONTENT;
  159. sentbuf.len = strlen(sentbuf.buf);
  160. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server,sizeof(server));
  161. no++;
  162. bzero(&sentbuf , sizeof(sentbuf));
  163. }
  164. bzero(&sentbuf , sizeof(sentbuf));
  165. sentbuf.command = END;
  166. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
  167. }
  168. else if(recvbuf.command == NO){
  169. printf("not transmission!\n");
  170. }
  171. else{
  172. perror("error! wrong choice!\n");
  173. }
  174. }

server(服务端):

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include "protocol.h"
  10.  
  11. int socketfd;
  12. int addrlen;
  13. struct sockaddr_in server;
  14. struct sockaddr_in client;
  15. struct protocol sentbuf;
  16. struct protocol recvbuf;
  17. int num;
  18. char ip[];
  19. int port;
  20. int choice;
  21. char filename[];
  22.  
  23. void DownLoad();
  24. void ShutDown();
  25. void UpLoad();
  26.  
  27. int main(){
  28. socketfd = socket( AF_INET , SOCK_DGRAM , );
  29. if( socketfd == -){
  30. perror("socket failed!\n");
  31. exit();
  32. }
  33.  
  34. printf("please input client ip:\n");
  35. scanf("%s" , ip);
  36. printf("please input client port:\n");
  37. scanf("%d",&port);
  38.  
  39. bzero(&server , sizeof(server));
  40. server.sin_family = AF_INET;
  41. server.sin_port = htons(port);
  42. server.sin_addr.s_addr = inet_addr(ip);
  43.  
  44. int bin = bind(socketfd , (struct sockaddr*)&server , sizeof(server));
  45. if(bin == -){
  46. perror("bind failed!\n");
  47. exit();
  48. }
  49.  
  50. addrlen = sizeof(client);
  51. //char filename[100];
  52.  
  53. while(){
  54. bzero(&recvbuf , sizeof(recvbuf));
  55. num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen);
  56. choice = recvbuf.command;
  57. bcopy(recvbuf.buf ,filename, sizeof(recvbuf.buf));
  58. printf("%s\n",filename);
  59. if(choice == DOWNLOAD){
  60. printf("kkkkkkkk\n");
  61. DownLoad();
  62. }
  63. else if(choice == SHUTDOWN){
  64. printf("server will shutdown!\n");
  65. ShutDown();
  66. break;
  67. }
  68. else if(choice == UPLOAD){
  69. UpLoad();
  70. }
  71. }
  72. return ;
  73. }
  74.  
  75. void DownLoad(){
  76. char buf[];
  77. int fd ;
  78. printf("11111\n");
  79. printf("%s\n",filename);
  80. fd = open((filename) , O_RDONLY);
  81. printf("fd:%d\n",fd);
  82. bzero(&sentbuf , sizeof(sentbuf));
  83. if(fd < ){
  84. sentbuf.command = NO;
  85. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
  86. printf("no such file!\n");
  87. exit();
  88. }
  89. else{
  90. sentbuf.command = YES;
  91. printf("YES!\n");
  92. sendto(socketfd , &sentbuf, sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
  93. recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen);
  94. if(recvbuf.command == START){
  95. int no = ;
  96. while((num = read(fd , sentbuf.buf , INFOLEN)) > ){
  97. sentbuf.no = no ;
  98. sentbuf.command = CONTENT;
  99. sentbuf.len = strlen(sentbuf.buf);
  100. sendto(socketfd , &sentbuf , sizeof(sentbuf),,(struct sockaddr*)&client , sizeof(client));
  101. no++;
  102. bzero(&sentbuf , sizeof(sentbuf));
  103. }
  104. bzero(&sentbuf , sizeof(sentbuf));
  105. sentbuf.command = END;
  106. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
  107. }
  108. }
  109. }
  110.  
  111. void ShutDown(){
  112. printf("Now server is shutdown!\n");
  113. }
  114.  
  115. void UpLoad(){
  116. //bzero(&recvbuf , sizeof(recvbuf));
  117. bzero(&sentbuf , sizeof(sentbuf));
  118. bzero(&filename , sizeof(filename));
  119. //recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen);
  120. printf("4:NO 5:START\n");
  121. scanf("%d" , &sentbuf.command);
  122. sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
  123. if( sentbuf.command == START){
  124. int no = ;
  125. printf("filename:%s\n",recvbuf.buf);
  126. int fd = open(recvbuf.buf , O_CREAT | O_TRUNC | O_WRONLY , );
  127. if(fd < ){
  128. perror("create file failed!\n");
  129. exit();
  130. }
  131. bzero(&recvbuf , sizeof(recvbuf) );
  132. while(( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen)) > ){
  133. if( recvbuf.command == CONTENT ){
  134. if( no == recvbuf.no ){
  135. write(fd , recvbuf.buf , recvbuf.len);
  136. bzero(&recvbuf , sizeof(recvbuf));
  137. }
  138. else{
  139. perror("The file no is not same . Some massage is missed!\n");
  140. break;
  141. }
  142. }
  143. if( recvbuf.command == END ){
  144. close(fd);
  145. printf("transmission is successful!\n");
  146. break;
  147. }
  148. }
  149. }
  150. else if( sentbuf.command == NO ){
  151. printf("The file can't transmission!\n");
  152. }
  153. else{
  154. perror("please input right choice!\n");
  155. exit();
  156. }
  157. }

makefile:

  1. main:udpserver.o udpclient.o
  2. gcc -o udpserver udpserver.o
  3. gcc -o udpclient udpclient.o
  4. udpserver.o:udpserver.c
  5. gcc -c udpserver.c
  6. udpclient.o:udpclient.c
  7. gcc -c udpclient.c

linux网络环境下socket套接字编程(UDP文件传输)的更多相关文章

  1. linux网络编程-(socket套接字编程UDP传输)

    今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中, ...

  2. [网络编程之Socket套接字介绍,套接字工作流程,基于TCP协议的套接字程序]

    [网络编程之Socket套接字介绍,套接字工作流程,基于TCP协议的套接字程序] 为何学习socket套接字一定要先学习互联网协议: 1.首先:要想开发一款自己的C/S架构软件,就必须掌握socket ...

  3. socket套接字编程 HTTP协议

    socket套接字编程  套接字介绍  1. 套接字 : 实现网络编程进行数据传输的一种技术手段  2. Python实现套接字编程:import  socket  3. 套接字分类 >流式套接 ...

  4. socket 套接字编程

    今日内容 socket 套接字编程 简易服务端与客户端代码实现 通信循环 黏包现象(TCP协议) 报头制作.struct 模块.封装形式 内容详细 一.socket 套接字编程 实现一款能够进行数据交 ...

  5. Linux之socket套接字编程20160704

    介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...

  6. 网络编程之socket套接字

    目录 socket套接字简介 socket模块 通信循环 代码优化 连接循环 半连接池 黏包问题 解决黏包问题 黏包问题特殊情况(文件过大) socket套接字简介 由于操作OSI七层是所有C/S架构 ...

  7. java 25 - 3 网络编程之 Socket套接字

    Socket Socket套接字: 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字. Socket原理机制: 通信的两端都有Socket. 网络通信其实就是Socket ...

  8. 19、网络编程 (Socket套接字编程)

    网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用层.传输层.网络层和链路层,每层分别负责不同的通信功能,接下来针对这四层进行详细地讲解. 链路层:链路层是用于定义物理传输通道,通常是对某些 ...

  9. 31_网络编程(Socket套接字编程)_讲义

    今日内容介绍 1.网络三要素及传输协议 2.实现UDP协议的发送端和接收端 3.实现TCP协议的客户端和服务器 4.TCP上传文件案例 01网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用 ...

随机推荐

  1. 实体类调用泛型父类中的静态方法中执行CRUD——第二版

    using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; namespa ...

  2. HTML 图像<img>

    定义和用法: img元素向网页中嵌入一副图像. 请注意:从技术上讲,<img>标签并不会在网页中插入图像,而是从网页上链接图像.<img>标签创建的是被引用图像的占位空间. 属 ...

  3. C++ 中 ZeroMemory、memset 危险需慎用

    使用C/C++编程时,常使用ZeroMemory.memset或 “={0}”来对结构体对象进行初始化或清零.然而这三种方式都有各自的特点,使用时需谨慎,否则容易出现严重错误,本人今日解决一个导致宕机 ...

  4. Android doc打开太慢

    C:\Windows\System32\drivers\etc\HOSTS 127.0.0.1 fonts.googleapis.com 127.0.0.1 www.google.com 127.0. ...

  5. Java 并发和多线程(一) Java并发性和多线程介绍[转]

    作者:Jakob Jenkov 译者:Simon-SZ  校对:方腾飞 http://tutorials.jenkov.com/java-concurrency/index.html 在过去单CPU时 ...

  6. 为什么高手离不了Linux系统?这就是我的理由

    摘要: 通过本文来记录下我在Linux系统的学习经历,聊聊我为什么离不了Linuxx系统,同时也为那些想要尝试Linux而又有所顾忌的用户答疑解惑,下面将为你介绍我所喜欢的Linux系统,这里有一些你 ...

  7. bzoj 2761: [JLOI2011]不重复数字

    #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #d ...

  8. hidesBottombarWhenPushed的副作用

    在UITabbarController包含的UINavigationController应用中,如果UINavigationController某一页(某个level)需要隐藏Tabbar,之前的做法 ...

  9. 网页首页制作总结(div+css+javascript)

    一.对网页整体布局,分几个版块 如下图所示: 确定布局之后,规划好网页,准备素材,按照标准文档流的顺序,从上到下,从左到右写入代码. 以上图为例,分为两部分,红色的主体部分和页脚.主体部分分割为头部. ...

  10. 解决SSH会话连接超时问题

    用SSH客户端连接linux服务器时,经常会出现与服务器会话连接中断现象,照成这个问题的原因便是SSH服务有自己独特的会话连接机制.记得在一年前就有朋友问过我这个问题,那时候我便是草草打发,结果自己现 ...