19
semA.c |
#include<stdio.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<stdlib.h> //exit() main() { key_t key; int semid; //1 创建信号量 key = ftok(".", 99); if(key == -1) { printf("ftok error: %m\n"), exit(-1); } semid = semget(key, 1, //信号量数组个数 IPC_CREAT | IPC_EXCL | 0666); if(semid == -1) { printf("semget error: %m\n"), exit(-1); } //2 初始化信号量 //3 对信号量进行阻塞操作 //4 删除(可以不删除) } |
#include<stdio.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<stdlib.h> //exit() main() { key_t key; int semid; //1 创建信号量 key = ftok(".", 99); if(key == -1) { printf("ftok error: %m\n"), exit(-1); } semid = semget(key, 1, 0); //得到信号量 if(semid == -1) { printf("semget error: %m\n"), exit(-1); }
printf("id: %d\n", semid); //2 初始化信号量 //3 对信号量进行阻塞操作 //4 删除(可以不删除) } |
#include<stdio.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<stdlib.h> //exit() //2.1定义一个联合体 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; } main() { key_t key; int semid; //信号量ID union semun v; //2.2 定义初始化值 int r; //1 创建信号量 key = ftok(".", 99); if(key == -1) { printf("ftok error: %m\n"), exit(-1); } semid = semget(key, 1, 0); //得到信号量 if(semid == -1) { printf("semget error: %m\n"), exit(-1); }
printf("id: %d\n", semid); //2 初始化信号量 v.val = 2; r = semctl(semid, 0, SETVAL, v); //2.3设置信号量的值 if(r == -1) { printf("semctl error: %m\n"),exit(-1); } //3 对信号量进行阻塞操作 //4 删除(可以不删除) } |
#include<stdio.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<stdlib.h> //exit() //2.1定义一个联合体 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; } main() { key_t key; int semid; //信号量ID union semun v; //2.2 定义初始化值 int r; struct sembuf op[1]; //3.1 定义操作 //1 创建信号量 key = ftok(".", 99); if(key == -1) { printf("ftok error: %m\n"), exit(-1); } semid = semget(key, 1, 0); //得到信号量 if(semid == -1) { printf("semget error: %m\n"), exit(-1); }
printf("id: %d\n", semid); //2 初始化信号量 v.val = 2; r = semctl(semid, 0, SETVAL, v); //2.3设置信号量的值 if(r == -1) { printf("semctl error: %m\n"),exit(-1); } //3 对信号量进行阻塞操作 op[0].sem_num = 0; //信号量的下标 op[0].sem_op = -1; //信号量操作单位与类型 op[0].sem_flg = 0; //操作标记 while(1) { r = semop(semid, op, 1); printf("unblock\n"); //解除阻塞 } //4 删除(可以不删除) } |
#include<stdio.h> #include<unistd.h> #include<sys/sem.h> #include<sys/ipc.h> #include<stdlib.h> //exit() //2.1定义一个联合体 union semun { int val; struct semid_ds *buf; unsigned short *array; struct seminfo *__buf; } main() { key_t key; int semid; //信号量ID union semun v; //2.2 定义初始化值 int r; struct sembuf op[1]; //3.1 定义操作 //1 创建信号量 key = ftok(".", 99); if(key == -1) { printf("ftok error: %m\n"), exit(-1); } semid = semget(key, 1, 0); //得到信号量 if(semid == -1) { printf("semget error: %m\n"), exit(-1); }
printf("id: %d\n", semid); //2 初始化信号量 //3 对信号量进行阻塞操作 op[0].sem_num = 0; //信号量的下标 op[0].sem_op = +1; //信号量操作单位与类型 op[0].sem_flg = 0; //操作标记 while(1) { r = semop(semid, op, 1); sleep(1); } //4 删除(可以不删除) } |
Inet.c |
#include<stdlio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { in_addr_t nip= 192 << 24 | 168 << 16 | 1 << 8 | 66; //1 整数表示法 char *ip = "192.168.0.26"; //2 a字符串 //char cip[4] = {192.168.0,26}; //2 b字符串 //int *nip2 =(int *) cip; //把整数抓换为字符串 inet_ntoa struct in_addr sip; sip.s_addr = nip; printf("%s\n", inet_ntoa(sip));
} |
#include<stdlio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { in_addr_t nip= 192 << 24 | 168 << 16 | 1 << 8 | 66; //1 整数表示法 char *ip = "192.168.1.66"; //2 a字符串 //char cip[4] = {192.168.0,26}; //2 b字符串 //int *nip2 =(int *) cip; //把整数抓换为字符串 inet_ntoa struct in_addr sip; sip.s_addr = nip; printf("%s\n", inet_ntoa(sip));
int myip; myip = inet_addr(ip); printf("%u\n", myip); printf("%u.%u.%u.%\n" myip >> 24 & 255, myip >> 16 & 255, myip >> 8 & 255, myip >> 0 & 255);
} |
603.c |
#include<stdlio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { char *ip = "192.168.1.66"; struct in_addr addr; in_addr_t net; in_addr_t host; struct in_addr tmp;
inet_aton(ip, &addr); net = inet_lonaof(addr); host = inet_netof(addr);
tmp.s_addr = net; printf("%s\n", inet_ntoa(tmp)); tmp.s_addr = host; printf("%s\n", inet_ntoa(tmp)); } |
Root: setup |
Cd /etc/ |
Vi hosts |
Cd /etc/ |
Vi protocols |
Cat /etc/ |
Vi services |
Host1.c |
#include<stdio.h> #include<netdb.h> main() { struct hostent *ent; //1 打开主机配置数据库文件 sethostent(1); //1 一直保持连接 while(1) { ent = gethostent(); if(ent == 0) { break; } printf("host name:%s\t", ent->h_name); printf("host IP:%hhu.%hhu.%hhu.%hhu\n", ent->h_addr[0],ent->h_addr[1],ent->h_addr[2],ent->h_addr[3]); } } |
603.c |
#include<stdlio.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { char *ip = "192.168.1.66"; struct in_addr addr; in_addr_t net; in_addr_t host; struct in_addr tmp;
inet_aton(ip, &addr); net = inet_lonaof(addr); host = inet_netof(addr);
tmp.s_addr = net; printf("%s\n", inet_ntoa(tmp)); tmp.s_addr = host; printf("%s\n", inet_ntoa(tmp)); } |
Getip.c |
#include<stdio.h> #include<netdb.h>
main() { struct hostent *ent; ent = gethostbyname("baidu.com"); printf("%hhu.%hhu.%hhu.%hhu\n", ent->h_addr[0], ent->h_addr[1], ent->h_addr[2], ent->h_addr[3]); } |
#include<stdio.h> #include<netdb.h>
main() { int i; struct hostent *ent; ent = gethostbyname("baidu.com"); for(i = 0; i < 3; i ++) { printf("%hhu.%hhu.%hhu.%hhu\n", ent->h_addr_list[i][0], ent->h_addr_list[i][1], ent->h_addr_list[i][2], ent->h_addr_list[i][3]); } } |
Protocol.c |
#include<stdio.h> #include<netdb.h>
main() { struct protoent *ent; ent = getprotobyname("udp"); printf("udp: %d\n", ent->p_proto); } |
Uname1.c |
#include<stdio.h> #include<netdb.h> #include<sys/utsname.h> main() { struct utsname name; uname(&name); printf("machine: %s\n", name.machine); printf("nodename: %s\n", name.nodename); printf("sysname: %s\n", name.sysname); } |
#include<stdio.h> #include<netdb.h> #include<sys/utsname.h> main() { struct utsname name; uname(&name); printf("machine: %s\n", name.machine); printf("nodename: %s\n", name.nodename); printf("sysname: %s\n", name.sysname); printf("domainname: %s\n", name.domainname); } |
udpA.c |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<netinet/in.h>
main() { int fd; //socket描述符号 struct sockaddr_in ad; //本机的IP地址 char *buf[100]; //接收数据的缓冲 struct scokaddr_in ad_snd; //发送者的IP地址 socklen_t len; //发送者IP的长度 int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); //17:UDP协议 if(fd == -1) { printf("socket error:%m\n"), exit(-1); } printf("create socket success!\n"); ad.sin_family = AF_INET; ad.sin_port = htons(11111); inet_aton("192.168.1.66", &ad.sin_addr); r = bind(fd, (struct sockaddr*)&ad, sizeof(ad)) if(r == -1) { printf("bind error: %m\n"), exit(-1); } printf("bind success!\n);
while(1) { len = sizeof(ad_snd); r = recvfrom(fd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&ad_snd, &len); if(r > 0) { buf[r] = 0; printf("send IP: %s\n, data: %s\n", inet_ntoa(ad_snd.sin_addr), buf); } if(r == 0) { printf("send close!\n"); break; } if(r == -1) { printf("net error!\n"); break; } } close(fd); } |
udpB.c |
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<netdb.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { int fd; struct sockaddr_in ad; char buf[101]; int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); if(fd == -1) { printf("socket error: %m\n"),exit(-1); }
ad.sin_family = AF_INET; ad.sin_port = htons(11111); ad.sin_addr.s_addr = inet_addr("192.168.1.66"); while(1) { r = read(0, buf, sizeof(buf) - 1); if(r <= 0) { break; } buf[r] = 0; r = sendto(fd, buf, r, 0, (struct sockaddr*)&ad, sizeof(ad)); if(r == -1) { break; } } close(fd); } |
Ifconfig |
Export PATH=${PATH}:/sibin/ |
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<netdb.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { int fd; struct sockaddr_in ad; char buf[101]; int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); if(fd == -1) { printf("socket error: %m\n"),exit(-1); }
ad.sin_family = AF_INET; ad.sin_port = htons(11111); ad.sin_addr.s_addr = inet_addr("192.168.1.66"); connect(fd, (struct sockaddr*)&ad, sizof(ad)); //2.1 while(1) { r = read(0, buf, sizeof(buf) - 1); if(r <= 0) { break; } buf[r] = 0; // r = sendto(fd, buf, r, 0, (struct sockaddr*)&ad, sizeof(ad)); // 改写为2.1 2.2 r = send(fd, buf, r, 0); //2.2
if(r == -1) { break; } } close(fd); } |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<netinet/in.h>
main() { int fd; //socket描述符号 struct sockaddr_in ad; //本机的IP地址 char *buf[100]; //接收数据的缓冲 struct scokaddr_in ad_snd; //发送者的IP地址 socklen len; //发送者IP的长度 int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); //17:UDP协议 if(fd == -1) { printf("socket error:%m\n"), exit(-1); } printf("create socket success!\n"); ad.sin_family = AF_INET; ad.sin_port = hton(11111); inet_aton("192.168.1.66", ad.sin_addr); r = bind(fd, (struct sockaddr)&ad, sizeof(ad)) if(r == -1) { printf("bind error: %m\n"), exit(-1); } printf("bind success!\n);
while(1) { len = sizeof(ad_snd); r = recvfrom(fd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&ad_snd, &len); if(r > 0) { buf[r] = 0; printf("send IP: %s\n port: %hu data: %s\n", inet_ntoa(ad_snd.sin_addr), ntohs(ad_snd.sin_port), buf); } if(r == 0) { printf("send close!\n"); break; } if(r == -1) { printf("net error!\n"); break; } } close(fd); } |
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<string.h> #include<sys/socket.h> #include<netinet/in.h>
main() { int fd; //socket描述符号 struct sockaddr_in ad; //本机的IP地址 char *buf[100]; //接收数据的缓冲 struct scokaddr_in ad_snd; //发送者的IP地址 socklen len; //发送者IP的长度 int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); //17:UDP协议 if(fd == -1) { printf("socket error:%m\n"), exit(-1); } printf("create socket success!\n"); ad.sin_family = AF_INET; ad.sin_port = hton(11111); inet_aton("192.168.1.66", ad.sin_addr); r = bind(fd, (struct sockaddr)&ad, sizeof(ad)) if(r == -1) { printf("bind error: %m\n"), exit(-1); } printf("bind success!\n);
while(1) { len = sizeof(ad_snd); r = recvfrom(fd, buf, sizeof(buf)-1, 0, (struct sockaddr*)&ad_snd, &len); if(r > 0) { buf[r] = 0; printf("send IP: %s\n port: %hu data: %s\n", inet_ntoa(ad_snd.sin_addr), ntohs(ad_snd.sin_port), buf); sendto(fd, "auto !!!", strlen("auto !!!"), 0, (struct sockaddr*)&ad_snd, sizeof(ad_snd)); } if(r == 0) { printf("send close!\n"); break; } if(r == -1) { printf("net error!\n"); break; } } close(fd); } |
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<netdb.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h>
main() { int fd; struct sockaddr_in ad; char buf[101]; int r;
fd = socket(AF_INET, SOCK_DGRAM, 17); if(fd == -1) { printf("socket error: %m\n"),exit(-1); }
ad.sin_family = AF_INET; ad.sin_port = htons(11111); ad.sin_addr.s_addr = inet_addr("192.168.1.66"); connect(fd, (struct sockaddr*)&ad, sizof(ad)); //2.1 while(1) { r = read(0, buf, sizeof(buf) - 1); if(r <= 0) { break; } buf[r] = 0; // r = sendto(fd, buf, r, 0, (struct sockaddr*)&ad, sizeof(ad)); // 改写为2.1 2.2 r = send(fd, buf, r, 0); //2.2 bzero(buf,sizeof(buf)); r = recv(fd, buf,sizeof(buf), 0); buf[r] = 0; printf("recv data: %s\n", buf); if(r == -1) { break; } } close(fd); } |