0% found this document useful (0 votes)
100 views13 pages

TCP Server with Select & Poll

Uploaded by

Jaya Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views13 pages

TCP Server with Select & Poll

Uploaded by

Jaya Prakash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

24 .

Design a TCP concurrent server to convert a given


text into upper case using multiplexing system call
“select”

Server Code

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <sys/select.h>

#include <errno.h>

#include<ctype.h>

#define SERV_PORT 9877


#define MAXLINE 4096

#define LISTENQ 10

void to_uppercase(char *str) {

while (*str) {

*str = toupper((unsigned char) *str);

str++;

int main(int argc, char **argv) {


int listenfd, connfd, sockfd;

int maxfd, maxi, i;

int nready, client[FD_SETSIZE];

ssize_t n;

fd_set rset, allset;

char buf[MAXLINE];

socklen_t clilen;

struct sockaddr_in cliaddr, servaddr;

listenfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

listen(listenfd, LISTENQ);

maxfd = listenfd;

maxi = -1;

for (i = 0; i < FD_SETSIZE; i++)

client[i] = -1;

FD_ZERO(&allset);

FD_SET(listenfd, &allset);

for (;;) {

rset = allset;
nready = select(maxfd + 1, &rset, NULL, NULL, NULL);

if (FD_ISSET(listenfd, &rset)) {

clilen = sizeof(cliaddr);

connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

for (i = 0; i < FD_SETSIZE; i++) {

if (client[i] < 0) {

client[i] = connfd;

break;

if (i == FD_SETSIZE) {

perror("too many clients");

exit(1);

FD_SET(connfd, &allset);

if (connfd > maxfd)

maxfd = connfd;

if (i > maxi)

maxi = i;

if (--nready <= 0)

continue;

for (i = 0; i <= maxi; i++) {


if ((sockfd = client[i]) < 0)

continue;

if (FD_ISSET(sockfd, &rset)) {

if ((n = read(sockfd, buf, MAXLINE)) == 0) {

close(sockfd);

FD_CLR(sockfd, &allset);

client[i] = -1;

} else {

buf[n] = '\0';

to_uppercase(buf);

write(sockfd, buf, n);

if (--nready <= 0)

break;

}
}

/* End of server code and start of client code for testing */

Client Code for Testing :

#include <stdio.h>
#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define MAXLINE 4096

#define SERV_PORT 9877

void str_cli(FILE *fp, int sockfd) {

char sendline[MAXLINE], recvline[MAXLINE];

while (fgets(sendline, MAXLINE, fp) != NULL) {

write(sockfd, sendline, strlen(sendline));

if (read(sockfd, recvline, MAXLINE) == 0) {

perror("str_cli: server terminated prematurely");

exit(1);
}

fputs(recvline, stdout);

int main(int argc, char **argv) {

int sockfd;

struct sockaddr_in servaddr;

if (argc != 2) {
fprintf(stderr, "usage: %s <IPaddress>\n", argv[0]);

exit(1);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(SERV_PORT);

if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {

perror("inet_pton error");

exit(1);

if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {

perror("connect error");

exit(1);
}

str_cli(stdin, sockfd);

close(sockfd);

return 0;

}
Output :

How to execute the code ???

1. Open two terminals.


2. Compile and Run server code in one terminal
3. Compile and run client code in another terminal (./a.out 127.0.0.1)

Enjoy the code.

25. Design a TCP concurrent server to echo given set of


sentences using poll functions

Server Code Using poll


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#include <poll.h>
#include <errno.h>

#define SERV_PORT 9877

#define MAXLINE 4096

#define OPEN_MAX 1024

int main(int argc, char **argv) {

int listenfd, connfd, sockfd;

int i, maxi, nready;

ssize_t n;

char buf[MAXLINE];

socklen_t clilen;

struct pollfd client[OPEN_MAX];

struct sockaddr_in cliaddr, servaddr;

listenfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

listen(listenfd, 10);

client[0].fd = listenfd;

client[0].events = POLLIN;

for (i = 1; i < OPEN_MAX; i++)

client[i].fd = -1;
maxi = 0;

for (;;) {

nready = poll(client, maxi + 1, -1);

if (client[0].revents & POLLIN) {

clilen = sizeof(cliaddr);

connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

for (i = 1; i < OPEN_MAX; i++) {

if (client[i].fd < 0) {

client[i].fd = connfd;

break;

if (i == OPEN_MAX) {

perror("too many clients");


exit(1);

client[i].events = POLLIN;

if (i > maxi)

maxi = i;

if (--nready <= 0)

continue;

}
for (i = 1; i <= maxi; i++) {

if ((sockfd = client[i].fd) < 0)

continue;

if (client[i].revents & (POLLIN | POLLERR)) {

if ((n = read(sockfd, buf, MAXLINE)) < 0) {

if (errno == ECONNRESET) {

close(sockfd);

client[i].fd = -1;

} else {

perror("read error");

} else if (n == 0) {

close(sockfd);

client[i].fd = -1;

} else {

write(sockfd, buf, n);

if (--nready <= 0)

break;

}
Client Code for Testing

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netinet/in.h>

#define MAXLINE 4096

#define SERV_PORT 9877

void str_cli(FILE *fp, int sockfd) {

char sendline[MAXLINE], recvline[MAXLINE];

while (fgets(sendline, MAXLINE, fp) != NULL) {

write(sockfd, sendline, strlen(sendline));

if (read(sockfd, recvline, MAXLINE) == 0) {

perror("str_cli: server terminated prematurely");

exit(1);

fputs(recvline, stdout);

}
int main(int argc, char **argv) {

int sockfd;

struct sockaddr_in servaddr;

if (argc != 2) {

fprintf(stderr, "usage: %s <IPaddress>\n", argv[0]);

exit(1);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_port = htons(SERV_PORT);

if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {

perror("inet_pton error");

exit(1);
}

if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) {

perror("connect error");

exit(1);

str_cli(stdin, sockfd);

close(sockfd);
return 0;

Output:

How to execute the code???

Same as previous code lol.

You might also like