web123456

Embedded Learning Day37--Linux Software Programming---Network Programming

  • #include "../"
  • #define MAX_POLL_FDNUM 10000
  • int CreateListenSocket(const char *pip, int port)
  • {
  • int sockfd = 0;
  • int ret = 0;
  • struct sockaddr_in seraddr;
  • sockfd = socket(AF_INET, SOCK_STREAM, 0);
  • if (-1 == sockfd)
  • {
  • return -1;
  • }
  • seraddr.sin_family = AF_INET;
  • seraddr.sin_port = htons(port);
  • seraddr.sin_addr.s_addr = INADDR_ANY;
  • ret = bind(sockfd, (struct sockaddr *)&seraddr, sizeof(seraddr));
  • if (-1 == ret)
  • {
  • return -1;
  • }
  • ret = listen(sockfd, 10);
  • if (-1 == ret)
  • {
  • return -1;
  • }
  • return sockfd;
  • }
  • int HandleConnection(int confd)
  • {
  • char tmpbuff[4096] = {0};
  • ssize_t nsize = 0;
  • nsize = recv(confd, tmpbuff, sizeof(tmpbuff), 0);
  • if (-1 == nsize)
  • {
  • return -1;
  • }
  • else if (0 == nsize)
  • {
  • return 0;
  • }
  • printf("RECV:%s\n", tmpbuff);
  • sprintf(tmpbuff, "%s --- echo", tmpbuff);
  • nsize = send(confd, tmpbuff, strlen(tmpbuff), 0);
  • if (-1 == nsize)
  • {
  • return -1;
  • }
  • return nsize;
  • }
  • int AddFd(int epfd, int fd, uint32_t tmpenvent)
  • {
  • struct epoll_event env;
  • int ret = 0;
  • = tmpenvent;
  • = fd;
  • ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &env);
  • if (-1 == ret)
  • {
  • perror("fail to epoll_ctl");
  • return -1;
  • }
  • return ret;
  • }
  • int DelFd(int epfd, int fd)
  • {
  • int ret = 0;
  • ret = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL);
  • if (-1 == ret)
  • {
  • perror("fail to epoll_ctl");
  • return -1;
  • }
  • return ret;
  • }
  • int main(void)
  • {
  • int sockfd = 0;
  • int confd = 0;
  • int ret = 0;
  • int epfd = 0;
  • int nready = 0;
  • int i = 0;
  • struct epoll_event retenv[MAX_POLL_FDNUM];
  • sockfd = CreateListenSocket(SER_IP, SER_PORT);
  • if (-1 == sockfd)
  • {
  • printf("Creating a listen socket failed\n");
  • return -1;
  • }
  • epfd = epoll_create(MAX_POLL_FDNUM);
  • if (-1 == epfd)
  • {
  • perror("fail to epoll_create");
  • return -1;
  • }
  • ret = AddFd(epfd, sockfd, EPOLLIN);
  • if (-1 == ret)
  • {
  • printf("Add socket failed\n");
  • return -1;
  • }
  • while (1)
  • {
  • nready = epoll_wait(epfd, retenv, MAX_POLL_FDNUM, -1);
  • if (-1 == nready)
  • {
  • perror("fail to epoll_wait");
  • return -1;
  • }
  • for (i = 0; i < nready; i++)
  • {
  • if (retenv[i]. == sockfd)
  • {
  • confd = accept(sockfd, NULL, NULL);
  • if (-1 == confd)
  • {
  • printf("Processing connection failed\n");
  • DelFd(epfd, sockfd);
  • close(sockfd);
  • continue;
  • }
  • AddFd(epfd, confd, EPOLLIN);
  • }
  • else if (retenv[i]. != sockfd)
  • {
  • ret = HandleConnection(retenv[i].);
  • if (-1 == ret)
  • {
  • printf("Receive exception!\n");
  • DelFd(epfd, retenv[i].);
  • close(retenv[i].);
  • continue;
  • }
  • else if (0 == ret)
  • {
  • printf("Close the connection!\n");
  • DelFd(epfd, retenv[i].);
  • close(retenv[i].);
  • continue;
  • }
  • }
  • }
  • }
  • close(epfd);
  • return 0;
  • }