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; int main(){ /*-------------create UDP socket------------*/
if((socketfd = socket(AF_INET,SOCK_DGRAM,)) == -){
perror("socket error\n");
exit();
} /*-----------------IO-----------------------*/
printf("Please input the ip:\n");
scanf("%s",ip);
printf("Please input the port:\n");
scanf("%d",&port); /*-----------------bind----------------------*/
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
if (bind(socketfd,(struct sockaddr *)&server,sizeof(server)) == -){
perror("bind error\n");
exit();
} /*---------------------调试信息------------
addrlen = sizeof(client);
recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
num = strlen(recvbuf.buf);
recvbuf.buf[num] = '\0';
printf("command %d\n",recvbuf.command );
printf("len %d\n",recvbuf.len );
printf("no %d\n", recvbuf.no);
printf("buf %s\n",recvbuf.buf ); */ addrlen = sizeof(client);
while(){
bzero(&recvbuf,sizeof(recvbuf));
num =recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&client,&addrlen);
choice = recvbuf.command;
if(choice == DOWNLOAD){
char buf[];
int fd;
fd = open((recvbuf.buf),O_RDONLY);
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;
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));
}
}
}
else if(choice == UPLOAD){
printf("The client want to upload the file: %s\n",recvbuf.buf);
printf("Please choice start or no? \n");
printf("5 :start, 4: no\n");
scanf("%d",&sentbuf.command);
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
if(sentbuf.command ==START){
int no =;
int fd =open(recvbuf.buf,O_CREAT | O_TRUNC |O_WRONLY,);
if(fd < ){
perror("create file error\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);
printf("kkk%s\n",recvbuf.buf );
bzero(&recvbuf,sizeof(recvbuf));
}
else{
perror("The file no is not same.Some message is missed! error occured! \n");
break;
}
}
if(recvbuf.command == END){
close(fd);
printf("transmission is successful!\n");
break;
}
}
}
else if(sentbuf.command == NO){
printf("Not to trans the file\n");
}
else{
perror("error! wrong choice!\n");
exit();
} }
else if (recvbuf.command == SHUTDOWN){
printf("Now the server is shutdown!\n");
break;
}
} /*----------------------close ----------*/
close(socketfd);
return ;
}
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传输)的更多相关文章
- 19、网络编程 (Socket套接字编程)
网络模型 *A:网络模型 TCP/IP协议中的四层分别是应用层.传输层.网络层和链路层,每层分别负责不同的通信功能,接下来针对这四层进行详细地讲解. 链路层:链路层是用于定义物理传输通道,通常是对某些 ...
- linux网络环境下socket套接字编程(UDP文件传输)
今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中, ...
- socket 套接字编程
今日内容 socket 套接字编程 简易服务端与客户端代码实现 通信循环 黏包现象(TCP协议) 报头制作.struct 模块.封装形式 内容详细 一.socket 套接字编程 实现一款能够进行数据交 ...
- day31 socket套接字编程
为什么要有套接字编程? 在上节课的学习中,我们学习了OSI七层协议,但是如果每次进行编程时我们都需要一层一层的将各种协议使用在我们的程序中,这样编写程序实在是太麻烦了,所以为了让程序的编写更加的简单, ...
- socket套接字编程 HTTP协议
socket套接字编程 套接字介绍 1. 套接字 : 实现网络编程进行数据传输的一种技术手段 2. Python实现套接字编程:import socket 3. 套接字分类 >流式套接 ...
- 19 网络编程--Socket 套接字方法
1.Socket(也称套接字)介绍 socket这个东东干的事情,就是帮你把tcp/ip协议层的各种数据封装啦.数据发送.接收等通过代码已经给你封装好了 ,你只需要调用几行代码,就可以给别的机器发消息 ...
- 网络编程--Socket(套接字)
网络编程 网络编程的目的就是指直接或间接地通过网络协议与其他计算机进行通讯.网络编程中 有两个主要的问题,一个是如何准确的定位网络上一台或多台主机,另一个就是找到主机后 如何可靠高效的进行数据传输.在 ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
- Linux网络编程——原始套接字编程
原始套接字编程和之前的 UDP 编程差不多,无非就是创建一个套接字后,通过这个套接字接收数据或者发送数据.区别在于,原始套接字可以自行组装数据包(伪装本地 IP,本地 MAC),可以接收本机网卡上所有 ...
随机推荐
- C#虚方法
若一个实例方法声明前带有virtual关键字,那么这个方法就是虚方法.虚方法与非虚方法的最大不同是,虚方法的实现可以由派生类所取代,这种取代是通过方法的重写实现的(以后再讲)虚方法的特点:虚方法前不允 ...
- Linux 指令
cat cdchmod chowncp cut 名称:cat使用权限:所有使用者使用方式:cat [-AbeEnstTuv] [--help] [--version] fileName说明:把档案串连 ...
- 一个php soap的错误记录
今天使用php soap实现两个系统之间的互通 在写好php的soapserver后,client端调用一直报 looks like we got no XML document,尝试好久后无法解决 ...
- HTML 认识
1.1认识什么是纯文本文件 Window 自带的一个软件,叫做记事本,记事本保存的格式就是TXT,就是英文text的缩写,术语上称呼为"纯文本文件". 注意: TXT文件,只 ...
- jquery iframe自适应高度[转]
经典代码 iFrame 自适应高度,在IE6/IE7/IE8/Firefox/Opera/Chrome/Safari通过测试. 很古老的方法: <iframe src="../In ...
- linux性能测试命令-----top
Top命令显示了实际CPU使用情况,默认情况下,它显示了服务器上占用CPU的任务信息,并且每5秒钟刷新一次.它会显示CPU使用量.内存使用量.交换内存.缓存大小.缓冲区大小.流程PID.用户.命令等. ...
- css实现自适应宽度布局
1.实现左侧宽度固定,右侧全屏自适应. body{margin:0;padding:0} .wrap{ width:100%; float:left} .content{ height:300px;b ...
- HTML 文本格式化<b><big><em><i><small><strong><sub><sup><ins><del>
<b> 标签-粗体 定义和用法: <b>标签规定粗体文本. 提示和注释 注释:根据 HTML5 规范,在没有其他合适标签更合适时,才应该把 <b> 标签作为最后的选 ...
- css —— 图片环绕+首行缩进
1.利用css实现图片环绕文字的效果: 只需要给img标签设置float:left/right即可: 2.实现上段文字首行缩进两个字的效果: 使用 text-indent: 2em;即可(em为相对单 ...
- EXE捆绑器
释放的方法真没想到 太神奇了 // 文件捆绑器Dlg.cpp : 实现文件 // #include "stdafx.h" #include "文件捆绑器.h" ...