服务器
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.31.45" //本机IP ifconfig
#define PORT 6666
int main(int argc, const char *argv[])
{
//创建报式套接字
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__);
//填充服务器的地址信息结构体,
//真实的地址信息结构体根据地址族指定 AF_INET:man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(PORT) ; //端口号,1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //本机IP, ifconfig
//绑定--->必须绑定
//功能:将服务器的ip地址和端口号绑定到服务器套接字文件中;
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;
while(1)
{ //对于字符串的操作,操作之前或者操作之后要清空
//防止之前的数据对这次操作有干扰
bzero(buf, sizeof(buf));
//接收
res=read(sfd,buf,sizeof(buf));
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf(":%s\n",buf);
}
//关闭文件描述符
close(sfd);
return 0;
}
客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define SER_PORT 6666 //1024~49151
#define SER_IP "192.168.31.45"
#define CLI_PORT 3333
#define CLI_IP "192.168.31.45" //多个接口
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__);
//填充服务器的地址信息结构体,给下面的sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填充AF_INET
sin.sin_port = htons(SER_PORT); //1024~49151
sin.sin_addr.s_addr = inet_addr(SER_IP); //ifconfig出来的本机IP
char buf[128] = "";
ssize_t res = 0;
struct sockaddr_in rcvaddr; //存储数据包从谁哪里来
socklen_t addrlen = sizeof(rcvaddr);
while(1)
{
bzero(buf, sizeof(buf));
//发送数据
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = 0;
//sendto将数据发送给服务器,所以需要填充服务器的地址信息结构体
if(sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
bzero(buf, sizeof(buf));
//接收数据
res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcvaddr, &addrlen);
if(res < 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;
}
下载
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d ", __LINE__);\
perror(msg);\
}while(0)
#define PORT 69 //1024~49151
#define IP "192.168.0.154"
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__);
//填充服务器的地址信息结构体,给下面的sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
//发送下载请求
char buf[516]="";
bzero(buf,sizeof(buf));
unsigned short* p1=(unsigned short*)buf;
*p1=htons(1); //操作码
char* p2=buf+2;
strcpy(p2,"dict.txt"); //要下载的文件名
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);
//sendto将数据发送给服务器,所以 需要填充服务器的地址信息结构体
if(sendto(cfd, buf, size, 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success\n");
while(1)
sleep(1);
}