========================//write//==============================
#include "head.h"
int main(void)
{
int fd = 0;
char tmpbuff[4096] = {0};
mkfifo("/tmp/myfifo", 0664);
fd = open("/tmp/myfifo", O_WRONLY);
if (-1 == fd)
{
perror("fail to open");
return -1;
}
while (1)
{
gets(tmpbuff);
write(fd, tmpbuff, strlen(tmpbuff));
}
close(fd);
return 0;
}
==============================================================
=================//read//=================
#include "head.h"
//异步IO
int fd = 0;
void handler(int signo)
{
char tmpbuff[4096] = {0};
ssize_t nsize = 0;
memset(tmpbuff, 0, sizeof(tmpbuff));
nsize = read(fd, tmpbuff, sizeof(tmpbuff));
if (nsize > 0)
{
printf("FIFO:%s\n", tmpbuff);
}
return;
}
int main(void)
{
int flags = 0;
char *pret = NULL;
ssize_t nsize = 0;
char tmpbuff[4096] = {0};
signal(SIGIO, handler);
mkfifo("/tmp/myfifo", 0664);
fd = open("/tmp/myfifo", O_RDONLY);
if (-1 == fd)
{
perror("fail to open");
return -1;
}
flags = fcntl(fd, F_GETFL);
flags |= O_ASYNC; // 文件状态异步IO
fcntl(fd, F_SETFL, flags);
fcntl(fd, F_SETOWN, getpid()); //F_SETOWN : 发送给谁
while (1)
{
memset(tmpbuff, 0, sizeof(tmpbuff));
gets(tmpbuff);
printf("STDIN:%s\n", tmpbuff);
}
close(fd);
}
===========================================================================
POLL-----TCP
==============================用户端client.c============================
#include "head.h"
int CreateTcpClient(char *pip, int port)
{
int ret = 0;
int sockfd = 0;
struct sockaddr_in seraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(port);
seraddr.sin_addr.s_addr = inet_addr(pip);
ret = connect(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (-1 == ret)
{
perror("fail to connect");
return -1;
}
return sockfd;
}
int main(void)
{
int sockfd = 0;
char tmpbuff[4096] = {"hello world"};
int cnt = 0;
ssize_t nsize = 0;
sockfd = CreateTcpClient("192.168.1.135", 50000);
while (1)
{
memset(tmpbuff, 0, sizeof(tmpbuff));
sprintf(tmpbuff, "hello world --- %d", cnt);
cnt++;
nsize = send(sockfd, tmpbuff, strlen(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to send");
return -1;
}
memset(tmpbuff, 0, sizeof(tmpbuff));
nsize = recv(sockfd, tmpbuff, sizeof(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to recv");
return -1;
}
printf("RECV:%s\n", tmpbuff);
}
close(sockfd);
return 0;
}
===========================//server.c//===============================
#include "head.h"
int CreateListenSocket(char *pip, int port)
{
int ret = 0;
int sockfd = 0;
struct sockaddr_in seraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(port);
seraddr.sin_addr.s_addr = inet_addr(pip);
ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (-1 == ret)
{
perror("fail to bind");
return -1;
}
ret = listen(sockfd, 10);
if (-1 == ret)
{
perror("fail to listen");
return -1;
}
return sockfd;
}
int HandleTcpClient(int confd)
{
char tmpbuff[4096] = {0};
ssize_t nsize = 0;
memset(tmpbuff, 0, sizeof(tmpbuff));
nsize = recv(confd, tmpbuff, sizeof(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to recv");
return -1;
}
else if (0 == nsize)
{
return 0;
}
sprintf(tmpbuff, "%s ----echo", tmpbuff);
nsize = send(confd, tmpbuff, strlen(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to send");
return -1;
}
return nsize;
}
int InitFds(struct pollfd *fds, int maxlen)
{
int i = 0;
for (i = 0; i < maxlen; i++)
{
fds[i].fd = -1;
}
return 0;
}
int AddFd(struct pollfd *fds, int maxlen, int fd, short env)
{
int i = 0;
for (i = 0; i < maxlen; i++)
{
if (fds[i].fd == -1)
{
fds[i].fd = fd;
fds[i].events = env;
break;
}
}
if (i == maxlen)
{
return -1;
}
return 0;
}
int DeleteFd(struct pollfd *fds, int maxlen, int fd)
{
int i = 0;
for (i = 0; i < maxlen; i++)
{
if (fds[i].fd == fd)
{
fds[i].fd = -1;
break;
}
}
return 0;
}
int main(void)
{
int sockfd = 0;
int confd = 0;
struct pollfd fds[1024];
int nready = 0;
int i = 0;
int ret = 0;
sockfd = CreateListenSocket("192.168.1.135", 50000);
InitFds(fds, 1024);
AddFd(fds, 1024, sockfd, POLLIN);
while (1)
{
nready = poll(fds, 1024, -1);
if (-1 == nready)
{
perror("fail to poll");
return -1;
}
for (i = 0; i < 1024; i++)
{
if (fds[i].fd == -1)
{
continue;
}
if (fds[i].revents & POLLIN && fds[i].fd == sockfd)
{
confd = accept(sockfd, NULL, NULL);
if (-1 == confd)
{
perror("fail to accept");
DeleteFd(fds, 1024, sockfd);
close(sockfd);
continue;
}
AddFd(fds, 1024, confd, POLLIN);
}
else if (fds[i].revents & POLLIN && fds[i].fd != sockfd)
{
ret = HandleTcpClient(fds[i].fd);
if (-1 == ret)
{
fprintf(stderr, "handle tcp client failed!\n");
close(fds[i].fd);
DeleteFd(fds, 1024, fds[i].fd);
continue;
}
else if (0 == ret)
{
fprintf(stderr, "client disconnected!\n");
close(fds[i].fd);
DeleteFd(fds, 1024, fds[i].fd);
continue;
}
}
}
}
close(sockfd);
return 0;
}
select--TCP
======================//用户端client.c//================================
#include "head.h"
int CreateTcpClient(char *pip, int port)
{
int ret = 0;
int sockfd = 0;
struct sockaddr_in seraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(port);
seraddr.sin_addr.s_addr = inet_addr(pip);
ret = connect(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (-1 == ret)
{
perror("fail to connect");
return -1;
}
return sockfd;
}
int main(void)
{
int sockfd = 0;
char tmpbuff[4096] = {"hello world"};
int cnt = 0;
ssize_t nsize = 0;
sockfd = CreateTcpClient("192.168.1.135", 50000);
while (1)
{
memset(tmpbuff, 0, sizeof(tmpbuff));
sprintf(tmpbuff, "hello world --- %d", cnt);
cnt++;
nsize = send(sockfd, tmpbuff, strlen(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to send");
return -1;
}
memset(tmpbuff, 0, sizeof(tmpbuff));
nsize = recv(sockfd, tmpbuff, sizeof(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to recv");
return -1;
}
printf("RECV:%s\n", tmpbuff);
}
close(sockfd);
return 0;
}
============================//server.c 服务端//============================
#include "head.h"
int CreateListenSocket(char *pip, int port) // 创建
{
int ret = 0;
int sockfd = 0;
struct sockaddr_in seraddr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror("fail to socket");
return -1;
}
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(port);
seraddr.sin_addr.s_addr = inet_addr(pip);
ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
if (-1 == ret)
{
perror("fail to bind");
return -1;
}
ret = listen(sockfd, 10);
if (-1 == ret)
{
perror("fail to listen");
return -1;
}
return sockfd;
}
int HandleTcpClient(int confd) //接受TCP用户端
{
char tmpbuff[4096] = {0};
ssize_t nsize = 0;
memset(tmpbuff, 0, sizeof(tmpbuff));
nsize = recv(confd, tmpbuff, sizeof(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to recv");
return -1;
}
else if (0 == nsize)
{
return 0;
}
sprintf(tmpbuff, "%s ----echo", tmpbuff);
nsize = send(confd, tmpbuff, strlen(tmpbuff), 0);
if (-1 == nsize)
{
perror("fail to send");
return -1;
}
return nsize;
}
int main(void)
{
int sockfd = 0;
int confd = 0;
fd_set rdfds;
fd_set tmpfds;
int maxfd = 0;
int ret = 0;
int i = 0;
sockfd = CreateListenSocket("192.168.1.135", 50000);
FD_ZERO(&rdfds);
FD_SET(sockfd, &rdfds);
maxfd = sockfd;
while (1)
{
tmpfds = rdfds;
ret = select(maxfd+1, &tmpfds, NULL, NULL, NULL);
if (-1 == ret)
{
perror("fail to select");
return -1;
}
if (FD_ISSET(sockfd, &tmpfds))
{
confd = accept(sockfd, NULL, NULL);
if (-1 == confd)
{
perror("fail to accept");
FD_CLR(sockfd, &rdfds);
close(sockfd);
continue;
}
FD_SET(confd, &rdfds);
maxfd = maxfd > confd ? maxfd : confd;
}
for (i = sockfd+1; i <= maxfd; i++)
{
if (FD_ISSET(i, &tmpfds))
{
ret = HandleTcpClient(i);
if (-1 == ret)
{
fprintf(stderr, "handle client failed!\n");
FD_CLR(i, &rdfds);
close(i);
continue;
}
else if (0 == ret)
{
fprintf(stderr, "client disconnected!\n");
FD_CLR(i, &rdfds);
close(i);
continue;
}
}
}
}
close(confd);
close(sockfd);
return 0;
}