今天我们来介绍一下在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. /*-----------------------变量声明区------------------*/
  12. int socketfd;
  13. int addrlen;
  14. struct sockaddr_in server;
  15. struct sockaddr_in client;
  16. struct protocol sentbuf;
  17. struct protocol recvbuf;
  18. int num;
  19. char ip[];
  20. int port;
  21. int choice;
  22.  
  23. int main(){
  24.  
  25. /*-------------create UDP socket------------*/
  26. if((socketfd = socket(AF_INET,SOCK_DGRAM,)) == -){
  27. perror("socket error\n");
  28. exit();
  29. }
  30.  
  31. /*-----------------IO-----------------------*/
  32. printf("Please input the ip:\n");
  33. scanf("%s",ip);
  34. printf("Please input the port:\n");
  35. scanf("%d",&port);
  36.  
  37. /*-----------------bind----------------------*/
  38. bzero(&server,sizeof(server));
  39. server.sin_family = AF_INET;
  40. server.sin_port = htons(port);
  41. server.sin_addr.s_addr = inet_addr(ip);
  42. if (bind(socketfd,(struct sockaddr *)&server,sizeof(server)) == -){
  43. perror("bind error\n");
  44. exit();
  45. }
  46.  
  47. /*---------------------调试信息------------
  48. addrlen = sizeof(client);
  49. recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
  50. num = strlen(recvbuf.buf);
  51. recvbuf.buf[num] = '\0';
  52. printf("command %d\n",recvbuf.command );
  53. printf("len %d\n",recvbuf.len );
  54. printf("no %d\n", recvbuf.no);
  55. printf("buf %s\n",recvbuf.buf ); */
  56.  
  57. addrlen = sizeof(client);
  58. while(){
  59. bzero(&recvbuf,sizeof(recvbuf));
  60. num =recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&client,&addrlen);
  61. choice = recvbuf.command;
  62. if(choice == DOWNLOAD){
  63. char buf[];
  64. int fd;
  65. fd = open((recvbuf.buf),O_RDONLY);
  66. if(fd <){
  67. sentbuf.command = NO;
  68. sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
  69. printf("no such file!\n");
  70. exit();
  71. }
  72. else{
  73. sentbuf.command = YES;
  74. sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
  75. recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&client,&addrlen);
  76. if(recvbuf.command == START){
  77. int no =;
  78. while((num = read(fd,sentbuf.buf,INFOLEN)) >){
  79. sentbuf.no = no;
  80. sentbuf.command = CONTENT;
  81. sentbuf.len = strlen(sentbuf.buf);
  82. sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
  83. no++;
  84. bzero(&sentbuf,sizeof(sentbuf));
  85. }
  86. bzero(&sentbuf,sizeof(sentbuf));
  87. sentbuf.command = END;
  88. sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
  89. }
  90. }
  91. }
  92. else if(choice == UPLOAD){
  93. printf("The client want to upload the file: %s\n",recvbuf.buf);
  94. printf("Please choice start or no? \n");
  95. printf("5 :start, 4: no\n");
  96. scanf("%d",&sentbuf.command);
  97. sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
  98. if(sentbuf.command ==START){
  99. int no =;
  100. int fd =open(recvbuf.buf,O_CREAT | O_TRUNC |O_WRONLY,);
  101. if(fd < ){
  102. perror("create file error\n");
  103. exit();
  104. }
  105. bzero(&recvbuf,sizeof(recvbuf));
  106. while((num = recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&server,&addrlen)) >){
  107. if(recvbuf.command == CONTENT){
  108. if(no == recvbuf.no){
  109. write(fd,recvbuf.buf,recvbuf.len);
  110. printf("kkk%s\n",recvbuf.buf );
  111. bzero(&recvbuf,sizeof(recvbuf));
  112. }
  113. else{
  114. perror("The file no is not same.Some message is missed! error occured! \n");
  115. break;
  116. }
  117. }
  118. if(recvbuf.command == END){
  119. close(fd);
  120. printf("transmission is successful!\n");
  121. break;
  122. }
  123. }
  124. }
  125. else if(sentbuf.command == NO){
  126. printf("Not to trans the file\n");
  127. }
  128. else{
  129. perror("error! wrong choice!\n");
  130. exit();
  131. }
  132.  
  133. }
  134. else if (recvbuf.command == SHUTDOWN){
  135. printf("Now the server is shutdown!\n");
  136. break;
  137. }
  138. }
  139.  
  140. /*----------------------close ----------*/
  141. close(socketfd);
  142. return ;
  143. }

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. 19、网络编程 (Socket套接字编程)

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

  2. linux网络环境下socket套接字编程(UDP文件传输)

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

  3. socket 套接字编程

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

  4. day31 socket套接字编程

    为什么要有套接字编程? 在上节课的学习中,我们学习了OSI七层协议,但是如果每次进行编程时我们都需要一层一层的将各种协议使用在我们的程序中,这样编写程序实在是太麻烦了,所以为了让程序的编写更加的简单, ...

  5. socket套接字编程 HTTP协议

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

  6. 19 网络编程--Socket 套接字方法

    1.Socket(也称套接字)介绍 socket这个东东干的事情,就是帮你把tcp/ip协议层的各种数据封装啦.数据发送.接收等通过代码已经给你封装好了 ,你只需要调用几行代码,就可以给别的机器发消息 ...

  7. 网络编程--Socket(套接字)

    网络编程 网络编程的目的就是指直接或间接地通过网络协议与其他计算机进行通讯.网络编程中 有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后 如何可靠高效的进行数据传输.在 ...

  8. Linux之socket套接字编程20160704

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

  9. Linux网络编程——原始套接字编程

    原始套接字编程和之前的 UDP 编程差不多,无非就是创建一个套接字后,通过这个套接字接收数据或者发送数据.区别在于,原始套接字可以自行组装数据包(伪装本地 IP,本地 MAC),可以接收本机网卡上所有 ...

