//网络编程发送端--大文件传输(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. java.lang.RuntimeException: Fail to connect to camera service问题

    做音视频录制功能的真机调试的时候出现这个问题 错误意思为无法连接到相机服务 可能由两种情况导致 1.配置清单文件没有设置相应的权限 <uses-permission android:name=& ...

  2. 解决虚拟机linux端mysql数据库无法远程访问

    解决虚拟机linux端mysql数据库无法远程访问 1. 在控制台执行 mysql -u root -p mysql,CentOS系统提示输入数据库root用户的密码,输入完成后即进入mysql控制台 ...

  3. json数组和List转换

    package hb; import java.util.Date; public class Person { String id; int age; String name; Date birth ...

  4. svn错误

    在myEclipse客户端第一次连到SVN时,如:svn://192.168.20.242/MyProject1,然后要求输入用户名和密码.如果用户名和密码输入出错了,强行确定后.问题来了!会出现,以 ...

  5. Effective Java 74 Implement Serializable judiciously

    Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...

  6. Web Farm 和Web Garden

    这两个都是提高网站性能的服务器端技术 1.Web Farm:如果应用程序被多个服务器托管,这种情况就可以称作Web Farm. 2.Web Garden: 指的是一个应用程序可以分成多个进程(w3wp ...

  7. device eth0 does not seem to be present, delaying initialization

    在搭建LVS+Keepalived高可用负载均衡环境的过程中,使用VirtualBox复制了两个Centos的环境,并且选中了“重新初始化网卡”的选项,但是在启动这两个复制的Centos环境的时候,发 ...

  8. Nginx安装学习使用详细记录

    选择Nginx的优点:Nginx 可以在大多数 Unix like OS 上编译运行,并有 Windows 移植版. Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建 ...

  9. hibernate一对多映射实现

    Junit4方法详解 setUpBeforeClass()类初始化前调用 tearDownAfterClass()类初始化后调用 setUp()在测试方法前调用 tearDown()在测试方法后调用 ...

  10. 烂泥:NFS存储与VSphere配合使用

    本文首发于烂泥行天下. 公司服务器的虚拟化使用的是VM ESXi 5.0,为了更有效的利用服务器的硬盘空间.就把所有的镜像文件存放到另外一台linux服务器上,这样在使用vsphere安装虚拟机时可以 ...