1>广播
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_INET, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}int broadcast = 1; if (setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) == -1){perror("setsockopt error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);char sbuf[128] = "";while (1){printf("请输入>>>");fgets(sbuf, sizeof(sbuf), stdin);sbuf[strlen(sbuf) - 1] = 0;sendto(sfd, sbuf, strlen(sbuf), 0, (struct sockaddr*)&sin, sizeof(sin));printf("发送成功\n");}close(sfd);return 0;
}
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_INET, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT);sin.sin_addr.s_addr = inet_addr(SER_IP);if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) == -1){perror("bind error");return -1;}char rbuf[128] = "";while (1){bzero(rbuf, sizeof(rbuf));recv(sfd, rbuf, strlen(rbuf), 0);printf("收到消息:%s\n", rbuf);}close(sfd);return 0;
}
2>组播
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_INET, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}struct sockaddr_in rin;rin.sin_family = AF_INET;rin.sin_port = htons(SER_PORT);rin.sin_addr.s_addr = inet_addr(SER_IP);if (bind(sfd, (struct sockaddr*)&rin, sizeof(rin)) == -1){perror("bind error");return -1;}struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(8888);sin.sin_addr.s_addr = inet_addr("224.1.2.3");char sbuf[128] = "";while(1){printf("请输入>>>");fgets(sbuf, sizeof(sbuf), stdin);sbuf[strlen(sbuf) - 1] = 0;sendto(sfd, sbuf, strlen(sbuf), 0, (struct sockaddr*)&sin, sizeof(sin));printf("发送成功\n");}close(sfd);return 0;
}
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_INET, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}struct ip_mreqn imr;imr.imr_multiaddr.s_addr = inet_addr("224.1.2.3");imr.imr_address.s_addr = inet_addr(SER_IP);imr.imr_ifindex = 2;if (setsockopt(sfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &imr, sizeof(imr)) == -1){perror("setsockopt error");return -1;}struct sockaddr_in rin;rin.sin_family = AF_INET;rin.sin_port = htons(SER_PORT);rin.sin_addr.s_addr = inet_addr(SER_IP);if (bind(sfd, (struct sockaddr*)&rin, sizeof(rin)) == -1){perror("bind error");return -1;}char rbuf[128] = "";while(1){bzero(rbuf, sizeof(rbuf));recv(sfd, rbuf, strlen(rbuf), 0);printf("收到消息:%s\n", rbuf);}close(sfd);return 0;
}
3>流式域套接字
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_UNIX, SOCK_STREAM, 0);if(sfd == -1){perror("socket error");return -1;}if (access("./mysocket", F_OK) == -1){if (unlink("./mysocket") == -1){perror("unlink error");return -1;}}struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path, "./mysocket");if (bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}if (listen(sfd, 128) == -1){perror("listen error");return -1;}struct sockaddr_un cun;socklen_t socklen = sizeof(cun);int newfd = -1;if ((newfd = accept(sfd, (struct sockaddr*)&cun, &socklen)) == -1){perror("accept error");return -1;}char rbuf[128] = "";while(1){bzero(rbuf, sizeof(rbuf));recv(sfd, rbuf, strlen(rbuf), 0);printf("[%s] : %s\n", cun.sun_path, rbuf);strcat(rbuf, "*_*");send(newfd, rbuf, strlen(rbuf), 0);printf("发送成功\n");}close(sfd);close(newfd);return 0;
}
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_UNIX, SOCK_STREAM, 0);if(sfd == -1){perror("socket error");return -1;}if (access("./mysocket1", F_OK) == -1){if (unlink("./mysocket1") == -1){perror("unlink error");return -1;}}struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path, "./mysocket1");if (bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}struct sockaddr_un cun;cun.sun_family = AF_UNIX;strcpy(cun.sun_path, "./mysocket1");if (connect(sfd, (struct sockaddr*)&cun, sizeof(cun)) == -1){perror("connect error");return -1;}char wbuf[128] = "";while(1){bzero(wbuf, sizeof(wbuf));printf("请输入>>>");fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] = 0;send(sfd, wbuf, strlen(wbuf), 0);printf("发送成功\n");if (strcmp(wbuf, "quit") == 0){break;}bzero(wbuf, sizeof(wbuf));recv(sfd, wbuf, strlen(wbuf), 0);printf("收到消息:%s\n", wbuf);}close(sfd);return 0;
}
4>报域式嵌套字
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}if (access("./linux1", F_OK) == -1){if (unlink("./linux1") == -1){perror("unlink error");return -1;}}struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path, "./linux1");if (bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}struct sockaddr_un cun;socklen_t socklen = sizeof(cun);char rbuf[128] = "";while(1){bzero(rbuf, sizeof(rbuf));recvform(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, &socklen);printf("收到消息:%s\n", rbuf);strcat(rbuf, "*_*");sendto(sfd, rbuf, strlen(rbuf), 0, (struct sockaddr*)&cun, socklen);}close(sfd);return 0;
}
#include "test.h"#define SER_IP "192.168.42.106"
#define SER_PORT 8888int main(int argc, const char *argv[])
{int sfd = socket(AF_UNIX, SOCK_DGRAM, 0);if(sfd == -1){perror("socket error");return -1;}if (access("./linux2", F_OK) == -1){if (unlink("./linux2") == -1){perror("unlink error");return -1;}}struct sockaddr_un sun;sun.sun_family = AF_UNIX;strcpy(sun.sun_path, "./linux2");if (bind(sfd, (struct sockaddr*)&sun, sizeof(sun)) == -1){perror("bind error");return -1;}struct sockaddr_un cun;cun.sun_family = AF_UNIX;strcpy(cun.sun_path, "./linux1");char wbuf[128] = "";while(1){bzero(wbuf, sizeof(wbuf));printf("请输入>>>");fgets(wbuf, sizeof(wbuf), stdin);wbuf[strlen(wbuf) - 1] = 0;sendto(sfd, wbuf, strlen(wbuf), 0, (struct sockaddr*)&cun, sizeof(cun));printf("发送成功\n");bzero(wbuf, sizeof(wbuf));recvfrom(sfd, wbuf, strlen(wbuf), 0, NULL, NULL);printf("收到消息:%s\n", wbuf);}close(sfd);return 0;
}