随机推荐

  1. DOM遍历方法

    为标题行添加样式 $(document).ready(function(){ $('th').parent().addClass('table-heading'); $('tr:not([th]):o ...

  2. python 安装模块步骤

    1.下载 pyocr-0.4.1.tar.gz   tar.gz文件  解压  放到 c:/python27 文件夹下面 C:\Python27\pyocr-0.4.1  直接 cmd 命令 进入   ...

  3. Using Spring Boot without the parent POM

    Using Spring Boot without the parent POM: 问题 spring boot项目一般情况下的parent如下: <parent> <groupId ...

  4. (转)js activexobject调用客户机exe文件

    原文地址:http://blog.csdn.net/jiafugui/article/details/5364210 function Run(strPath) { try { var objShel ...

  5. javascript new

    1. 仅function可以使用new 2. function使用new时,会拷贝function中this的内容给新对象,并将function的prototype指向新对象(如果该function没 ...

  6. 每周一荐:学习ACE一定要看的书

    作 者:david++发布时间:2012/06/08 09:02文章地址:http://game-lab.org/?p=320 近两个月都在学习ACE,一个超级强大,也超级复杂的网络框架库.对ACE的 ...

  7. spring随手笔记3:销毁方法

    1. public class HelloWorld { private String msg; public void setMsg(String msg) { this.msg = msg; } ...

  8. 第十二章 非对称加密算法-RSA

    注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第8章“高等加密算法--非对称加密算法” 12.1.RSA(最经典的非对称加密算法) 特点: 使用一套密钥即可完成加解密(与D ...

  9. 源代码tfs to git

    TFSàgit可以保留完整历史记录,方法: https://github.com/git-tfs/git-tfs 系统变量的path里加上: ;C:\Program Files (x86)\Git\b ...

  10. Android Studio插件推荐(PreIOC,GsonFormat)

    好的插件能加快项目的开发速度,尤其是一些针对重复性的代码的插件,所以在这里向大家推荐2款不错的插件,如果以后发现新的好的插件,还会继续推荐,同时欢迎大家推荐 GsonFormat GsonFormat ...