UDP流程图
服务器
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%s__ __%s__ __%d__ ", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)
#define PORT 6666 //1024~49151
#define IP "000.000.000.000" //ifconfig
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 __%d__\n", sfd, __LINE__);
//填充服务器的地址信息结构体, AF_INET: man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //1024~49151
sin.sin_addr.s_addr = inet_addr(IP); //ifconfig
//绑定服务器自身的地址信息
if(bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
char buf[128] = "";
struct sockaddr_in cin; //存储客户端的地址信息
socklen_t addrlen = sizeof(cin);
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
//接收
res = recvfrom(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, &addrlen);
//res = recvfrom(sfd, buf, sizeof(buf), 0, NULL, NULL);
//res = recv(sfd, buf, sizeof(buf), 0);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s __%d__\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), buf, __LINE__);
//发送 --> 谁发给我,我发还给谁
strcat(buf, "*_*");
if(sendto(sfd, buf, sizeof(buf), 0, (struct sockaddr*)&cin, sizeof(cin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
printf("sendto success __%d__\n", __LINE__);
}
//关闭
if(close(sfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
客户端
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "__%s__ __%s__ __%d__ ", __FILE__, __func__, __LINE__);\
perror(msg);\
}while(0)
#define PORT 6666 //1024~49151
#define IP "000.000.000.000" //ifconfig
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 __%d__\n", cfd, __LINE__);
//绑定客户端自身的地址信息---》非必须绑定
//若不绑定则操作系统会自动给客户端绑定上一个可用IP以及随机端口。
/*
//绑定服务器自身的地址信息
if(bind(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("bind");
return -1;
}
printf("bind success __%d__\n", __LINE__);
*/
//填充服务器的地址信息结构体,给sendto函数使用
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //服务器的端口
sin.sin_addr.s_addr = inet_addr(IP); //服务器的IP
char buf[128] = "";
struct sockaddr_in dstAddr;
socklen_t addrlen = sizeof(dstAddr);
ssize_t res = 0;
while(1)
{
bzero(buf, sizeof(buf));
printf("请输入>>> ");
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 __%d__\n", __LINE__);
//接收
res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&dstAddr, &addrlen);
//res = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
//res = recv(cfd, buf, sizeof(buf), 0);
//res = read(cfd, buf, sizeof(buf));
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
printf("[%s:%d] : %s __%d__\n", \
inet_ntoa(dstAddr.sin_addr), ntohs(dstAddr.sin_port), buf, __LINE__);
}
//关闭
if(close(cfd) < 0)
{
ERR_MSG("close");
return -1;
}
return 0;
}
IP地址和端口号可自行修改