Linux Linux程序练习十(网络编程大文件发送)
//网络编程客户端--大文件传输
#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 three param !\n");
return -;
}
int port=atoi(args[]);
//create socket
int st=socket(AF_INET,SOCK_STREAM,);
if(st==-)
{
printf("create socket failed ! error message :%s\n",strerror(errno));
return -;
}
//defien IP address
struct sockaddr_in addr;
//init addr
memset(&addr,,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_port=htons(port);
addr.sin_addr.s_addr=inet_addr(args[]);
if(connect(st,(struct sockaddr *)&addr,sizeof(addr))==-)
{
printf("connect failed ! error message :%s\n",strerror(errno));
goto END;
}
//send file //define file stream
FILE * pfr=NULL;
//open the file in read mode
pfr=fopen(args[],"r");
if(pfr==NULL)
{
printf("open the file failed !error message :%s\n",strerror(errno));
goto END;
}
char buf[]={};
size_t num=;
while((num=fread(buf,sizeof(char),sizeof(buf),pfr))>)
{
//send part of the file
if(send(st,buf,sizeof(char)*num,)==-)
{
printf("send failed !error message :%s\n",strerror(errno));
break;
}
memset(buf,,sizeof(buf));
}
fclose(pfr);
END:close(st);
return ;
}
//网络编程服务端--发送大文件
#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_STREAM, );
if (st == -)
{
printf("create socket failed ! error message :%s\n", strerror(errno));
return -;
}
//defien IP address
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);
//bind port
if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -)
{
printf("bind failed ! error message :%s\n", strerror(errno));
goto END;
}
//listen
if (listen(st, ) == -)
{
printf("listen failed ! error message :%s\n", strerror(errno));
goto END;
}
//只接收一个用户连接
int clientst = ;
struct sockaddr_in client_addr;
memset(&client_addr, , sizeof(client_addr));
size_t client_addrlen = sizeof(client_addr);
clientst = accept(st, (struct sockaddr *) &client_addr, &client_addrlen);
if (clientst == -)
{
printf("accept failed ! error message :%s\n", strerror(errno));
goto END;
}
//recv message
char buf[] = { };
int mflag = ;
//open the file stream
FILE * pfa = NULL;
pfa = fopen("/home/test/2/1.dat", "a");
if (pfa == NULL)
{
printf("open the file failed ! error message :%s\n", strerror(errno));
goto END;
}
while ()
{
mflag = recv(clientst, buf, sizeof(buf), );
if (mflag == )
{
printf("client is closed!\n");
break;
} else if (mflag == -)
{
printf("recv message is failed ! error message :%s\n",
strerror(errno));
break;
}
//将文件写到当前程序目录下
fwrite(buf, sizeof(char), mflag, pfa);
memset(buf, , sizeof(buf));
}
fclose(pfa);
END: close(st);
return ;
}
.SUFFIXES:.c .o
CC=gcc
SRCS1=mclient.c
SRCS2=mserver.c
OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)
EXEC1=mcl
EXEC2=mser 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)
Linux Linux程序练习十(网络编程大文件发送)的更多相关文章
- Linux Linux程序练习十一(网络编程大文件发送UDP版)
//网络编程发送端--大文件传输(UDP) #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
- linux下C语言socket网络编程简例
原创文章,转载请注明转载字样和出处,谢谢! 这里给出在linux下的简单socket网络编程的实例,使用tcp协议进行通信,服务端进行监听,在收到client的连接后,发送数据给client:clie ...
- Linux C 程序 GTK+图形界面编程(22)
GTK+图形界面编程 Linux大多是在字符界面,但也可以开发图形界面 目前已经存在多种Linux下开发图形界面的程序开发包:最常用的是Qt和GTK+ Qt是一个跨平台的图形界面开发库,不仅仅支持Li ...
- Linux IO多路复用之epoll网络编程及源码(转)
原文: 前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网 ...
- Linux IO多路复用之epoll网络编程(含源码)
前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网络读取一 ...
- Linux IO多路复用之epoll网络编程
前言 本章节是用基本的Linux基本函数加上epoll调用编写一个完整的服务器和客户端例子,可在Linux上运行,客户端和服务端的功能如下: 客户端从标准输入读入一行,发送到服务端 服务端从网络读取一 ...
- 《Linux命令行与shell脚本编程大全》 第一、二章 学习笔记
第一章:初识Linux shell Linux内核负责以下4个主要功能: 1.系统内存管理 2.软件程序管理 3.硬件设备管理 4.文件系统管理 1.系统内存管理 内核不仅管理服务器上的可用物理内存, ...
- 大数据之路week04--day03(网络编程)
哎,怎么感觉自己变得懒了起来,更新博客的频率变得慢了起来,可能是因为最近得知识开始变得杂变得难了起来,之前在上课的时候,也没有好好听这一方面的知识,所以,现在可以说是在学的新的知识,要先去把新的知识思 ...
- 浅谈TCP/IP网络编程中socket的行为
我认为,想要熟练掌握Linux下的TCP/IP网络编程,至少有三个层面的知识需要熟悉: 1. TCP/IP协议(如连接的建立和终止.重传和确认.滑动窗口和拥塞控制等等) 2. Socket I/O系统 ...
随机推荐
- IOS中程序如何进行推送消息(本地推送,远程推送)2(上)
未看过本地推送的,可以提前看一下本地推送. http://www.cnblogs.com/wolfhous/p/5135711.html =============================== ...
- 自定义Dialog以及Dialog返回值到Activity
步骤: 1.定义自定义的Dialog的布局文件 2.写一个类MyDialog继承Dialog 3.Dialog 返回值到Activity的方法是定义一个接口,接口中定义返回值到Activity的方法, ...
- Android UI之下拉刷新上拉刷新实现
在实际开发中我们经常要用到上拉刷新和下拉刷新,因此今天我写了一个上拉和下拉刷新的demo,有一个自定义的下拉刷新控件 只需要在布局文件中直接引用就可以使用,非常方便,非常使用,以下是源代码: 自定义的 ...
- eclipse 中手动安装 subversive SVN
为什么我选择手动安装呢?因为通过 eclipse market 下载实在太慢了. 1.下载离线安装包 http://www.eclipse.org/subversive/latest-releas ...
- 深入剖析jsonp跨域原理
在项目中遇到一个jsonp跨域的问题,于是仔细的研究了一番jsonp跨域的原理.搞明白了一些以前不是很懂的地方,比如: 1)jsonp跨域只能是get请求,而不能是post请求: 2)jsonp跨域的 ...
- Web Application Project is configured to use IIS. Unable to access the IIS metabase.(配置为使用IIS Web应用程序xxxx项目。无法访问IIS元数据库。)
这几天重装系统,装了win10,居然用vs2013打开项目出现下面这个提示错误,搞了很久才知道原因: Even though I am an administrator on the machine, ...
- centos7 拨号之后添加路由
问题:拨号主机再自动拨号(/sbin/ifdown ppp0;/sbin/ifup ppp0)之后,无法上网(没有添加路由) 思路:在拨号程序中添加路由代码 vim /sbin/ifup { slee ...
- tmpFile.renameTo(classFile) failed 错误
突然的出现了这个tmpFile.renameTo(classFile) failed 错误. 也许是我删掉了tomcat里面的webapps 中的项目,而通过debug部署,而出现了这个问题. 一开始 ...
- Ubuntu16.04安装ROS-kinetic
参考链接:http://www.voidcn.com/blog/wishchin/article/p-5972036.html 第一步: 软件源配置1. 增加下载源(增加ubuntu版的ros数据仓库 ...
- Windows Live Writer离线编写博客
WLW最新版本为2012,官网下载 Windows Live Writer配置步骤 使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 L ...