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版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
随机推荐
- 关于Eclipse 和 IDEA 导入library库文件 的步骤
这里我们以PullToRefresh(上拉刷新下拉加载)组件的library为例 下载地址: https://github.com/chrisbanes/Android-PullToRefresh 现 ...
- C++map类型
map是键-值对的集合,可以理解为关联数组,可以使用键作为下标来获取一个值 本文地址:http://www.cnblogs.com/archimedes/p/cpp-map.html,转载请注明源地址 ...
- Android底部TabHost API
今天在项目中遇到了底部TabHost,顺便就写了一个底部TabHost的api继承即可使用非常简单,以下为源代码: 首先是自定义的TabHostActivity,如果要使用该TabHost继承该类即可 ...
- PMP 项目管理过程组与知识领域
- Jmeter之HTTP Request Defaults
一.HTTP Request Defaults的作用: 该组件可以为我们的http请求设置默认的值.假如,我们创建一个测试计划有很多个请求且都是发送到相同的server,这时我们只需添加一个Http ...
- Asp.net 页面访问模板页的属性
首先 页面需要添加下面一段代码 <%@ MasterType VirtualPath="~/User/User.Master" %> 添加的位置如图 这样就可以在这个页 ...
- javascript中五种常见的DOM方法
getElementById将返回一个与那个有着给定id属性值的元素节点对应的对象. <html xmlns="http://www.w3.org/1999/xhtml"&g ...
- Linux LDAP Server--->Clients配置
Linux Ldap Configuration LDAP Server Base Software & SysTem Info SysTem Info 系统版本:centos 6.4 LDA ...
- ElasticSearch Filter Aggregations
类似于sql语句中where子句的作用 { "query": { "match_all": {} }, "aggs": { "ag ...
- std::list
1遍历 std::list<TYPE*>::const_iterator iter_list; for (iter_list = my_list.begin(); iter_list != ...