Linux下UDP通讯开发
服务器部分
#include"protocol.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"netinet/in.h"
#include"fcntl.h"
int main()
{
int sockfd;
int writefd,readfd;
struct sockaddr_in server,client;
char recvbuf[1000];
char readbuf[1000];
char sendbuf[1000];
char writebuf[1000];
struct protocol *pro,*repro;
int checkcode;
int run =1;
socklen_t len;
server.sin_addr.s_addr=inet_addr("127.0.1");
server.sin_port=htons(12345);
server.sin_family=AF_INET;
len =sizeof(server);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
bind(sockfd,(struct sockaddr*)&server,len);
while(run)
{
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&client,&len);
repro = (struct protocol*)malloc(sizeof(struct protocol));
pro = (struct protocol*)malloc(sizeof(struct protocol));
memcpy(repro,recvbuf,sizeof(recvbuf));
int select;
switch(repro->command)
{
case SHUTDOWN:
pro->command=SHUTDOWN;
pro->no=checkcode++;
strcpy(pro->buf,"nullptr");
pro->len=strlen("nullptr");
run=0;
break;
case DOWNLOAD:
pro->no=++repro->no;
if(-1==(readfd=open(repro->buf,O_RDWR,0666)))
{
pro->command=NO;
printf("yes---------->\n");
}
else
{
pro->command=YES;
printf("no:---------->\n");
}
strcpy(pro->buf,"nullptr");
pro->len=strlen("nullptr");
break;
case UPLOAD:
printf("client want to upload a file ,do you allow?\n");
printf(" 1 START other no \n");scanf("%d",&select);
if(select==1)
{
pro->command=START;
pro->no=checkcode++;
strcpy(pro->buf,"nullptr");
pro->len= sizeof("nullptr");
writefd=open(repro->buf,O_RDWR|O_CREAT,0666);
}
else
{
pro->command=NO;
pro->no=checkcode++;
strcpy(pro->buf,"nullptr");
pro->len= sizeof("nullptr");
}
break;
case START:
read(readfd,readbuf,sizeof(readbuf));
pro->command=CONTENT;
pro->no=checkcode++;
strcpy(pro->buf,readbuf);
pro->len=strlen(readbuf);
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&client,len);
printf("wait to finish download.........\n");
free(pro);
pro = (struct protocol*)malloc(sizeof(struct protocol));
pro->command=END;
pro->no=checkcode++;
strcpy(pro->buf,"EDN");
pro->len=strlen("EDN");
break;
case CONTENT:
write(writefd,repro->buf,sizeof(repro->buf));
close(writefd);
printf("upload will be finish\n");
break;
case END:
printf("\t upload is ok \n");
break;
default :
printf("default command :%d\n",repro->command);
pro->command=100;
pro->no=0;
strcpy(pro->buf,"connect");
pro->len=strlen("connect");
break;
}
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&client,len);
printf("\tcommand-->%d\n\tlen------>%d\n\tno------->%d\n\tbuf------>%s\n",
pro->command,pro->len,pro->no,pro->buf);
free(pro);
free(repro);
}
close(sockfd);
return 0;
}
客户端部分
#include"protocol.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"stdio.h"
#include"stdlib.h"
#include"netinet/in.h"
#include"fcntl.h"
int main()
{
int sockfd;
int run=1;
int slip=1;
int writefd;
int readfd;
struct sockaddr_in server,client;
char recvbuf[1000];
char readbuf[1000];
char writebuf[1000];
struct protocol *pro,*repro;
char sendbuf[INFOLEN];
char filename[20];
socklen_t len;
server.sin_addr.s_addr=inet_addr("127.0.1");
server.sin_port=htons(12345);
server.sin_family=AF_INET;
len =sizeof(server);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
int checkcode=0;
pro=(struct protocol*)malloc(sizeof(struct protocol));
char inputbuf[1000];
//first connect
pro->command=100;
pro->no=0;
strcpy(pro->buf,"nullptr");
pro->len=strlen("nullptr");
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&server,len);
printf("\t connect .............\n");
free(pro);
while(run)
{
repro = (struct protocol*)malloc(sizeof(struct protocol));
pro=(struct protocol*)malloc(sizeof(struct protocol));
recvfrom(sockfd,recvbuf,sizeof(recvbuf),0,(struct sockaddr*)&server,&len);
memcpy(repro,recvbuf,sizeof(recvbuf));
switch(repro->command)
{
case YES:
//download yes
pro->command=START;
pro->no=checkcode++;
strcpy(pro->buf,"nullptr");
pro->len=strlen("nullptr");
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&server,len);
printf("\t download is yes \n");
free(pro);
slip=0;
pro=(struct protocol*)malloc(sizeof(struct protocol));
break;
case CONTENT:
writefd=open(filename,O_RDWR|O_CREAT,0666);
write(writefd,repro->buf,strlen(repro->buf));
close(writefd);
printf("download is ok cloent\n");
break;
case NO:
//download no
printf("\thave no such file\n");
break;
case START:
//upload start
readfd=open(filename,O_RDWR|O_CREAT,0666);
read(readfd,readbuf,sizeof(readbuf));
pro->command=CONTENT;
pro->no=checkcode++;
strcpy(pro->buf,readbuf);
pro->len=strlen(readbuf);
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&server,len);
printf("\t upload is start \n");
free(pro);
pro=(struct protocol*)malloc(sizeof(struct protocol));
pro->command=END;
pro->no=checkcode++;
strcpy(pro->buf,"END");
pro->len=strlen("END");
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&server,len);
break;
case SHUTDOWN:
//service had shutdown
run =0;
break;
case END:
printf("\t download is ok \n");
slip=1;
break;
default :
printf("default ............%d \n",repro->command);
break;
}
free(repro);
if(run+slip>=2)
{
printf("\t*********************************************************\n");
printf("\t*********************** Client *********************\n");
printf("\t*********************************************************\n");
printf("\t*********************** 0 SHUTDOWN ******************\n");
printf("\t*********************** 1 DOWNLOAD ******************\n");
printf("\t*********************** 3 UPLOAD ******************\n");
printf("\t*********************************************************\n");
printf("please input a num:\t");int n;scanf("%d",&n);
switch(n)
{
case SHUTDOWN:
pro->command=SHUTDOWN;
pro->no=checkcode++;
strcpy(pro->buf,"nullptr");
pro->len=strlen("nullptr");
break;
case DOWNLOAD:
pro->command=DOWNLOAD;
pro->no=checkcode++;
printf("\t please input the name of the download-file:\t");scanf("%s",&inputbuf);
strcpy(pro->buf,inputbuf);
strcpy(filename,inputbuf);
pro->len=strlen(inputbuf);
break;
case UPLOAD:
pro->command=UPLOAD;
pro->no=checkcode++;
printf("\t please input the name of the upload-file):\t");scanf("%s",&inputbuf);
strcpy(pro->buf,inputbuf);
strcpy(filename,inputbuf);
pro->len=strlen(inputbuf);
break;
default :
break;
}
memcpy(sendbuf,pro,INFOLEN);
sendto(sockfd,sendbuf,sizeof(sendbuf),0,(struct sockaddr *)&server,len);
printf("\tcommand-->%d\n\tlen------>%d\n\tno------->%d\n\tbuf------>%s\n",
pro->command,pro->len,pro->no,pro->buf);
}
}
close(sockfd);
return 0;
}
协议结构体
#ifndef _protocol
#define _protocol
#define INFOLEN 1000
#define SHUTDOWN 0
#define DOWNLOAD 1
#define UPLOAD 2
#define YES 3
#define NO 4
#define START 5
#define END 6
#define CONTENT 7
#define OK 8
struct protocol
{
int command;
int len; //length of buf
int no;
char buf[INFOLEN];
};
#endif
版权声明:本文为博主原创文章,未经博主允许不得转载。