udp客户端
#include <myhead.h>
#define IP "192.168.8.22"
#define PORT "8888"
#define ERR_MSG(msg) do{\
fprintf(stderr,"__%d__",__LINE__);\
perror(msg);\
}while(0)
int main(int argc, const char *argv[])
{
int cfd=socket(AF_INET,SOCK_DGRAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd =%d\n",cfd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin_port=htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
struct sockaddr_in rcvaddr;
socket_t addrlen =sizeof(rcvaddr);
char buf[128]="";
while(1)
{
bzero(buf,sizeof(buf));
printf("please input");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if(sendto(cfd ,buf,sizeof(buf),0,(struct sockaddr)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success \n");
if(recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr *)&rcvaddr,&addrlen)<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(rcvaddr.sin_addr),ntohs(rcvaddr.sin_port),buf);
}
close(cfd);
return 0;
}
udp客户端
#include <myhead.h>
#define ERR_MSG(msg)do{\
fpritntf(stderr,"__%d__",__LINE__);\
perrror(msg);\
}while(0)
#define IP "192.168.8.22"
#define PORT "8888"
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("SOCKET");
return -1;
}
printf("sfd= %d\n",sfd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin.sin_port=htons(PORT);
sin,sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_in cin;//存储从外部来的数据包
socklen_t addrlen=sizeof(cin);
char buf[128];
while(1)
{
bzero(buf,sizeof(buf));
if(recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr *)&cin,&addrlen)<0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s : %d]: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
//发送信息
strcat(buf,"*_*");
if(sendto(sfd,buf,sizeof(buf),0,(struct sockaddr)&cin,&addrlen)<0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
}
close(sfd);
return 0;
}
tftp下载
#include <myhead.h>
#define ERR_MSG(msg)do{\
fpritntf(stderr,"__%d__",__LINE__);\
perrror(msg);\
}while(0)
#define IP "192.168.8.22"
#define PORT "8888"
void download(int ufd,struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
int sfd=socket(AF_INET,SOCK_DGRAM,0);
if(sfd<0)
{
ERR_MSG("SOCKET");
return -1;
}
printf("sfd= %d\n",sfd);
struct sockaddr_in sin;
sin.sin_family=AF_INET;
sin.sin.sin_port=htons(PORT);
sin,sin_addr.s_addr=inet_addr(IP);
if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success\n");
struct sockaddr_in cin;//存储从外部来的数据包
socklen_t addrlen=sizeof(cin);
char buf[128];
while(1)
{
case '1':
{
}break;
case '2':
{
download(sfd,sin);
}break;
case '3':
{
exit(EXIT_SUCCESS);
}break;
default:printf("error\n"); break;
}
close(sfd);
return 0;
}
void download(int ufd,struct sockaddr_in sin)
{
char buf[516]="";
short* p1=(short*)buf;
*p1=htons(1);
char *p2=(char*)(p1+1);
printf("input name of file\n");
scanf("%s",p2);
while(getchar()!='\n');
char *p3=p2+strlen(p2)+1;
strcpy(p3,"octet");
size_t bufsize = 4+strlen(p2)+strlen(p3);
//发送下载请求
if(sendto(ufd,buf,bufsize,0,(struct sockaddr*)&sin,sizeof(sin))==-1)
PRI_ERR("send err");
//打开一个用于写入的文件
int fd = open(p2,O_WRONLY|O_CREAT|O_APPEND,0664);
if(fd == -1)
PRI_ERR("fd err");
//循环接收数据,发送ACK,判断数据大小
while(1)
{
//接受数据
bzero(buf,sizeof(buf));//将buf清空
socklen_t addlen = sizeof(sin);
int datasize = 0;
if((datasize = recvfrom(ufd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addlen))==-1)
PRI_ERR("recvfrom err");
if(datasize<516)//判断数据的大小
break;
char* data = buf+4;
write(fd,data,512);
short *id = (short*)buf;
short ACK = *id;
short num = *(id+1);
id +=1;
//组ACK包
bzero(buf,sizeof(buf));
short *A = (short*)buf;
*A = htons(4);
*(A+1) = num;
//将ACK发给服务器
if(sendto(ufd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin))==-1)
PRI_ERR("send err");
}
close(fd);
return;
}