标签:io
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
// non-block
int flags = fcntl(sockfd, F_GETFL, 0);
flags |= O_NONBLOCK;
int ret = fcntl(sockfd, F_SETFL, flags);
int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
/* * echo_client_block.c * * Created on: Jun 7, 2014 * Author: damonhao */ #include <stdio.h> //perror() #include <string.h> //strlen() #include <strings.h>//bzero() #include <errno.h> #include <unistd.h> //close() #include <stdlib.h> //exit() //net #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define MAXBUF 1024*1000 //#define MAXBUF 20 int main(int agrc, char *argv[]) { if (agrc != 3) { puts("usage:<program> <server ip> <port>"); exit(0); } puts("block echo client up!"); const char *server_ip = argv[1]; int server_port = atoi(argv[2]); int sockfd; //Create streaming socket if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(errno); } struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(server_port); inet_pton(AF_INET, server_ip, &server_addr.sin_addr); if (connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) != 0) { perror("connect"); exit(errno); } char input_buff[MAXBUF]; char message[MAXBUF]; memset(message, 'a', MAXBUF - 1); message[MAXBUF - 1] = '\0'; int send_num = send(sockfd, message, strlen(message) + 1, 0); printf("send num:%d\n", send_num); int recv_num = recv(sockfd, input_buff, sizeof(input_buff), 0); printf("recv num:%d\n", recv_num); close(sockfd); return 0; } /* ./echo_client_block 127.0.0.1 9999 block echo client up! send num:1024000 recv num:65664 */
/* * echo_client_nonblock.c * * Created on: Jun 7, 2014 * Author: damonhao */ #include <stdio.h> //perror() #include <string.h> //strlen() #include <strings.h>//bzero() #include <errno.h> #include <unistd.h> //close() #include <stdlib.h> //exit() //net #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/poll.h> #define MAXBUF 1024*1000 int main(int agrc, char *argv[]) { if (agrc != 3) { puts("usage:<program> <server ip> <port>"); exit(0); } puts("nonblock echo client up!"); const char *server_ip = argv[1]; int server_port = atoi(argv[2]); int sockfd; //Create a nonblocking streaming socket if ((sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) { perror("socket"); exit(errno); } struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(server_port); inet_pton(AF_INET, server_ip, &server_addr.sin_addr); //For nonblocking sockfd, connect will return -1 immediately with errno set to EINPROGRESS. int ret = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)); int saved_errno = (ret == 0) ? 0 : errno; if (saved_errno == EINPROGRESS || saved_errno == 0) { struct pollfd pfds[1]; pfds[0].fd = sockfd; pfds[0].events = POLLOUT; while (1) { poll(pfds, sizeof(pfds), -1); if (pfds[0].revents & POLLOUT) { char input_buff[MAXBUF]; char message[MAXBUF]; memset(message, 'a', MAXBUF - 1); message[MAXBUF - 1] = '\0'; int send_num = send(sockfd, message, strlen(message) + 1, 0); printf("send num:%d\n", send_num); int recv_num = recv(sockfd, input_buff, sizeof(input_buff), 0); printf("recv num:%d\n", recv_num); close(sockfd); break; } } } else { perror("connect"); } return 0; } /* ./echo_client_nonblock 127.0.0.1 9999 nonblock echo client up! send num:180224 recv num:1024 **/
/* * echo_server_block.c * * Created on: Jun 8, 2014 * Author: damonhao */ #include <strings.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> //net #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define MAXBUF 1024 int main(int argc, char *argv[]) { if (argc != 2) { puts("usage:<program> <port>"); return 0; } puts("block echo server up!"); //set server address; int port = atoi(argv[1]); struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(port); int listen_sockfd = -1; //Create streaming socket if ((listen_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(errno); } //bind server address to listen_sockfd if (bind(listen_sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) != 0) { perror("bind error"); exit(errno); } //make listen_sockfd a "listening socket" if (listen(listen_sockfd, 20) != 0) { perror("listen error"); exit(errno); } while (1) { struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); int clientfd = accept(listen_sockfd, (struct sockaddr*) &client_addr, &addr_len); pid_t pid; if ((pid = fork()) < 0) { perror("fork"); exit(errno); } else if (pid == 0) //in child progress { char buffer[MAXBUF]; printf("connection from %s:%d up\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); //Echo back anything received int recv_num = 0; while ((recv_num = recv(clientfd, buffer, MAXBUF, 0)) > 0) { printf("recevied data size: %d\n", recv_num); send(clientfd, buffer, recv_num, 0); } if (recv_num < 0) { puts("received data error"); } close(clientfd); printf("connection from %s:%d down\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); break; } else // in parent progress { close(clientfd); } } return 0; }
/* * echo_server_nonblock.c * * Created on: Jun 8, 2014 * Author: damonhao */ #include <strings.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> //net //define _GNU_SOURCE for accept4() //#define _GNU_SOURCE //man page says use _GNU_SOURCE, but in socket.h I find it should be __USE_GNU; //define __USE_GNU for accept4() #define __USE_GNU #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/poll.h> #define MAXBUF 1024 int main(int argc, char *argv[]) { if (argc != 2) { puts("usage:<program> <port>"); return 0; } puts("nonblock echo server up!"); //set server address; int port = atoi(argv[1]); struct sockaddr_in server_addr; bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = INADDR_ANY; server_addr.sin_port = htons(port); int listen_sockfd = -1; //Create streaming socket if ((listen_sockfd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) { perror("socket"); exit(errno); } //bind server address to listen_sockfd if (bind(listen_sockfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) != 0) { perror("bind error"); exit(errno); } //make listen_sockfd a "listening socket" if (listen(listen_sockfd, 20) != 0) { perror("listen error"); exit(errno); } struct pollfd pfds[1]; pfds[0].fd = listen_sockfd; pfds[0].events = POLLIN; while (1) { struct sockaddr_in client_addr; socklen_t addr_len = sizeof(client_addr); poll(pfds, sizeof(pfds), -1); if (pfds[0].revents & POLLIN) { int clientfd = accept4(listen_sockfd, (struct sockaddr*) &client_addr, &addr_len, SOCK_NONBLOCK); pid_t pid; if ((pid = fork()) < 0) { perror("fork"); exit(errno); } else if (pid == 0) // in child progress; { printf("connection from %s:%d up\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); struct pollfd client_pfds[1]; client_pfds[0].fd = clientfd; client_pfds[0].events = POLLIN; while (1) { poll(client_pfds, sizeof(client_pfds), -1); if (client_pfds[0].revents & POLLIN) { char buffer[MAXBUF]; int recv_num = recv(clientfd, buffer, MAXBUF, 0); if (recv_num > 0) { printf("recevied data size: %d\n", recv_num); //FIXME: may send less than require; int send_num = send(clientfd, buffer, recv_num, 0); if (send_num < 0) { perror("send data error"); } } else if (recv_num == 0) { close(clientfd); printf("connection from %s:%d down\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); break; } else { perror("received data error"); exit(errno); } } } } else // in parent progress { close(clientfd); } } } return 0; }
阻塞与非阻塞IO -- 网络编程随想,布布扣,bubuko.com
标签:io
原文地址:http://blog.csdn.net/damonhao/article/details/29379735