Linux Linux程序练习十一(网络编程大文件发送UDP版)
//网络编程发送端--大文件传输(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版)的更多相关文章
- Linux Linux程序练习十(网络编程大文件发送)
//网络编程客户端--大文件传输 #include <stdio.h> #include <stdlib.h> #include <string.h> #inclu ...
- linux下C语言socket网络编程简例
原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...
- Linux命令行与shell脚本编程大全.第3版(文字版) 超清文字-非扫描版 [免积分、免登录]
此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Linux命令行与shell脚本编程大全.第3版 (大小:约22M)
- 《Linux命令、编辑器与shell编程》第三版 学习笔记---002
<Linux命令.编辑器与shell编程>第三版 学习笔记---001 Linux命令.编辑器与shell编程 Shell准备 1.识别Shell类型 echo $0 echo $BAS ...
- 《Linux命令行与shell脚本编程大全 第3版》创建实用的脚本---11
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》高级Shell脚本编程---47
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---57
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
随机推荐
- 个人开源作品,即时通讯App支持文本、语音、图片聊天
开源一个即时通讯类App,支持纯文本.语音.地理位置.图片聊天,同时还加入了好友圈功能,支持分享动态和发送图片,支持搜索附近的人,使用的百度地图定位功能:由Bmob后端云提供服务器支持,欢迎喜欢的伙伴 ...
- iOS--九宫格奥秘(UIView)(arc4random)
下面程序主要实现,九宫格的模型,每个小UIView的颜色随机产生的,用到了 arc4random函数: ; ; ; ; ; i<; i++) { ; j<; j++) { UIView * ...
- Java眼中的XML--文件读取--1 应用DOM方式解析XML
初次邂逅XML: 需要解析的XML文件: 这里有两个book子节点. 1.如何进行XML文件解析前的准备工作,另外解析先获取book节点. 这个我后来看懂了: 这个Node的ELEMENT_NODE= ...
- Android开发艺术探索学习笔记(三)
第三章 View的事件体系 3.1 View基础知识 3.1.1 什么是view View 是Android中所有控件的基类,是一种界面层的控件的一种抽象,它代表了一个控件. 3.1.2 View的 ...
- !!!jQuery中事件绑定 推荐使用.delegate()或者live()
jQuery中的.bind()..live()和.delegate()之间区别分析 参考:http://www.jb51.net/article/27309.htm DOM树 首先,可视化一个HM ...
- JavaScript Patterns 4.3 Returning Functions
Use closure to store some private data, which is accessible by the returned function but not to the ...
- NHibernate使用ICriteria分页并返回数据库记录总条数
最近在使用NHibernate,发现进行分页查询无法得到数据库记录的总条数,在网上找了很久没找到具体的实现方法,找到的资料都说得不是很清楚,研究了很久终于写出了这样一个方法. NHibernate下分 ...
- oracle数据库ORA-01654 错误的解决方法
引言: 数据库突然报: ORA-01654: unable to extend index BO.INDEX_indexname by 311072 in tablespace 错误,上网查原因,发现 ...
- Json常用的转换
简单记录一下jquery里面的JSON.parse()和JSON.stringify()函数,和js中的eval()函数的用法 1,JSON.parse 函数(常用) 作用:将 JavaScript ...
- Linux 安装 redis
环境:centos7 参考:http://blog.csdn.net/lk10207160511/article/details/50364088 步骤如下: 安装redis: 打开终端 输入 s ...