#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
// udp socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("socket");
exit(1);
}
struct sockaddr_in ser, cli;
bzero(&ser, sizeof(ser));
bzero(&cli, sizeof(cli));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = inet_addr("192.168.157.128"); // atoi
int ret = bind(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
exit(1);
}
socklen_t len = sizeof(cli);
while (1) {
char buf[1024] = {0};
recvfrom(sockfd, buf, sizeof(buf), 0, (SA)&cli, &len);
printf("from cli:%s\n", buf);
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
sendto(sockfd, buf, strlen(buf), 0, (SA)&cli, len);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("sockfd");
return 1;
}
struct sockaddr_in ser;
bzero(&ser, sizeof(ser));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = inet_addr("192.168.157.128"); // atoi
while (1) {
char buf[1024] = "hello,this is udp test ";
sendto(sockfd, buf, strlen(buf), 0, (SA)&ser, sizeof(ser));
bzero(buf, sizeof(buf));
recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
printf("from ser:%s", buf);
fflush(stdout);
sleep(1);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
// udp socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("socket");
exit(1);
}
struct sockaddr_in ser, cli;
bzero(&ser, sizeof(ser));
bzero(&cli, sizeof(cli));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = inet_addr("127.0.0.1"); // 本地回环地址,只能自己收发
int ret = bind(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
exit(1);
}
socklen_t len = sizeof(cli);
int fd = open("2.png",O_WRONLY|O_CREAT|O_TRUNC,0666); //"w"
if(-1 == fd)
{
perror("open");
exit(1);
}
while (1) {
char buf[4096] = {0};
int rd_ret = recvfrom(sockfd, buf, sizeof(buf), 0, (SA)&cli, &len);
if(0 == strcmp(buf,"^_^"))
{
break;
}
write(fd,buf,rd_ret);
bzero(buf,sizeof(buf));
strcpy(buf,"go on");
sendto(sockfd, buf, strlen(buf), 0, (SA)&cli, len);
}
close(fd);
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("sockfd");
return 1;
}
struct sockaddr_in ser;
bzero(&ser, sizeof(ser));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = inet_addr("127.0.0.1"); // atoi
int fd = open("/home/linux/1.png",O_RDONLY);
if(-1 == fd)
{
perror("open");
exit(1);
}
char buf[4096] = {0};
while (1) {
bzero(buf,sizeof(buf)) ;
int rd_ret = read(fd,buf,sizeof(buf));
if(rd_ret<=0)
{
break;
}
sendto(sockfd, buf,rd_ret, 0, (SA)&ser, sizeof(ser)); // strlen() error sizeof() error
bzero(buf, sizeof(buf));
recvfrom(sockfd, buf, sizeof(buf), 0, NULL, NULL);
}
bzero(buf,sizeof(buf)) ;
strcpy(buf,"^_^");
sendto(sockfd, buf,strlen(buf), 0, (SA)&ser, sizeof(ser));
close(fd);
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
// udp socket
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("socket");
exit(1);
}
struct sockaddr_in ser, cli;
bzero(&ser, sizeof(ser));
bzero(&cli, sizeof(cli));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = INADDR_ANY; // atoi man 7 ip
int ret = bind(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
exit(1);
}
socklen_t len = sizeof(cli);
char buf[1024] = {0};
recvfrom(sockfd, buf,sizeof(buf),0,(SA)&cli,&len);
pid_t pid = fork();
if(pid>0)
{
while (1) {
bzero(buf,sizeof(buf));
printf("to cli:");
fgets(buf, sizeof(buf), stdin);
sendto(sockfd, buf, strlen(buf), 0, (SA)&cli, sizeof(cli));
}
}
else if (0 == pid)
{
while(1)
{
bzero(buf,sizeof(buf));
recvfrom(sockfd, buf,sizeof(buf),0,(SA)&cli,&len);
printf("from cli:%s\n",buf);
}
}
else
{
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("sockfd");
return 1;
}
struct sockaddr_in ser;
bzero(&ser, sizeof(ser));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = INADDR_ANY; // atoi
char buf[1024] = {0};
strcpy(buf,"hello");
sendto(sockfd, buf, strlen(buf), 0, (SA)&ser, sizeof(ser));
pid_t pid = fork();
if(pid>0)
{
while (1) {
bzero(buf,sizeof(buf));
printf("to ser:");
fgets(buf, sizeof(buf), stdin);
sendto(sockfd, buf, strlen(buf), 0, (SA)&ser, sizeof(ser));
}
}
else if (0 == pid)
{
while(1)
{
bzero(buf,sizeof(buf));
recvfrom(sockfd, buf,sizeof(buf),0,NULL,NULL);
printf("from ser:%s\n",buf);
}
}
else
{
}
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
//监听套接子
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
// man 7 ip
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3); // 建立链接的排队数
socklen_t len = sizeof(cli);
//通信套接子
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn) {
perror("accept");
return 1;
}
while (1) {
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0) {
break;
}
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
send(conn, buf, strlen(buf), 0);
}
close(conn);
close(listfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
int i = 10;
while (i--) {
char buf[2048] = "hello,this tcp test";
send(sockfd, buf, strlen(buf), 0);
bzero(buf, sizeof(buf));
recv(sockfd, buf, sizeof(buf), 0);
printf("%s", buf);
fflush(stdout);
sleep(1);
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
int flag = 0 ;
static int count = 0 ;
typedef struct sockaddr *(SA);
unsigned char totalbuf[4096];
typedef struct {
float temp;
int humi;
float vol;
float current;
float light;
unsigned char buf[50];
int buflen;
} DATA;
unsigned char GetCheckSum(unsigned short sum) {
unsigned char checksum = (256 - (sum % 256)) % 256;
return checksum;
}
unsigned short GetBufSum(unsigned char *buf,int len)
{
unsigned short sum = 0;
int i = 0 ;
for(i = 0 ;i<len;i++)
{
sum+=buf[i];
}
return sum;
}
// 00 temp(4) 01humi(4) 02vol(4) 03current(4) 04light(4)
//接受
// 开始 0读 起始地址 个数 check end
// 0xAA 0x00 0x0 0x02 ab 0xBB
//回复 读 长度 data1 data2 check end
//0xAA 0x0 0x8 0x11 0x22 0x33 0x44 0x11 0x22 0x33 0x44 bc 0xBB
void parsebuf(unsigned char *buf, int len, int sockfd,DATA *data)
{
unsigned char replybuf[30]={0};
int offset = 0;
int start_addr = 0 ;
int start_num = 0;
int aaa = rand()%9+1;
if (buf[0] == 0xAA && buf[len - 1] == 0xBB)
{
//check sum
unsigned short checksum =GetBufSum(buf+1, 3);
unsigned char result = GetCheckSum(checksum);
if(result != buf[len-2])
{
fprintf(stderr,"check sum error\n");
return ;
}
if(buf[1] == 0x00) //read data
{
start_addr=buf[2]; //起始地址
start_num = buf[3];//个数
if(start_num<1 || start_num >5)
{
return ;
}
replybuf[0]=0xAA;//start flag
replybuf[1]=0x0; //read flag
replybuf[2]= buf[3] * 4; // 每个都是4byte 数据的长度
offset +=3;
memcpy(replybuf+offset,data->buf +start_addr, buf[3]*4);
checksum= GetBufSum(replybuf+1, replybuf[2] +2); //+2 读的flag 长度
result = GetCheckSum(checksum);
offset += replybuf[2];
replybuf[offset] = result; //checksum
offset+=1;
replybuf[offset] = 0xbb; //end flag
offset+=1;
//sendto(sockfd,replybuf,offset,0,(SA)cli,sizeof(struct sockaddr_in));
count++;
if(count%9 == 0 )
{
if(flag == 0 )
{
replybuf[2]+=rand()%3 +1;
flag =1;
}
else
{
unsigned char tmp = rand()%0xff;
replybuf[offset-1] = tmp;
flag =0;
}
}
send(sockfd,replybuf,offset,0);
}
else // write data not support
{
}
}
else
{
}
}
void setvalue(DATA* data,unsigned char *buf)
{
int len = 0 ;
//temp
memcpy(buf+len,&data->temp,sizeof(data->temp));
len +=sizeof(data->temp);
//humi
memcpy(buf+len,&data->humi,sizeof(data->humi));
len +=sizeof(data->humi);
//vol
memcpy(buf+len,&data->vol,sizeof(data->vol));
len +=sizeof(data->vol);
//current
memcpy(buf+len,&data->current,sizeof(data->current));
len +=sizeof(data->current);
//light
memcpy(buf+len,&data->light,sizeof(data->light));
len +=sizeof(data->light);
data->buflen = len;
}
void *th(void *arg)
{
DATA* data = (DATA*)arg;
while (1)
{
data->temp = rand() % 25 + (rand() % 100) / 100.0;
data->humi= rand() % 100;
data->vol= (rand() % 5) / 10.0 + 220;
data->current = (rand() % 5) / 10.0 + 5;
data->light= rand() % 100 + 400 + (rand() % 100) / 100.0;
setvalue(data,data->buf);
printf("temp:%f humi:%d vol:%f current:%f light:%f\n",data->temp,data->humi,data->vol,data->current,data->light);
usleep(1000 * 300); // 0.3s
}
}
typedef struct
{
int sockfd;
DATA *data;
}TH_ARG;
void* th1(void* arg)
{
pthread_detach(pthread_self());
TH_ARG* tmp = (TH_ARG*)arg;
int sockfd = tmp->sockfd;
DATA* data = tmp->data;
while(1)
{
char buf[1024] = {0};
int ret = recv(sockfd, buf, sizeof(buf), 0);
parsebuf((unsigned char *)buf, ret, sockfd,data);
}
}
int main(int argc, char **argv)
{
int listfd = socket(AF_INET,SOCK_STREAM,0 );
if(-1 ==listfd)
{
perror("socket");
exit(1);
}
struct sockaddr_in ser,cli;
bzero(&ser,sizeof(ser));
bzero(&cli,sizeof(cli));
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr =inet_addr("127.0.0.1");
//man 7 socket
int on = 1;
//除去 ip port 重新bind cd;
setsockopt(listfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
setsockopt(listfd,SOL_SOCKET,SO_REUSEPORT,&on,sizeof(on));
int ret = bind(listfd,(SA)&ser,sizeof(ser));
if(-1 ==ret)
{
perror("bind");
exit(1);
}
//建立连接的排队数
listen(listfd,3);
socklen_t len = sizeof(cli);
pthread_t tid1;
DATA data;
pthread_create(&tid1,NULL,th, &data);
while(1)
{
//通讯套接字
int conn = accept(listfd,(SA)&cli,&len);
if(-1 == conn)
{
perror("accept");
//exit(1);
continue;
}
TH_ARG args;
args.data =&data;
args.sockfd =conn;
pthread_t tid;
pthread_create(&tid,NULL,th1,&args);
//sem_wait();
// join();
// 确保th中,把conn保存到局部变量中
usleep(1000*5);
}
close(listfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr *(SA);
unsigned char GetCheckSum(unsigned short sum) {
unsigned char checksum = (256 - (sum % 256)) % 256;
return checksum;
}
unsigned short GetBufSum(unsigned char *buf, int len) {
unsigned short sum = 0;
int i = 0;
for (i = 0; i < len; i++) {
sum += buf[i];
}
return sum;
}
typedef struct {
float temp;
int humi;
float vol;
float current;
float light;
} DATA;
typedef union
{
unsigned char buf[4];
float val_fal;
int val_int;
}CONVERT;
void getfloat(char* buf,float* val)
{
CONVERT con;
con.buf[0]= buf[0];
con.buf[1]= buf[1];
con.buf[2]= buf[2];
con.buf[3]= buf[3];
*val = con.val_fal;
}
void getint(char* buf,int* val)
{
CONVERT con;
con.buf[0]= buf[0];
con.buf[1]= buf[1];
con.buf[2]= buf[2];
con.buf[3]= buf[3];
*val = con.val_int;
}
void parsebuf(unsigned char* buf,int buflen, int start_addr,int num)
{
if(0xaa == buf[0] && 0xbb == buf[buflen-1])
{
unsigned short sum = GetBufSum(buf+1,buflen-3);
unsigned char check = GetCheckSum(sum);
if(check == buf[buflen-2])
{
//printf("ok\n");
char result[50]={0};
memcpy(result+start_addr*4,buf+3,num*4);
DATA data;
int pos = 0 ;
memcpy(&data.temp,result+pos,4);
pos+=4;
memcpy(&data.humi,result+pos,4);
pos+=4;
memcpy(&data.vol,result+pos,4);
pos+=4;
memcpy(&data.current,result+pos,4);
pos+=4;
memcpy(&data.light,result+pos,4);
printf("tmp:%f humi:%d vol:%f current:%f light:%f\n",data.temp,data.humi,data.vol,data.current,data.light);
}
else
{
printf("check sum error\n");
return ;
}
}
else
{
printf("aa bb error\n");
return ;
}
}
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("sockfd");
return 1;
}
struct sockaddr_in ser;
bzero(&ser, sizeof(ser));
ser.sin_family = AF_INET;
//需要转大端 host to net short
ser.sin_port = htons(50000);
//把点分十进制ip 转未大端的uint
ser.sin_addr.s_addr = inet_addr("127.0.0.1"); // atoi
int ret = connect(sockfd,(SA)&ser,sizeof(ser));
if(-1 == ret)
{
perror("connect");
exit(1);
}
while (1) { // start r start addr num check end flag
// 0xAA 0x00 0x0 0x02 ab 0xBB
char buf[50] = {0xaa, 0x0, 0x0, 0x05, 0x00, 0xbb};
unsigned short sum = GetBufSum((unsigned char *)buf + 1, 3);
unsigned char check = GetCheckSum(sum);
buf[4] = check; // check sum
send(sockfd, buf, 6, 0);
char recvbuf[50]={0};
int ret = recv(sockfd, recvbuf, sizeof(recvbuf), 0);
int i = 0;
// for (i = 0; i < ret; i++) {
// printf("%02x ", (unsigned char )buf[i]);
// }
// printf("\n");
parsebuf((unsigned char* )recvbuf,ret, buf[2],buf[3]);
sleep(1);
}
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(80);
ser.sin_addr.s_addr = inet_addr("103.205.5.228");
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
char *http_cmd[7] = {NULL};
http_cmd[0] = "GET "
"/?app=weather.today&cityNm=成都&appkey=10003&sign="
"b59bc3ef6191eb9f747dd4e83c99f2a4&format=json HTTP/1.1\r\n";
http_cmd[1] =
"Accept: "
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/"
"webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7\r\n";
http_cmd[2] = "Accept-Encoding: gzip, deflate\r\n";
http_cmd[3] = "Accept-Language: zh-CN,zh;q=0.9\r\n";
http_cmd[4] = "Connection: keep-alive\r\n";
http_cmd[5] = "Host: api.k780.com\r\n";
http_cmd[6] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 "
"Safari/537.36\r\n\r\n";
int i = 0 ;
for(i=0;i<7;i++)
{
send(sockfd,http_cmd[i],strlen(http_cmd[i]),0);
}
char buf[1024]={0};
recv(sockfd,buf,sizeof(buf),0);
char *days=NULL;
char* week=NULL;
char * citynm=NULL;
char* temp = NULL;
char * wea = NULL;
days = strstr(buf,"days");
if(NULL == days)
{
return 1;
}
week = strstr(days,"week");
citynm = strstr(week,"citynm");
temp = strstr(citynm,"temperature");
wea = strstr(temp,"weather");
char * end=NULL;
days+=7;
end = strchr(days,'"');
*end = '\0';
week+=7;
end = strchr(week,'"');
*end = '\0';
citynm+=9;
end = strchr(citynm,'"');
*end = '\0';
temp+=14;
end = strchr(temp,'"');
*end = '\0';
wea+=10;
end = strchr(wea,'"');
*end = '\0';
printf("%s-%s-%s-%s-%s\n",days,week,citynm,temp,wea);
close(sockfd);
return 0;
}
管道的读写特点和管道设置为非阻塞
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
while (1) {
char buf[128] = {0};
read(fd, buf, sizeof(buf));
printf("fifo:%s\n", buf);
fgets(buf, sizeof(buf), stdin);
printf("buf :%s", buf);
fflush(stdout);
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
//设置管道文件 为非阻塞模式
int flag = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
flag = fcntl(0, F_GETFL);
fcntl(fileno(stdin), F_SETFL, flag | O_NONBLOCK);
while (1) {
char buf[128] = {0};
if (read(fd, buf, sizeof(buf)) > 0) {
printf("fifo:%s\n", buf);
}
if (fgets(buf, sizeof(buf), stdin)) {
printf("buf :%s", buf);
fflush(stdout);
}
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
int fd;
void handle(int num)
{
char buf[256]={0};
read(fd, buf, sizeof(buf));
printf("fifo:%s\n", buf);
}
int main(int argc, char *argv[])
{
signal(SIGIO,handle);
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
// 把管道 设置为异步通讯 如果写端,写管道,就发信号通知
int flag =fcntl(fd, F_GETFL);
fcntl(fd,F_SETFL,flag|O_ASYNC);
// 管道写端写入数据就会发信号(SIGIO) ,设置信号接受者
fcntl(fd,F_SETOWN,getpid());
while (1)
{
char buf[128] = {0};
fgets(buf, sizeof(buf), stdin);
printf("buf :%s", buf);
fflush(stdout);
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
/* According to POSIX.1-2001, POSIX.1-2008 */
#include
/* According to earlier standards */
#include
#include
#include
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
//1 create set
fd_set rd_set,tmp_set;
FD_ZERO(&rd_set);
FD_ZERO(&tmp_set);
//2 add fd
FD_SET(0,&tmp_set);
FD_SET(fd,&tmp_set);
while (1)
{
//5 clean flag
rd_set = tmp_set;
//3 wait event
select(fd+1,&rd_set,NULL,NULL,NULL);
char buf[128] = {0};
//4 find fd
if(FD_ISSET(fd, &rd_set))
{
read(fd, buf, sizeof(buf));
printf("fifo:%s\n", buf);
}
if(FD_ISSET(0, &rd_set))
{
fgets(buf, sizeof(buf), stdin);
printf("buf :%s", buf);
fflush(stdout);
}
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
int add_fd(int epfd, int fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int ret = 0;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (-1 == ret) {
perror("add fd");
}
return ret;
}
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
// 1 create set
int epfd = epoll_create(2);
if (-1 == epfd) {
perror("epfd");
exit(1);
}
struct epoll_event rev[2];
// 2 add fd
add_fd(epfd, 0);
add_fd(epfd, fd);
while (1) {
// wait event
int ep_ret = epoll_wait(epfd, rev, 2, -1);
int i = 0;
for (i = 0; i < ep_ret; i++)
{
char buf[128] = {0};
if (rev[i].data.fd == fd) {
read(fd, buf, sizeof(buf));
printf("fifo:%s\n", buf);
}
if (rev[i].data.fd == 0) {
fgets(buf, sizeof(buf), stdin);
printf("buf :%s", buf);
fflush(stdout);
}
}
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
int add_fd(int epfd, int fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int ret = 0;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (-1 == ret) {
perror("add fd");
}
return ret;
}
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo", O_RDONLY);
if (-1 == fd) {
perror("open");
return 1;
}
// 1 create set
int epfd = epoll_create(2);
if (-1 == epfd) {
perror("epfd");
exit(1);
}
struct epoll_event rev[2];
// 2 add fd
add_fd(epfd, 0);
add_fd(epfd, fd);
while (1)
{
// wait event
int ep_ret = epoll_wait(epfd, rev, 2, 2000);
if (0 == ep_ret)
{
printf("time out\n");
}
else if (ep_ret > 0)
{
int i = 0;
for (i = 0; i < ep_ret; i++)
{
char buf[128] = {0};
if (rev[i].data.fd == fd)
{
read(fd, buf, sizeof(buf));
printf("fifo:%s\n", buf);
}
if (rev[i].data.fd == 0)
{
fgets(buf, sizeof(buf), stdin);
printf("buf :%s", buf);
fflush(stdout);
}
}
}
}
close(fd);
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int ret = mkfifo("myfifo",0666);
if(-1 == ret)
{
if(EEXIST==errno )
{
}
else
{
perror("mkfifo");
return 1;
}
}
int fd = open("myfifo",O_WRONLY);
if(-1 == fd)
{
perror("open");
return 1;
}
while(1)
{
char buf[]="hello ,this is fifo test";
write(fd,buf,strlen(buf));
sleep(3);
}
close(fd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
/* According to POSIX.1-2001, POSIX.1-2008 */
#include
/* According to earlier standards */
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
//监听套接子
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
// man 7 ip
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3); // 建立链接的排队数
socklen_t len = sizeof(cli);
// 1 create set
fd_set rd_set, tmp_set;
FD_ZERO(&rd_set);
FD_ZERO(&tmp_set);
// 2 add fd
FD_SET(listfd, &tmp_set);
int maxfd = listfd;
while (1) {
// 5 clean flag
rd_set = tmp_set;
// 3 wait select
select(maxfd + 1, &rd_set, NULL, NULL, NULL);
int i = 0;
char buf[2048] = {0};
for (i = 0; i < maxfd + 1; i++)
{
if(FD_ISSET(i, &rd_set) && i == listfd) //判断是否是三次握手
{
//通信套接子
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
continue;
}
FD_SET(conn, &tmp_set);
if(conn>maxfd)
{
maxfd =conn;
}
}
if(FD_ISSET(i, &rd_set) && i != listfd)
{
int conn = i ;
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
printf("cli offline\n");
FD_CLR(conn,&tmp_set);
close(conn);
continue;
}
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
send(conn, buf, strlen(buf), 0);
}
}
}
close(listfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
int i = 10;
while (i) {
char buf[2048] = "hello,this tcp test";
send(sockfd, buf, strlen(buf), 0);
bzero(buf, sizeof(buf));
recv(sockfd, buf, sizeof(buf), 0);
printf("%s", buf);
fflush(stdout);
sleep(1);
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
/* According to POSIX.1-2001, POSIX.1-2008 */
#include
/* According to earlier standards */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int add_fd(int epfd, int fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int ret = 0;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (-1 == ret) {
perror("add fd");
}
return ret;
}
int del_fd(int epfd, int fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int ret = 0;
ret = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);
if (-1 == ret) {
perror("add fd");
}
return ret;
}
int main(int argc, char **argv) {
//监听套接子
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
// man 7 ip
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3); // 建立链接的排队数
socklen_t len = sizeof(cli);
struct epoll_event rev[5];
int epfd = epoll_create(10);
if(-1 == epfd)
{
perror("epoll_create");
return 1;
}
add_fd(epfd,listfd);
while (1) {
int ep_ret = epoll_wait(epfd,rev,5,-1);
int i = 0;
char buf[2048] = {0};
for (i = 0; i < ep_ret; i++)
{
if( rev[i].data.fd== listfd) //判断是否是三次握手
{
//通信套接子
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
continue;
}
add_fd(epfd,conn);
}
else
{
int conn = rev[i].data.fd ;
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
printf("cli offline\n");
del_fd(epfd,conn);
close(conn);
continue;
}
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
send(conn, buf, strlen(buf), 0);
}
}
}
close(listfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
int i = 10;
while (i) {
char buf[2048] = "hello,this tcp test";
send(sockfd, buf, strlen(buf), 0);
bzero(buf, sizeof(buf));
recv(sockfd, buf, sizeof(buf), 0);
printf("%s", buf);
fflush(stdout);
sleep(1);
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* According to POSIX.1-2001, POSIX.1-2008 */
#include
/* According to earlier standards */
#include
#include
#include
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo1", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo1");
return 1;
}
}
ret = mkfifo("myfifo2", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo2");
return 1;
}
}
int fd_r = open("myfifo1", O_RDONLY);
if (-1 == fd_r) {
perror("open");
return 1;
}
int fd_w = open("myfifo2", O_WRONLY);
if (-1 == fd_r) {
perror("open");
return 1;
}
fd_set rd_set, tmp_set;
FD_ZERO(&rd_set);
FD_ZERO(&tmp_set);
FD_SET(0, &tmp_set);
FD_SET(fd_r, &tmp_set);
while (1) {
rd_set = tmp_set;
select(1024, &rd_set, NULL, NULL, NULL);
if (FD_ISSET(0, &rd_set)) {
char buf[256] = {0};
printf("to B");
fgets(buf, sizeof(buf), stdin);
write(fd_w, buf, strlen(buf));
} else if (FD_ISSET(fd_r, &rd_set)) {
char buf[256] = {0};
read(fd_r, buf, sizeof(buf));
printf("from b: %s", buf);
fflush(stdout);
}
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int add_fd(int epfd, int fd) {
struct epoll_event ev;
ev.events = EPOLLIN;
ev.data.fd = fd;
int ret = 0;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
if (-1 == ret) {
perror("add fd");
}
return ret;
}
int main(int argc, char *argv[]) {
int ret = mkfifo("myfifo1", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo1");
return 1;
}
}
ret = mkfifo("myfifo2", 0666);
if (-1 == ret) {
if (EEXIST == errno) {
} else {
perror("mkfifo2");
return 1;
}
}
int fd_w = open("myfifo1", O_WRONLY);
if (-1 == fd_w) {
perror("open");
return 1;
}
int fd_r = open("myfifo2", O_RDONLY);
if (-1 == fd_r) {
perror("open");
return 1;
}
struct epoll_event rev[2];
int epfd = epoll_create(2);
if (-1 == epfd) {
perror("epoll_create");
return 1;
}
add_fd(epfd, 0);
add_fd(epfd, fd_r);
while (1) {
int ep_ret = epoll_wait(epfd, rev, 2, -1);
int i = 0;
for (i = 0; i < ep_ret; i++) {
if (0 == rev[i].data.fd)
{
char buf[256] = {0};
printf("to a");
fgets(buf, sizeof(buf), stdin);
write(fd_w, buf, strlen(buf));
if (0 == strcmp(buf, "#quit\n")) {
exit(1);
}
} else {
char buf[256] = {0};
int ret = read(fd_r, buf, sizeof(buf));
if (ret <= 0) {
// break;
exit(1);
}
if (0 == strcmp(buf, "#quit\n")) {
exit(1);
}
printf("from a: %s", buf);
fflush(stdout);
}
}
}
// remove("myfifo");
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
void handle(int num)
{
wait(NULL);
}
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
signal(SIGCHLD,handle);
//监听套接子
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
// man 7 ip
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3); // 建立链接的排队数
socklen_t len = sizeof(cli);
while (1)
{
//通信套接子
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
continue;
}
pid_t pid = fork();
if(pid>0)
{
close(conn);
}
else if (0 == pid)
{
close(listfd);
while(1)
{
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
exit(1);
}
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
send(conn, buf, strlen(buf), 0);
}
}
else
{
perror("fork");
return 1;
}
}
close(listfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
int i = 10;
while (i) {
char buf[2048] = "hello,this tcp test";
send(sockfd, buf, strlen(buf), 0);
bzero(buf, sizeof(buf));
recv(sockfd, buf, sizeof(buf), 0);
printf("%s", buf);
fflush(stdout);
sleep(1);
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
typedef struct sockaddr *(SA);
void * th(void* arg)
{
pthread_detach(pthread_self());
int conn = *(int*)arg;
while(1)
{
char buf[2048] = {0};
int rd_ret = recv(conn, buf, sizeof(buf), 0);
if (rd_ret <= 0)
{
close(conn);
printf("client offline\n");
break;
}
time_t tm;
time(&tm);
sprintf(buf, "%s %s", buf, ctime(&tm));
send(conn, buf, strlen(buf), 0);
}
return NULL;
}
int main(int argc, char **argv) {
//监听套接子
int listfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == listfd) {
perror("socket");
exit(1);
}
// man 7 ip
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = bind(listfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
return 1;
}
listen(listfd, 3); // 建立链接的排队数
socklen_t len = sizeof(cli);
while (1)
{
//通信套接子
int conn = accept(listfd, (SA)&cli, &len);
if (-1 == conn)
{
perror("accept");
return 1;
}
pthread_t tid;
pthread_create(&tid,NULL,th,&conn );
//pthread_join();
}
close(listfd);
return 0;
}
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
#include
typedef struct sockaddr *(SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd) {
perror("socket");
return 1;
}
struct sockaddr_in ser;
ser.sin_family = AF_INET;
ser.sin_port = htons(50000);
ser.sin_addr.s_addr = INADDR_ANY;
int ret = connect(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("connect");
return 1;
}
int i = 10;
while (i) {
char buf[2048] = "hello,this tcp test";
send(sockfd, buf, strlen(buf), 0);
bzero(buf, sizeof(buf));
recv(sockfd, buf, sizeof(buf), 0);
printf("%s", buf);
fflush(stdout);
sleep(1);
}
close(sockfd);
return 0;
}
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
typedef struct sockaddr* (SA);
int main(int argc, char **argv) {
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
struct sockaddr_in ser, cli;
ser.sin_family = AF_INET;
ser.sin_port = htons(9999);
ser.sin_addr.s_addr =0;
int ret = bind(sockfd, (SA)&ser, sizeof(ser));
if (-1 == ret) {
perror("bind");
exit(1);
}
socklen_t len = sizeof(cli);
while(1)
{
char buf[512]={0};
recvfrom(sockfd,buf,sizeof(buf),0,(SA)&cli,&len);
printf("%s\n",buf);
}
return 0;
}
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
typedef struct sockaddr* (SA);
int main(int argc, char **argv)
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("socket");
exit(1);
}
//man 7 socket 208
socklen_t on =1;
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
struct sockaddr_in all;
all.sin_family = AF_INET;
all.sin_port = htons(9999);
all.sin_addr.s_addr = inet_addr("192.168.0.255");
while(1)
{
char buf[]="this is udp boardcast test...";
sendto(sockfd,buf,strlen(buf),0,(SA)&all,sizeof(all));
sleep(1);
}
return 0;
}
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
#include
#include
typedef struct sockaddr* (SA);
#define MUTIL_ADDR "235.1.2.3"
int main(int argc, char **argv)
{
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(-1 == sockfd)
{
perror("socket");
exit(1);
}
struct sockaddr_in cli,sendaddr;
cli.sin_family = AF_INET;
cli.sin_port = htons(9999);
cli.sin_addr.s_addr =inet_addr(MUTIL_ADDR);
char sendaddrbuf[64]={0};
socklen_t len = sizeof(cli);
while(1)
{
char buf[512]="this is udp mulitcast test";
//send mutil cast
sendto(sockfd,buf,strlen(buf),0,(SA)&cli,sizeof(cli));
bzero(buf,sizeof(buf));
recvfrom(sockfd,buf,sizeof(buf),0,(SA)&sendaddr,&len);
printf("%s:%d %s\n",inet_ntop(AF_INET,&sendaddr.sin_addr,sendaddrbuf,sizeof(sendaddrbuf)),ntohs(sendaddr.sin_port),buf);
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include /* See NOTES */
#include
typedef struct sockaddr *(SA);
#define MUTIL_ADDR "235.1.2.3"
int main(int argc, char **argv) {
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd) {
perror("socket");
exit(1);
}
//本机 地址
struct sockaddr_in local, sendaddr;
local.sin_family = AF_INET;
local.sin_port = htons(9999);
// ip is 0
local.sin_addr.s_addr = INADDR_ANY;
int ret = bind(sockfd,(SA)&local,sizeof(local));
if(-1 ==ret)
{
perror("bind");
exit(1);
}
struct ip_mreqn multiaddr;
multiaddr.imr_multiaddr.s_addr = inet_addr(MUTIL_ADDR);
multiaddr.imr_address.s_addr = INADDR_ANY;
// 0 to indicate any interface.
multiaddr.imr_ifindex = 0;
//把自己的地址加入组播地址
//man 7 ip 89
setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &multiaddr,sizeof(multiaddr));
char sendaddrbuf[64] = {0};
socklen_t len = sizeof(sendaddr);
while (1) {
char buf[512] = {0};
recvfrom(sockfd, buf, sizeof(buf), 0, (SA)&sendaddr, &len);
printf("%s:%d %s\n",
inet_ntop(AF_INET, &sendaddr.sin_addr, sendaddrbuf,
sizeof(sendaddrbuf)),
ntohs(sendaddr.sin_port), buf);
sprintf(buf, "%s %s", buf, "aaa");
// send mutil cast
sendto(sockfd, buf, strlen(buf), 0, (SA)&sendaddr, sizeof(sendaddr));
sleep(1);
}
return 0;
}