linux网络环境下socket套接字编程(UDP文件传输)
今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作!
在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中,如果我们使用TCP传输,会造成传输速度较慢的情况,所以我们在进行文件传输的过程中,最好要使用UDP传输。
在其中,我们需要写两个程序,一个客户端,一个服务端,在一个终端中,先运行服务端,在运行客户端,在服务端和客户端都输入IP地址和端口号,注意服务端和客户端的端口号要相同,然后选择功能,在linux网络编程中,使用UDP进行传输属于比较简单的操作,所以直接上代码吧,详细的讲解我将会在之后上传!
client(客户端):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h" /*--------------------------*/
int socketfd ;
int addrlen;
struct sockaddr_in server;
struct protocol sentbuf;
struct protocol recvbuf;
int num;
char ip[];
int port;
int choice ;
char filename[]; /*--------------------------*/
void ShowMenu();
void DownLoad();
void UpLoad();
void ShutDown(); int main(){
/*---------------------------*/
socketfd = socket(AF_INET , SOCK_DGRAM , );
if(socketfd == -){
perror("socket failed!\n");
exit();
}
/*---------------------------*/
printf("please input ip of server:\n");
scanf("%s",ip);
printf("please input port of server:\n");
scanf("%d",&port);
/*---------------------------*/
bzero(&server , sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
addrlen = sizeof(server);
/*---------------------------*/
while(){
ShowMenu();
scanf("%d",&choice);
if(choice == DOWNLOAD){
printf("client download!\n");
DownLoad();
}
else if(choice == UPLOAD){
UpLoad();
}
else if(choice == SHUTDOWN){
printf("client shutdown!\n");
ShutDown();
break;
}
else{
printf("please input the right choice!\n");
}
}
close(socketfd);
return ;
} void ShowMenu(){
printf("please make a choice:\n");
printf("0:shutdown!\n");
printf("1:download!\n");
printf("2:upload!\n");
} void DownLoad(){
bzero(&recvbuf , sizeof(recvbuf));
bzero(&sentbuf , sizeof(sentbuf));
bzero(filename , sizeof(filename));
printf("please input the filename:\n");
scanf("%s",sentbuf.buf);
sentbuf.command = DOWNLOAD;
sendto(socketfd , &sentbuf , sizeof(sentbuf), , (struct sockaddr*)&server , sizeof(server));
bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&server , &addrlen);
printf("recvbuf:%d\n",recvbuf.command);
if(recvbuf.command == YES){
printf("YES!\n");
int choice_1;
printf("if you input a 5 , the file transmission start!\n");
scanf("%d",&choice_1);
if(choice_1 == START){
sentbuf.command = START;
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
int no = ;
int fd = open(filename , O_CREAT | O_TRUNC | O_WRONLY , );
if(fd < ){
perror("creat file is failed!\n");
exit();
}
bzero(&recvbuf , sizeof(recvbuf));
while( ( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&server , &addrlen)) > ){
if( recvbuf.command == CONTENT ){
if(no == recvbuf.no){
write(fd , recvbuf.buf , recvbuf.len);
bzero(&recvbuf , sizeof(recvbuf));
}
else{
perror("The file no is not same, Some massage is missed!error occured!\n");
break;
}
}
if( recvbuf.command == END){
close(fd);
printf("transmission is successful!\n");
break;
}
}
}
}
else if(recvbuf.command == NO){
perror("No such file on server!\n");
}
else{
perror("recvbuf.command error!\n");
exit();
}
} void ShutDown(){
sentbuf.command = SHUTDOWN;
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
printf("client is end!\n");
} void UpLoad(){
bzero(&recvbuf , sizeof(recvbuf));
bzero(&sentbuf , sizeof(sentbuf));
bzero(filename , sizeof(filename));
printf("please input you want to upload filename:\n");
scanf("%s",sentbuf.buf);
sentbuf.command = UPLOAD;
sendto(socketfd , &sentbuf , sizeof(sentbuf), , (struct sockaddr*)&server , sizeof(server));
bcopy(sentbuf.buf , filename , sizeof(sentbuf.buf));
int fd ;
fd = open(filename , O_RDONLY);
if(fd < ){
perror("The file is not exist!\n");
exit();
}
recvfrom(socketfd , &recvbuf , sizeof(recvbuf), , (struct sockaddr*)&server , &addrlen);
if( recvbuf.command == START ){
int no = ;
while( ( num = read(fd , sentbuf.buf , INFOLEN)) > ){
sentbuf.no = no ;
sentbuf.command = CONTENT;
sentbuf.len = strlen(sentbuf.buf);
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server,sizeof(server));
no++;
bzero(&sentbuf , sizeof(sentbuf));
}
bzero(&sentbuf , sizeof(sentbuf));
sentbuf.command = END;
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&server , sizeof(server));
}
else if(recvbuf.command == NO){
printf("not transmission!\n");
}
else{
perror("error! wrong choice!\n");
}
}
server(服务端):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h" int socketfd;
int addrlen;
struct sockaddr_in server;
struct sockaddr_in client;
struct protocol sentbuf;
struct protocol recvbuf;
int num;
char ip[];
int port;
int choice;
char filename[]; void DownLoad();
void ShutDown();
void UpLoad(); int main(){
socketfd = socket( AF_INET , SOCK_DGRAM , );
if( socketfd == -){
perror("socket failed!\n");
exit();
} printf("please input client ip:\n");
scanf("%s" , ip);
printf("please input client port:\n");
scanf("%d",&port); bzero(&server , sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip); int bin = bind(socketfd , (struct sockaddr*)&server , sizeof(server));
if(bin == -){
perror("bind failed!\n");
exit();
} addrlen = sizeof(client);
//char filename[100]; while(){
bzero(&recvbuf , sizeof(recvbuf));
num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen);
choice = recvbuf.command;
bcopy(recvbuf.buf ,filename, sizeof(recvbuf.buf));
printf("%s\n",filename);
if(choice == DOWNLOAD){
printf("kkkkkkkk\n");
DownLoad();
}
else if(choice == SHUTDOWN){
printf("server will shutdown!\n");
ShutDown();
break;
}
else if(choice == UPLOAD){
UpLoad();
}
}
return ;
} void DownLoad(){
char buf[];
int fd ;
printf("11111\n");
printf("%s\n",filename);
fd = open((filename) , O_RDONLY);
printf("fd:%d\n",fd);
bzero(&sentbuf , sizeof(sentbuf));
if(fd < ){
sentbuf.command = NO;
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
printf("no such file!\n");
exit();
}
else{
sentbuf.command = YES;
printf("YES!\n");
sendto(socketfd , &sentbuf, sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen);
if(recvbuf.command == START){
int no = ;
while((num = read(fd , sentbuf.buf , INFOLEN)) > ){
sentbuf.no = no ;
sentbuf.command = CONTENT;
sentbuf.len = strlen(sentbuf.buf);
sendto(socketfd , &sentbuf , sizeof(sentbuf),,(struct sockaddr*)&client , sizeof(client));
no++;
bzero(&sentbuf , sizeof(sentbuf));
}
bzero(&sentbuf , sizeof(sentbuf));
sentbuf.command = END;
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
}
}
} void ShutDown(){
printf("Now server is shutdown!\n");
} void UpLoad(){
//bzero(&recvbuf , sizeof(recvbuf));
bzero(&sentbuf , sizeof(sentbuf));
bzero(&filename , sizeof(filename));
//recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , 0 , (struct sockaddr*)&client , &addrlen);
printf("4:NO 5:START\n");
scanf("%d" , &sentbuf.command);
sendto(socketfd , &sentbuf , sizeof(sentbuf) , , (struct sockaddr*)&client , sizeof(client));
if( sentbuf.command == START){
int no = ;
printf("filename:%s\n",recvbuf.buf);
int fd = open(recvbuf.buf , O_CREAT | O_TRUNC | O_WRONLY , );
if(fd < ){
perror("create file failed!\n");
exit();
}
bzero(&recvbuf , sizeof(recvbuf) );
while(( num = recvfrom(socketfd , &recvbuf , sizeof(recvbuf) , , (struct sockaddr*)&client , &addrlen)) > ){
if( recvbuf.command == CONTENT ){
if( no == recvbuf.no ){
write(fd , recvbuf.buf , recvbuf.len);
bzero(&recvbuf , sizeof(recvbuf));
}
else{
perror("The file no is not same . Some massage is missed!\n");
break;
}
}
if( recvbuf.command == END ){
close(fd);
printf("transmission is successful!\n");
break;
}
}
}
else if( sentbuf.command == NO ){
printf("The file can't transmission!\n");
}
else{
perror("please input right choice!\n");
exit();
}
}
makefile:
main:udpserver.o udpclient.o
gcc -o udpserver udpserver.o
gcc -o udpclient udpclient.o
udpserver.o:udpserver.c
gcc -c udpserver.c
udpclient.o:udpclient.c
gcc -c udpclient.c
linux网络环境下socket套接字编程(UDP文件传输)的更多相关文章
- linux网络编程-(socket套接字编程UDP传输)
今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中, ...
- [网络编程之Socket套接字介绍,套接字工作流程,基于TCP协议的套接字程序]
[网络编程之Socket套接字介绍,套接字工作流程,基于TCP协议的套接字程序] 为何学习socket套接字一定要先学习互联网协议: 1.首先:要想开发一款自己的C/S架构软件,就必须掌握socket ...
- socket套接字编程 HTTP协议
socket套接字编程 套接字介绍 1. 套接字 : 实现网络编程进行数据传输的一种技术手段 2. Python实现套接字编程:import socket 3. 套接字分类 >流式套接 ...
- socket 套接字编程
今日内容 socket 套接字编程 简易服务端与客户端代码实现 通信循环 黏包现象(TCP协议) 报头制作.struct 模块.封装形式 内容详细 一.socket 套接字编程 实现一款能够进行数据交 ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
- 网络编程之socket套接字
目录 socket套接字简介 socket模块 通信循环 代码优化 连接循环 半连接池 黏包问题 解决黏包问题 黏包问题特殊情况(文件过大) socket套接字简介 由于操作OSI七层是所有C/S架构 ...
- java 25 - 3 网络编程之 Socket套接字
Socket Socket套接字: 网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字. Socket原理机制: 通信的两端都有Socket. 网络通信其实就是Socket ...
- 19、网络编程 (Socket套接字编程)
网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用层.传输层.网络层和链路层,每层分别负责不同的通信功能,接下来针对这四层进行详细地讲解. 链路层:链路层是用于定义物理传输通道,通常是对某些 ...
- 31_网络编程(Socket套接字编程)_讲义
今日内容介绍 1.网络三要素及传输协议 2.实现UDP协议的发送端和接收端 3.实现TCP协议的客户端和服务器 4.TCP上传文件案例 01网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用 ...
随机推荐
- ubuntu上mysql服务器安装后只能本地连接不能远程连接的问题
安装好mysql后,想使用另一个电脑进行远程登录,在登录时 提示拒绝连接 百度后,发现需要两个步骤解决该问题 /etc/mysql/my.cnf 里修改bind_address = 0.0.0.0 ...
- 课时8—弹窗modal
首先弹窗的实现效果如下: 主要实现的代码如下: CSS: .header,.footer,.wrap-page{ position:absolute; left:; right:; backgroun ...
- ArcGIS学习推荐
1. Arcgis Desktop 10帮助库 ArcGIS 系统的帮助库.该帮助库已经过编译,可为 ArcGIS 各方面的应用提供综合文档.建立该库的目的是满足以下各类主要用户的需求: GIS 专 ...
- 常用git命令及问题解决方法
使用git不久,在这里记录使用git的命令. 1.将本地项目上传git git端 1.[start a project]新建一个项目 example 客户端 1.git init 初始化本地git仓库 ...
- zoj 1203 Swordfish prim算法
#include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...
- Java 编程入门(词汇表)
抽象类(abstract class):抽象类不能创建对象,主要用来创建子类.Java中的抽象类使用 abstract 修饰符定义. 抽象数据类型(abstract data type ADT):抽象 ...
- PHP二次开发discuz3.2最新体验
康盛官方于6月4号发布了discuz3.2的正式版,因为这两天一直忙于一个项目,一直没来的及体验,现在抽时间总算是装上了,也体验一把. 根据官方说明:Discuz! X3.2 在继承和完善 Discu ...
- 使用ASP.NET 上传文件 三种类型判断方法(后缀,MIME,数据流)
#region 一. 安全性比较低,把文本文件1.txt改成1.jpg照样可以上传,但其实现方法容易理解,实现也简单,所以网上很多还是采取这种方法. Boolean fileOk = false; s ...
- C++编程优化心得(持续更新)
1. 对齐原则.比如64位总线,每次寻址读取8B.编程时注意变量地址,尽量消耗总线最少的寻址次数.堆内存申请时,系统严格按照对齐原则分配,故而使用时候也尽量不要跨寻址边界. 2. 需要的时候,可为了效 ...
- Appium移动自动化测试(一)--安装Appium
Appium 自动化测试是很早之前就想学习和研究的技术了,可是一直抽不出一块完整的时间来做这件事儿.现在终于有了. 反观各种互联网的招聘移动测试成了主流,如果再不去学习移动自动化测试技术将会被淘汰. ...