#include <iostream> #include <stdio.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/select.h> #include <unistd.h> #include <sys/time.h> #include <thread> int tcp_client() { // create socket int sock_fd = socket(AF_INET, SOCK_STREAM, 0); // connect server struct sockaddr_in addr; inet_pton(AF_INET, "192.168.0.203", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(8080); connect(sock_fd, (struct sockaddr *) &addr, sizeof(addr)); // read and write char buf[1024] = {0}; while (1) { int n = read(STDIN_FILENO, buf, sizeof(buf)); write(sock_fd, buf, n); read(sock_fd, buf, sizeof (buf)); std::cout << buf << std::endl; } // close close(sock_fd); return 0; } int tcp_server() { // create socket int sock_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; inet_pton(AF_INET, "192.168.0.159", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(8080); // bind int a = bind(sock_fd, (struct sockaddr *) &addr, sizeof(addr)); // listen a = listen(sock_fd, 128); // accept struct sockaddr_in clientAddr; socklen_t len = sizeof(clientAddr); int cfd = accept(sock_fd, (struct sockaddr *) &clientAddr, &len); // read and write char buf[1024] = {0}; while (1) { int n = read(STDIN_FILENO, buf, sizeof(buf)); write(cfd, buf, n); read(cfd, buf, sizeof (buf)); std::cout << buf << std::endl; } // close close(cfd); close(sock_fd); } // duojincheng cunczai henduo wenti int fork_tcp_server() { // create socket int sock_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; inet_pton(AF_INET, "192.168.0.159", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(8081); // bind int a = bind(sock_fd, (struct sockaddr *) &addr, sizeof(addr)); // listen a = listen(sock_fd, 128); // accept struct sockaddr_in clientAddr; socklen_t len = sizeof(clientAddr); while (1) { int cfd = accept(sock_fd, (struct sockaddr *) &clientAddr, &len); pid_t pid; pid = fork(); if (pid < 0) { perror("fork error"); } else if (pid == 0) { close(sock_fd); char buf[1024]; int n = read(STDIN_FILENO, buf, sizeof(buf)); if (n < 0) { perror("client error"); close(cfd); break; } else if (n == 0) { printf("client close"); close(cfd); break; } else { printf("%s\n", buf); write(cfd, buf, n); } } else { close(cfd); } } close(sock_fd); } int thread_tcp_server() { // create socket int sock_fd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; inet_pton(AF_INET, "192.168.0.159", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(8081); // bind int a = bind(sock_fd, (struct sockaddr *) &addr, sizeof(addr)); // listen a = listen(sock_fd, 128); // accept struct sockaddr_in clientAddr; socklen_t len = sizeof(clientAddr); while (1) { int cfd = accept(sock_fd, (struct sockaddr *) &clientAddr, &len); std::thread thread([&](){ int c = cfd; char buf[1024] = {0}; while (1) { int n = read(c, buf, sizeof (buf)); write(c, buf, n); std::cout << buf << std::endl; } }); thread.detach(); } close(sock_fd); } int select_server() { // create socket int lfd = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in addr; inet_pton(AF_INET, "192.168.0.159", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(8081); // bind int a = bind(lfd, (struct sockaddr *) &addr, sizeof(addr)); // listen a = listen(lfd, 128); // select int maxfd = lfd; fd_set oldSet, rSet; FD_ZERO(&oldSet); FD_ZERO(&rSet); FD_SET(lfd, &oldSet); while (1) { rSet = oldSet; int n = select(maxfd + 1, &rSet, 0, 0, 0); if (n < 0) { perror("error"); break; } else if (n == 0) { continue; } else { // lfd if (FD_ISSET(lfd, &rSet)) { struct sockaddr_in clientAddr; socklen_t len = sizeof(clientAddr); int cfd = accept(lfd, (struct sockaddr *) &clientAddr, &len); FD_SET(cfd, &oldSet); if (cfd > maxfd) maxfd = cfd; if (--n == 0) { continue; } } // cfd for (int i = lfd + 1; i < maxfd + 1; i++) { if (FD_ISSET(i, &rSet)) { char buf[1500] = {0}; int ret = read(i, buf, sizeof(buf)); if (ret < 0) { perror("error"); close(i); FD_CLR(i, &oldSet); } else if (ret==0) { perror("error"); close(i); FD_CLR(i, &oldSet); } else { printf("%s \n", buf); } } } } } } #define OPEN_MAX 1024 #define MAX_LINE 80 #include <sys/poll.h> #include <cstring> int poll_server() { int i, j, maxi, lfd, connfd, sockfd; int nready; ssize_t n; char buf[MAX_LINE], str[INET_ADDRSTRLEN]; socklen_t clilen; struct pollfd client[OPEN_MAX]; struct sockaddr_in cliaddr, servaddr; // create socket lfd = socket(AF_INET, SOCK_STREAM, 0); int opt = 1; setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); inet_pton(AF_INET, "192.168.0.159", &servaddr.sin_addr.s_addr); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(8081); // bind int a = bind(lfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); // listen a = listen(lfd, 128); // client[0]. } #include <sys/epoll.h> int epoll_pipe() { int fd[2]; pipe(fd); pid_t pid; pid = fork(); if (pid < 0) { perror("error"); } else if (pid == 0) { close(fd[0]); char buf[5]; char ch = 'a'; while (1) { memset(buf, ch++, sizeof(buf)); write(fd[1], buf, 5); } } else { close(fd[1]); int epfd = epoll_create(1); struct epoll_event ev, evs[1]; ev.data.fd = fd[0]; ev.events = EPOLLIN; epoll_ctl(epfd, EPOLL_CTL_ADD, fd[0], &ev); while (1) { int n = epoll_wait(epfd, evs, 1, -1); if (n == 1) { char buf[128] = {0}; int ret = read(fd[0], buf, sizeof(buf)); if (ret <= 0) { close(fd[0]); epoll_ctl(epfd, EPOLL_CTL_DEL, fd[0], &ev); break; } else { printf("%s\n", buf); } } } } } /** epoll.c */ #include <stdio.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <string.h> #include <strings.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/epoll.h> #define PORT 8111 #define MSG_LEN 1024 #define MAX_EVENTS 20 #define TIMEOUT 500 int epoll_server() { int ret = -1; int on = 1; int backlog = 10; int flags = 1; int socket_fd = -1; int epoll_fd = -1;; int event_num = 0; struct epoll_event ev, events[MAX_EVENTS]; struct sockaddr_in local_addr, remote_addr; char buff[MSG_LEN] = { 0 }; socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (socket_fd < 0) { printf("create socket fd failed.\n"); _exit(-1); } flags = fcntl(socket_fd, F_GETFL, 0); fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK); ret = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); if (ret < 0) { printf("set socket option failed.\n"); } bzero(&local_addr, sizeof(local_addr)); local_addr.sin_family = AF_INET; local_addr.sin_port = htons(PORT); local_addr.sin_addr.s_addr = htonl(INADDR_ANY); ret = bind(socket_fd, (struct sockaddr *)&local_addr, sizeof(struct sockaddr)); if (ret < 0) { printf("bind port[%d] failed.\n", PORT); _exit(-1); } ret = listen(socket_fd, backlog); if (ret < 0) { printf("listen socket[%d] port[%d] failed.\n", socket_fd, PORT); _exit(-1); } printf("listening on port[%d]...\n", PORT); epoll_fd = epoll_create(256); ev.events = EPOLLIN; ev.data.fd = socket_fd; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, socket_fd, &ev); while (1) { event_num = epoll_wait(epoll_fd, events, MAX_EVENTS, TIMEOUT); for (int i = 0; i < event_num; ++i) { if (events[i].data.fd == socket_fd) { socklen_t addr_len = sizeof(struct sockaddr); int accept_fd = accept(socket_fd, (struct sockaddr *)&remote_addr, &addr_len); flags = fcntl(accept_fd, F_GETFL, 0); fcntl(accept_fd, F_SETFL, flags | O_NONBLOCK); ev.events = EPOLLIN | EPOLLET; ev.data.fd = accept_fd; epoll_ctl(epoll_fd, EPOLL_CTL_ADD, accept_fd, &ev); printf("connection[%d] estasblished...\n", accept_fd); } else if (events[i].events & EPOLLIN) { memset(buff, 0, sizeof(buff)); ret = recv(events[i].data.fd, (void *)buff, sizeof(buff), 0); if (ret <= 0) { switch (errno) { case EAGAIN: printf("receive EAGAIN, break...\n"); break; case EINTR: do { printf("index[%d] fd[%d] receive EINTR, rereceive...\n", i, events[i].data.fd); memset(buff, 0, sizeof(buff)); ret = recv(events[i].data.fd, (void *)buff, sizeof(buff), 0); } while (ret < 0 && errno == EINTR); break; default: printf("receive ret[%d] fd[%d] errno[%d|%s]\n", ret, events[i].data.fd, errno, strerror(errno)); close(events[i].data.fd); ev.events = EPOLLIN | EPOLLET; ev.data.fd = events[i].data.fd; epoll_ctl(epoll_fd, EPOLL_CTL_DEL, events[i].data.fd, &ev); break; } } if (ret > 0) { if (ret == MSG_LEN){ printf("maybe more data...\n"); } buff[ret] = '\0'; printf("recv[%s]\n", buff); send(events[i].data.fd, (void *)buff, strlen(buff), 0); } } } } close(socket_fd); return 0; } int udp_server() { int fd = socket(AF_INET, SOCK_DGRAM, 0); struct sockaddr_in myaddr; myaddr.sin_family = AF_INET; myaddr.sin_port = htons(9000); myaddr.sin_addr.s_addr = inet_addr("192.168.0.159"); int ret = bind(fd, (struct sockaddr*)&myaddr, sizeof (myaddr)); char buf[1500] = {0}; struct sockaddr_in cliaddr; socklen_t len = sizeof (cliaddr); int n = 0; struct sockaddr_in dstaddr; dstaddr.sin_family = AF_INET; dstaddr.sin_port = htons(8888); dstaddr.sin_addr.s_addr = inet_addr("0.0.0.0"); while (1) { n = recvfrom(fd, buf, sizeof (buf), 0, (struct sockaddr*)&cliaddr, &len); if (n < 0) { break; } else { printf("%s\n", buf); sendto(fd, buf, n, 0, (struct sockaddr*)&cliaddr, len); } } close(fd); return 0; } int main() { udp_server(); return 0; }