/*server.cpp*/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define MYPORT 2000 // the port users will be connecting to #define BACKLOG 5 // how many pending connections queue will hold #define BUF_SIZE 200 typedef struct sockaddr_in my_sockaddr_in; typedef struct _cust_{ int fd; // accepted connection fd my_sockaddr_in* client_addr; }custConn; custConn fd_A[BACKLOG]; void distribution(int id, char * mess) { int i; for (i = 0; i < BACKLOG; i++) { if(i != id && fd_A[i].fd > 0) { send(fd_A[i].fd, mess, strlen(mess) + 1, 0); } } } int main(void) { int sock_fd, new_fd; // listen on sock_fd, new connection on new_fd my_sockaddr_in server_addr; // server address information my_sockaddr_in* pClient_addr; // connector's address information socklen_t sin_size; int yes = 1; char buf[BUF_SIZE]; char content[BUF_SIZE + 100]; int ret; if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } server_addr.sin_family = AF_INET; // host byte order server_addr.sin_port = htons(MYPORT); // short, network byte order server_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP memset(server_addr.sin_zero, '/0', sizeof(server_addr.sin_zero)); if (bind(sock_fd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1) { perror("bind"); exit(1); } if (listen(sock_fd, BACKLOG) == -1) { perror("listen"); exit(1); } printf("listen port %d/n", MYPORT); fd_set fdsr; int maxsock; int i; struct timeval tv; sin_size = sizeof(my_sockaddr_in); maxsock = sock_fd; while (1) { // initialize file descriptor set FD_ZERO(&fdsr); FD_SET(sock_fd, &fdsr); // timeout setting tv.tv_sec = 1; tv.tv_usec = 0; // add active connection to fd set for (i = 0; i < BACKLOG; i++) { if (fd_A[i].fd != 0) { FD_SET(fd_A[i].fd, &fdsr); } } ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv); if (ret < 0) { perror("select"); break; } else if (ret == 0) { //timeout continue; } // check every fd in the set for (i = 0; i < BACKLOG; i++) { if (fd_A[i].fd > 0 && FD_ISSET(fd_A[i].fd, &fdsr)) { memset(buf, 0x00, BUF_SIZE); ret = recv(fd_A[i].fd, buf, sizeof(buf), 0); if (ret <= 0) { //client close printf("client[%d] offline/n", i); close(fd_A[i].fd); //notice other user memset(content, 0x00, BUF_SIZE); sprintf(content, "%s offline", inet_ntoa((fd_A[i].client_addr)->sin_addr)); distribution(i, content); //clear fd_set FD_CLR(fd_A[i].fd, &fdsr); fd_A[i].fd = 0; } else { // receive data printf("client[%d] send:%s/n", i, buf); memset(content, 0x00, BUF_SIZE + 100); sprintf(content, "%s say: %s", inet_ntoa((fd_A[i].client_addr)->sin_addr), buf); distribution(i, content); } } } // check whether a new connection comes if (FD_ISSET(sock_fd, &fdsr)) { pClient_addr = new my_sockaddr_in; new_fd = accept(sock_fd, (struct sockaddr *) pClient_addr, &sin_size); if (new_fd <= 0) { perror("accept"); continue; } printf("new connection %s:%d socket:%d ", inet_ntoa(pClient_addr->sin_addr), ntohs( pClient_addr->sin_port), new_fd); //new connection int id; for(id = 0; id < BACKLOG; id++) { //find idle id if(fd_A[id].fd == 0) break; } printf("id: %d/n", id); if (id == BACKLOG) { printf("but, max connections arrive, reject./n"); send(new_fd, "bye", 4, 0); close(new_fd); continue; } //new user fd_A[id].fd = new_fd;/*save new socket*/ fd_A[id].client_addr = pClient_addr; if (new_fd > maxsock)//select need max maxsock = new_fd; //welcome this user send(new_fd, "welcome!", 9, 0); //notice other user memset(content, 0x00, BUF_SIZE + 100); sprintf(content, "%s online", inet_ntoa(pClient_addr->sin_addr)); distribution(id, content); } }//end while // close other connections for (i = 0; i < BACKLOG; i++) { if (fd_A[i].fd != 0) { close(fd_A[i].fd); delete fd_A[i].client_addr; } } exit(0); } /*client.cpp*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define PORT 2000 #define MAXSIZE 100 int main(int argc, char * argv[]) { if (argc != 2) { printf("error./n"); exit(1); } int listenfd, num; struct hostent * he; struct sockaddr_in server; if ((he = gethostbyname(argv[1])) == NULL) { perror("gethostbyname"); exit(1); } if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } memset(&server, 0x00, sizeof(server)); server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *) he->h_addr); if (connect(listenfd, (struct sockaddr *) &server, sizeof(server)) == -1) { perror("connect"); exit(1); } char buf[MAXSIZE]; int length; fd_set fdsr; int maxsock, ret; struct timeval tv; maxsock = listenfd; while(true) { // initialize file descriptor set FD_ZERO(&fdsr); FD_SET(0, &fdsr); FD_SET(listenfd, &fdsr); // timeout setting tv.tv_sec = 1; tv.tv_usec = 0; ret = select(maxsock + 1, &fdsr, NULL, NULL, &tv); if (ret < 0) { perror("select"); break; } else if (ret == 0) { continue; } //check socket read if (FD_ISSET(listenfd, &fdsr)) { memset(buf, 0x00, sizeof(buf)); length = recv(listenfd, buf, MAXSIZE, 0); if (length > 0) { printf("-> %s/n", buf); memset(buf, 0x00, sizeof(buf)); } } //check input if (FD_ISSET(0, &fdsr) && getchar() != EOF) { scanf("%s", buf); if(strcmp(buf, "exit") == 0) break; if ((length = send(listenfd, buf, sizeof(buf), 0)) == -1) { perror("send data:"); //close(socketfd); return -1; } } } close(listenfd); return 0; }