//网络编程发送端--大文件传输(UDP)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h> #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> int main(int arg, char * args[])
{
if (arg < )
{
printf("please print two param ! \n");
return -;
}
int port = atoi(args[]);
int st = socket(AF_INET, SOCK_DGRAM, );
if (st == -)
{
printf("create socket failed ! error message :%s\n", strerror(errno));
return -;
}
struct sockaddr_in addr;
memset(&addr, , sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = inet_addr(args[]);
char buf[] = { };
int num = ;
//open the file stream
FILE *pfr = NULL;
int index=;
pfr = fopen("/home/test/2/1.dat", "r");
if (pfr == NULL)
{
printf("open the file failed ! error message :%s\n", strerror(errno));
goto END;
}
while ((num = fread(buf, sizeof(char), sizeof(buf), pfr)) > )
{
if (sendto(st, buf, sizeof(char)*num, , (struct sockaddr *) &addr,
sizeof(addr)) == -)
{
printf("sendto failed ! error message :%s\n", strerror(errno));
break;
}
printf("read %d num=%d\n",index++,num);
}
fclose(pfr);
END: close(st);
return ;
}
//网络编程接收端--大文件传输(UDP)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h> #include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h> int main(int arg, char *args[])
{
if (arg < )
{
printf("please print one param ! \n");
return -;
}
int port=atoi(args[]);
int st = socket(AF_INET, SOCK_DGRAM, );
if (st == -)
{
printf("create socket failed ! error message:%s \n",strerror(errno));
return -;
}
struct sockaddr_in addr;
memset(&addr,,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(st,(struct sockaddr *)&addr,sizeof(addr))==-)
{
printf("bind IP failed ! error message:%s \n",strerror(errno));
goto END;
}
struct sockaddr_in client_addr;
socklen_t client_addrlen=sizeof(client_addr);
char buf[]={};
int num=;
int index=;
//define the file stream
FILE * pfa=NULL;
//open the file in append mode
pfa=fopen("/home/test/3/1.dat","a");
if(pfa==NULL)
{
printf("open the file failed ! error message :%s\n",strerror(errno));
goto END;
}
while()
{
memset(&client_addr,,sizeof(client_addr));
num=recvfrom(st,buf,sizeof(buf),,(struct sockaddr *)&client_addr,&client_addrlen);
if(num==-)
{
printf("recvform failed ! error message :%s\n",strerror(errno));
break;
}
/*
就算发送端关闭,recvfrom函数也不会返回0,而是会继续阻塞进程
*/
/*
else if(num==0)
{
printf("the other side socket is closed !\n");
break;
}
*/
printf("recv %d num=%d\n",index++,num);
fwrite(buf,sizeof(char),num,pfa);
if(num<)
{
printf("recv last! \n");
break;
}
memset(buf,,sizeof(buf));
}
fclose(pfa);
END:close(st);
return ;
}
.SUFFIXES:.c .o
CC=gcc
SRCS1=udprecv.c
SRCS2=udpsend.c
OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)
EXEC1=mrecv
EXEC2=msend start:$(OBJS1) $(OBJS2)
$(CC) -o $(EXEC1) $(OBJS1)
$(CC) -o $(EXEC2) $(OBJS2)
@echo "-------ok-----------"
.c.o:
$(CC) -Wall -g -o $@ -c $<
clean:
rm -f $(OBJS1)
rm -f $(EXEC1)
rm -f $(OBJS2)
rm -f $(EXEC2)

小结:UDP传输协议确实会出现丢包的情况,远没有TCP/IP协议来的安全,根据上图可以看出。

Linux Linux程序练习十一(网络编程大文件发送UDP版)的更多相关文章

  1. Linux Linux程序练习十(网络编程大文件发送)

    //网络编程客户端--大文件传输 #include <stdio.h> #include <stdlib.h> #include <string.h> #inclu ...

  2. linux下C语言socket网络编程简例

    原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...

  3. Linux命令行与shell脚本编程大全.第3版(文字版) 超清文字-非扫描版 [免积分、免登录]

    此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Linux命令行与shell脚本编程大全.第3版 (大小:约22M)

  4. 《Linux命令、编辑器与shell编程》第三版 学习笔记---002

    <Linux命令.编辑器与shell编程>第三版 学习笔记---001 Linux命令.编辑器与shell编程 Shell准备 1.识别Shell类型 echo  $0 echo $BAS ...

  5. 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  6. 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  7. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  8. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---57

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  9. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

随机推荐

  1. IOS客户端Coding项目记录(四)

    1:打开Xcode,然后闪退,报加载某库出现异常 如/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolc ...

  2. 弃用的异步get和post方法之代理方法

    #import "ViewController.h" #import "Header.h" @interface ViewController () <N ...

  3. linux终端python自动提示

    linux终端python自动提示 很多时候,在linux下编写python时, 都懒得去vi一个新文件,直接就新开一个终端, 进入python命令行模式,然后就可以写一些测试代码. 不过最悲剧的就是 ...

  4. .NET下dropdownlist的基本操作

    //List列中索引的赋值 teacher.DataValueField = ds.Tables[0].Columns["pidcord"].ColumnName; //List列 ...

  5. Android开发者的Git&GitHub(二)

     将代码托管到GitHub上 点击右上角New repository按钮来创建一个版本库 命名后选择添加一个Android项目类型的.gitignore文件,并选择开源协议(例如:Apache v2 ...

  6. iOS开发之网络编程--中文转码

    前言: 在GET请求或者是POST请求,请求上传的参数如果含有中文,可能会导致请求失败. 所以要对存储了URL地址的NSString对象进行中文转码,然后将这个NSString对象传递给URL.   ...

  7. iOS之github第三方框架(持续更新)

    1.MBProgressHUD MBProgressHUD是一个开源项目,实现了很多种样式的提示框 使用上简单.方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到. 到Github ...

  8. KVO的使用

    KVO的使用 KVO是一种设计模式,名为观察者. addObserver:forKeyPath:options:context: 通知其他对象的方法,这个方法在NSObject中就已经申明了,也就是说 ...

  9. JSON取值前判断

    public static void main(String[] args)throws Exception{ String jsonStr1="{\"access_token\& ...

  10. 读书笔记——网络编程与开发技术(3)基于TCP/IP协议的网络编程相关知识

    TCP/IP协议:数据链路层,网络层,传输层,应用层. IP地址分为5类:A类.B类.C类.D类.E类. (A类.B类.C类是基本类,D类多用于多播传送,E类为保留类.) "*"表 ...