socket
IP
和PORT
IP
和PORT
struct sockaddr_in cli;
cli.sin_family = AF_INET;
cli.port = htos(9898);
inet_pton(AF_INET, "xxx.xxx.xxx.125", &cli.adr);
sendto(fd, buf, len, 0)
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
// 创建套接字
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1)
{
perror("socket error");
exit(1);
}
// 绑定server的iP和端口
struct sockaddr_in serv;
memset(&serv, 0, sizeof(serv));
serv.sin_family = AF_INET;
serv.sin_port = htons(8787); // server端口
serv.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(fd, (struct sockaddr*)&serv, sizeof(serv));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 初始化客户端地址信息
struct sockaddr_in client;
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(6767); // 客户端要绑定的端口
// 使用广播地址给客户端发数据
inet_pton(AF_INET, "192.168.123.255", &client.sin_addr.s_addr);
// 给服务器开放广播权限
int flag = 1;
setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &flag, sizeof(flag));
// 通信
while(1)
{
// 一直给客户端发数据
static int num = 0;
char buf[1024] = {0};
sprintf(buf, "hello, udp == %d\n", num++);
int ret = sendto(fd, buf, strlen(buf)+1, 0, (struct sockaddr*)&client, sizeof(client));
if(ret == -1)
{
perror("sendto error");
break;
}
printf("server == send buf: %s\n", buf);
sleep(1);
}
close(fd);
return 0;
}
客户端
创建套接字
显示绑定IP
和PORT
bind
接受数据 - server数据
recvfrom();
code
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if(fd == -1)
{
perror("socket error");
exit(1);
}
// 绑定iP和端口
struct sockaddr_in client;
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(6767);
inet_pton(AF_INET, "0.0.0.0", &client.sin_addr.s_addr);
int ret = bind(fd, (struct sockaddr*)&client, sizeof(client));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 接收数据
while(1)
{
char buf[1024] = {0};
int len = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
if(len == -1)
{
perror("recvfrom error");
break;
}
printf("client == recv buf: %s\n", buf);
}
close(fd);
return 0;
}
跟广播类似,需要使用组播地址
224.0.0.0~224.0.0.255
224.0.0.0
保留不做分配,其他地址供路由协议使用。224.0.1.0~224.0.1.255
224.0.2.0~238.255.255.255
239.0.0.0~239.255.255.255
//头文件
#include
// 使用组播地址给客户端发数据
inet_pton(AF_INET, "239.0.0.10", &client.sin_addr.s_addr);
// 给服务器开放组播权限
struct ip_mreqn flag;
// init flag
inet_pton(AF_INET, "239.0.0.10", &flag.imr_multiaddr.s_addr); // 组播地址
inet_pton(AF_INET, "0.0.0.0", &flag.imr_address.s_addr); // 本地IP
flag.imr_ifindex = if_nametoindex("ens33");
setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &flag, sizeof(flag));
#include
// 加入到组播地址
struct ip_mreqn fl;
inet_pton(AF_INET, "239.0.0.10", &fl.imr_multiaddr.s_addr);
inet_pton(AF_INET, "0.0.0.0", &fl.imr_address.s_addr);
fl.imr_ifindex = if_nametoindex("ens33");
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &fl, sizeof(fl));
int lfd = socket(AF_LOACAL, SOCK_STREAM, 0);
struct sockaddr_un serv;
serv.sun_family = AF_LOCAL;
strcpy(ser.sun_path."server.socket");
–后缀.socket为了区分bind(lfd, (struct sockaddr*)&serv, len);
listen()
struct sockaddr_un client;
int len = sizeof(client);
int cfd = accept(lfd, &client, &len);
send()
recv()
close(cfd);
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
int lfd = socket(AF_LOCAL, SOCK_STREAM, 0);
if(lfd == -1)
{
perror("socket error");
exit(1);
}
// 如果套接字文件存在, 删除套接字文件
unlink("server.sock");
// 绑定
struct sockaddr_un serv;
serv.sun_family = AF_LOCAL;
strcpy(serv.sun_path, "server.sock");
int ret = bind(lfd, (struct sockaddr*)&serv, sizeof(serv));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 监听
ret = listen(lfd, 36);
if(ret == -1)
{
perror("listen error");
exit(1);
}
// 等待接收连接请求
struct sockaddr_un client;
socklen_t len = sizeof(client);
int cfd = accept(lfd, (struct sockaddr*)&client, &len);
if(cfd == -1)
{
perror("accept error");
exit(1);
}
printf("======client bind file: %s\n", client.sun_path);
// 通信
while(1)
{
char buf[1024] = {0};
int recvlen = recv(cfd, buf, sizeof(buf), 0);
if(recvlen == -1)
{
perror("recv error");
exit(1);
}
else if(recvlen == 0)
{
printf("clietn disconnect ....\n");
close(cfd);
break;
}
else
{
printf("recv buf: %s\n", buf);
send(cfd, buf, recvlen, 0);
}
}
close(cfd);
close(lfd);
return 0;
}
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
struct sockaddr_un client;
client.sun_family = AF_LOCAL;
strcpy(client.sun_path, "client.socket");
bind(fd, (struct sockaddr*)&client, len);
struct sockaddr_un serv;
ser.sun_family = AF_LOCAL;
strcpy(serv.sun_path,"server.socket");
connect(fd, &serv,sizeof(server));
recv
send
close();
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, const char* argv[])
{
int fd = socket(AF_LOCAL, SOCK_STREAM, 0);
if(fd == -1)
{
perror("socket error");
exit(1);
}
unlink("client.sock");
// ================================
// 给客户端绑定一个套接字文件
struct sockaddr_un client;
client.sun_family = AF_LOCAL;
strcpy(client.sun_path, "client.sock");
int ret = bind(fd, (struct sockaddr*)&client, sizeof(client));
if(ret == -1)
{
perror("bind error");
exit(1);
}
// 初始化server信息
struct sockaddr_un serv;
serv.sun_family = AF_LOCAL;
strcpy(serv.sun_path, "server.sock");
// 连接服务器
connect(fd, (struct sockaddr*)&serv, sizeof(serv));
// 通信
while(1)
{
char buf[1024] = {0};
fgets(buf, sizeof(buf), stdin);
send(fd, buf, strlen(buf)+1, 0);
// 接收数据
recv(fd, buf, sizeof(buf), 0);
printf("recv buf: %s\n", buf);
}
close(fd);
return 0;
}