sigaction.c
#include <signal.h> //sigaction
#include <stdio.h>
#include <string.h>
void handle(int sig)
{
if (SIGINT == sig)
{
printf("catched sigint\n");
}
while (1)
{
}
}
int main(void)
{
struct sigaction sigact;
sigact.sa_handler = handle; //设置信号的处理动作
sigemptyset(&(sigact.sa_mask));
//设置在信号处理动作过程中需要屏蔽的信号集
sigaddset(&(sigact.sa_mask), SIGQUIT);
//sigact.sa_flags = 0;
//SA_NODEFER:在处理信号过程中不屏蔽本身信号
//SA_RESETHAND:信号处理后回复信号之前的处理动作
sigact.sa_flags = SA_NODEFER;
struct sigaction oldact;
//指定sigact来处理信号SIGINT
//使用oldact来保留SIGINT之前的处理动作
sigaction(SIGINT, &sigact, &oldact);
while (1)
{}
// sigaction(SIGINT, &oldact, NULL);
//sigsuspend(); //wait for a signal
return 0;
}
运行结果:
1.PNG
sigprocmask.c
#include <signal.h> //sigprocmask
#include <stdio.h>
#include <string.h>
void handle(int sig)
{
if (SIGINT == sig)
{
printf("catched sigint\n");
}
}
int main(void)
{
signal(SIGINT, handle);
sigset_t set;
sigemptyset(&set); //清空信号集
sigaddset(&set, SIGINT); //将信号添加到信号集中
//判断信号集中是否包含了某个信号
if (1 == sigismember(&set, SIGINT))
{
printf("信号添加到信号集中成功\n");
}
sigset_t oldset;
int ret = -1;
//第一个参数:设置信号集set的处理方式
// SIG_BLOCK: 阻塞信号集set中的信号
// SIG_UNBLOCK:将信号集set中的信号设置为非阻塞
// SIG_SETMASK:使用信号集set中的信号值来设置
// 系统中相对应的信号的值
//第二个参数:需要特殊处理的信号集
//第三个参数:若不为NULL,则用来保留set集合中信号的之前的值
// 若为NULL,则不保留
ret = sigprocmask(SIG_BLOCK, &set, &oldset);
if (-1 == ret)
{
printf("sigprocmask error\n");
}
int i = 0;
while (1)
{
printf("this is main while...\n");
sleep(1);
i++;
if (5 == i)
{
break;
}
}
sigprocmask(SIG_SETMASK, &oldset, NULL);
while (1)
{}
return 0;
}
运行结果:
2.PNG
服务器端server.c
/*socket()*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> //struct sockaddr_in
#include <stdio.h>
#include <string.h>
//#include "link.h"
int main(void)
{
#if 0
List *pList = makeList();
if (NULL == pList)
{
return -1;
}
int iBaseId = 1001;
#endif
int sockfd = -1;
//创建socket描述符,用于监听接受客户端的连接
//AF_INET:ipv4
//SOCK_STREAM:tcp协议
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
return -1;
}
int ret = -1;
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET; //ipv4
serverAddr.sin_port = htons(8888); //port
//server ip
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(serverAddr.sin_zero), 8);
//将sockfd和地址进行绑定
ret = bind(sockfd, (struct sockaddr *)&serverAddr
, sizeof(serverAddr));
if (-1 == ret)
{
perror("bind");
return -1;
}
//监听客户端的连接
//sockfd:监听的socket描述符
//5:表示未经过处理的连接请求队列可以容纳的最大数目
ret = listen(sockfd, 5);
if (-1 == ret)
{
perror("listen");
return -1;
}
struct sockaddr_in clientAddr;
int iLen = sizeof(clientAddr);
int clientSockfd = -1;
// Node *pNode = NULL;
while (1)
{
//阻塞等待客户端的连接,直到有客户端连接
//成功返回socket描述符,
//该socket描述符用于服务器与客户端进行通信
//失败返回-1
//sockfd:监听的socket描述符
//clientAddr:用于保存客户端的地址等信息
//iLen:地址长度变量指针
clientSockfd = accept(sockfd
, (struct sockaddr *)&clientAddr
, &iLen);
if (-1 == clientSockfd)
{
perror("accept");
return -1;
}
printf("there is a client connected...\n");
#if 0
pNode = makeNode();
if (NULL == pNode)
{
continue;
}
pNode->iId = iBaseId;
pNode->sockfd = clientSockfd;
iBaseId++;
insertList(pList, pNode);
#endif
#if 1
//往客户端发送消息
write(clientSockfd, "Hello Client", 12);
#endif
#if 0
printf("has client connected:%s\n"
, inet_ntoa(clientAddr.sin_addr));
#endif
}
return 0;
}
客户端client.c
/*socket()*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h> //struct sockaddr_in
#include <stdio.h>
#include <string.h>
int main(void)
{
int sockfd = -1;
//创建sockfd用于和服务器通信
//AF_INET: ipv4
//SOCK_STREAM:tcp
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("socket");
return -1;
}
int ret = -1;
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET; //ipv4
serverAddr.sin_port = htons(8888); //port
//server ip
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(serverAddr.sin_zero), 8);
//连接服务器
ret = connect(sockfd, (struct sockaddr *)&serverAddr
, sizeof(serverAddr));
if (-1 == ret)
{
perror("connect");
return -1;
}
char caBuf[32] = {'\0'};
//读取服务器发的数据
read(sockfd, caBuf, sizeof(caBuf));
printf("%s\n", caBuf);
return 0;
}
运行结果:
服务器端:
服务器端.PNG
客户端:
客户端.PNG
main.c
#include "link.h"
int main()
{
List *pList =NULL;
pList =makeList();
int i=0;
int iBaseId =1001;
Node *pNode =NULL;
for(;i<5;i++)
{
pNode =makeNode();
if(NULL==pNode)
{
return -1;
}
pNode->iId=iBaseId;
insertList(pList,pNode);
iBaseId++;
}
showList(pList);
return 0;
}
link.c
#include "link.h"
#include <stdlib.h>
Node *makeNode()
{
Node *pNode = NULL;
pNode = (Node *)malloc(sizeof(Node));
if (NULL != pNode)
{
pNode->iId = 0;
pNode->sockfd = -1;
pNode->pNext = NULL;
}
return pNode;
}
List *makeList()
{
List *pList = NULL;
pList = (List *)malloc(sizeof(List));
if (NULL != pList)
{
pList->iLen = 0;
pList->pFirstNode = NULL;
}
return pList;
}
void insertList(List *pList, Node *pNode)
{
if (NULL == pList || NULL == pNode)
{
return;
}
if (NULL == pList->pFirstNode)
{
pList->pFirstNode = pNode;
}
else
{
pNode->pNext = pList->pFirstNode;
pList->pFirstNode = pNode;
}
pList->iLen++;
}
Node *findNode(const List *pList, int iId)
{
if (NULL == pList)
{
return NULL;
}
Node *pNode = pList->pFirstNode;
while (NULL != pNode)
{
if (pNode->iId == iId)
{
break;
}
pNode = pNode->pNext;
}
return pNode;
}
void showList(const List *pList)
{
if (NULL == pList)
{
return;
}
Node *pNode = pList->pFirstNode;
while (NULL != pNode)
{
printf("id: %d, sockfd: %d\n", pNode->iId
, pNode->sockfd);
pNode = pNode->pNext;
}
}
link.h
#ifndef LINK_H
#define LINK_H
#include <stdio.h>
typedef struct Node
{
int iId;
int sockfd;
struct Node *pNext;
}Node;
typedef struct List
{
int iLen;
Node *pFirstNode;
}List;
Node *makeNode();
List *makeList();
void insertList(List *pList, Node *pNode);
Node *findNode(const List *pList, int iId);
void showList(const List *pList);
#endif
运行结果:
link.PNG
网友评论