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版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
随机推荐
- Android 购物车功能的实现
首先,众所周知,ListView是Android最常用的控件,可以说是最简单的控件,也可以说是最复杂的控件. 作为一个Android初级开发者,可能会简单的ListView展示图文信息. 作为一个有一 ...
- 内存管理2(主讲MRR)
内存管理2 我们讨论过properties 后,所有的内存管理系统都是通过控制所有对象的生命周期来减少内存的占用.iOS和OS X应用程序完成这些是通过对象拥有者来实现的,它保证了只要对象使用就会存在 ...
- SQL JOIN
- 安卓第九天笔记-Activity
安卓第九天笔记-Activity 1.创建Activity 一个界面对应一个activity 创建一个Activity 1.写一个JAVA类,继承Activity publicclass CalcA ...
- mysql中FIND_IN_SET的使用方法
在mysql中,有时我们在做数据库查询时,需要得到某字段中包含某个值的记录,但是它也不是用like能解决的,使用like可能查到我们不想要的记录,它比like更精准,这时候mysql的FIND_IN_ ...
- 网站的SEO
提高网站SEO排名的策略除了要有高质量的内容,还有几种方案可以使用 1.关键词的设定 合适的关键词可以提升搜索引擎中的排名 ①最重要的是html中的title标签,这也是一个页面的最重要的概括,所以尽 ...
- Struts中的OGNL和EL表达式笔记
Struts中的OGNL和EL表达式笔记 OGNL(Object-Graph Navigation Language),可以方便的操作对象属性的表达式语言. 1.#符号的用途 一般有三种方式: 1.1 ...
- oracle序列
一.序列 序列是oracle用来生产一组等间隔的数值.序列是递增,而且连续的.oracle主键没有自增类型,所以一般使用序列产生的值作为某张表的主键,实现主键自增.序列的编号不是在插入记录的时候自动生 ...
- TFS 2015 Update 2功能探索
微软刚刚发布了TFS 2015 update 2的测试包,https://blogs.msdn.microsoft.com/bharry/2016/02/10/team-foundation-serv ...
- sass揭秘之变量(转载)
出处:http://www.w3cplus.com/preprocessor/sass-basic-variable.html 因为文章内含有很多sass代码,如需自己动手查看编译结果,推荐使用sas ...