// select-server.c — a cheezy multiperson chat server
// For Q1.1
#define _POSIX_C_SOURCE 200112L
Copyright By PowCoder代写 加微信 powcoder
#include
#include
#include
#include
#include
#include
#include
#define PORT “9034”
int main(int argc, char* argv[]) {
struct addrinfo hints, *res;
// create address we’re going to listen on (with given port number)
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_STREAM; // TCP
hints.ai_flags = AI_PASSIVE; // for bind, listen, accept
// node (NULL means any interface), service (port), hints, res
int s = getaddrinfo(NULL, PORT, &hints, &res);
if (s != 0) {
fprintf(stderr, “getaddrinfo: %s\n”, gai_strerror(s));
exit(EXIT_FAILURE);
// create socket
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0) {
perror("socket");
exit(EXIT_FAILURE);
// reuse the socket if possible
int const reuse = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(int)) < 0) {
perror("setsockopt");
exit(EXIT_FAILURE);
// bind address to socket
if (bind(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
perror("bind");
exit(EXIT_FAILURE);
freeaddrinfo(res);
// listen on the socket
listen(sockfd, 5);
// initialise an active file descriptors set
fd_set masterfds;
FD_ZERO(&masterfds);
FD_SET(sockfd, &masterfds);
// record the maximum socket number
int maxfd = sockfd;
while (1) {
// monitor file descriptors
fd_set readfds = masterfds;
if (select(FD_SETSIZE, &readfds, NULL, NULL, NULL) < 0) {
perror("select");
exit(EXIT_FAILURE);
// loop all possible descriptor
for (int i = 0; i <= maxfd; ++i)
// determine if the current file descriptor is active
if (FD_ISSET(i, &readfds)) {
// welcome socket
// create new socket if there is new incoming connection request
if (i == sockfd) {
struct sockaddr_in cliaddr;
socklen_t clilen = sizeof(cliaddr);
int newsockfd =
accept(sockfd, (struct sockaddr*)&cliaddr, &clilen);
if (newsockfd < 0)
perror("accept");
// add the socket to the set
FD_SET(newsockfd, &masterfds);
// update the maximum tracker
if (newsockfd > maxfd)
maxfd = newsockfd;
// print out the IP and the socket number
char ip[INET_ADDRSTRLEN];
printf(“new connection from %s on socket %d\n”,
// convert to human readable string
inet_ntop(cliaddr.sin_family, &cliaddr.sin_addr,
ip, INET_ADDRSTRLEN),
newsockfd);
// a message is sent from the client
char buff[256];
int n = read(i, buff, 256);
if (n <= 0) {
if (n < 0)
perror("read");
printf("socket %d close the connection\n", i);
FD_CLR(i, &masterfds);
// broadcast the message as required
for (int j = 0; j <= maxfd; ++j)
if (j != i && j != sockfd &&
FD_ISSET(j, &masterfds) &&
write(j, buff, n) < 0) {
perror("write");
FD_CLR(j, &masterfds);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com