UDP服务器
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.0.106"
#define PORT 9119
int main(int argc, const char *argv[])
{
//创建套接字:socket
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success sfd = %d __%d__\n",sfd,__LINE__);
//填充服务器的地址信息结构体,绑定到套接字上
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定ip地址---》必须绑定:bind
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n",__LINE__);
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in cin;
socklen_t addelen = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
//接收数据:recvfrom
res = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addelen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//发送数据:sendto
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
//关闭套接字:close
close(sfd);
return 0;
}
UDP客户端
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.0.106"
#define PORT 9119
int main(int argc, const char *argv[])
{
//创建套接字:socket
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd = %d __%d__\n",cfd,__LINE__);
//填充服务器的地址信息结构体,
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//绑定ip地址---》非必须绑定:bind
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in cin;
socklen_t addelen = sizeof(cin);
while(1)
{
bzero(buf,sizeof(buf));
//发送数据:sendto
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = 0;
//将数据发给服务器,所以需要填充服务器的地址信息
if(sendto(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
bzero(buf,sizeof(buf));
//接收数据:recvfrom
res = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&cin,&addelen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]:%s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
}
//关闭套接字:close
close(cfd);
return 0;
}
基于UDP的上传
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.0.179"
#define PORT 69
int download(int cfd , struct sockaddr_in sin) //下载功能
{
char buf[128] = "";
bzero(buf,sizeof(buf));
unsigned short *p1 = (unsigned short*)buf;
*p1 = htons(1); //操作码
char *p2 = buf + 2;
char name[64] = "";
printf("\t请输入要下载的文件名>>");
scanf("%s",name);
getchar();
strcpy(p2,name); //要下载的文件名
char *p3 = p2 + strlen(p2);
*p3 = 0;
char *p4 =p3+1;
strcpy(p4,"octet"); //模式
char *p5 = p4 +strlen(p4);
*p5 = 0;
int size = 2+1+1+strlen(p2)+strlen(p4);
//功能:将数据打印到指定的内存空间中,常用语族包
// int size = sprintf(buf,"%c%c%s%c%s%c",0x00,0x01,"dict.txt",0,"octet",0);
if(sendto(cfd,buf,size,0,(struct sockaddr *)&sin,sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("发送成功\n");
struct sockaddr_in my_addr; //储存接收到的数据包
socklen_t addrlen = sizeof(my_addr);
//打开文件
int fd = open (name,O_WRONLY | O_TRUNC | O_CREAT,0666);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
short int num = 1; //块编号
char buf1[516] = "";//数据包
char buf2[6] = "";//ACK
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
//接收数据包
res = recvfrom(cfd,buf1,sizeof(buf1), 0,(struct sockaddr*)&my_addr,&addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
//发送ASK
if(sendto(cfd,buf2,4,0,(struct sockaddr *)&my_addr,addrlen) < 0)
{
ERR_MSG("sendto");
return -1;
}
if(res < 516)
{
break;
}
}
printf("下载完成\n");
return 0;
}
/*
void upload() //上传功能
*/
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket create success cfd =%d __%d__\n",cfd,__LINE__);
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
int a = 0; //页面设置
while(1)
{
printf("\t**************************\n");
printf("\t********* 1.下载 *********\n");
printf("\t********* 2.上传 *********\n");
printf("\t********* 3.退出 *********\n");
printf("\t**************************\n");
printf("\t请选择你的数字!>>>\n");
scanf("%d",&a);
switch(a)
{
case 1:
download(cfd,sin);//下载
break;/*
case 2:
upload(cfd,sin); //上传
break;*/
case 3:
break; //退出
}
}
//关闭套接字
close(cfd);
return 0;
}