Select function behavior - Multi Client Quiz - c

I have to build a quiz application.
Details about the application:
1. Each client has to register to server before participating in Quiz. The server will ask
username from each user and generate temporary id for each user.
2. After Registration process clients, who are successfully connected to server will get
Question from server.
3. The client will reply with answer.
4. Server will receive answer from different clients with time stamps, and it will calculate
time difference of each client which is called ∆t.
Define such as:
∆t = (Time Question sent - Time answer received) - RTT
Where RTT is Round Trip Time
Server will select client, whose ∆t is minimum to all and reply with whatever score client will gain remains will not gain any score.
After sending Question server will wait Answer for a particular time periods called (T). If client did not reply within ‘T’ time period Server will skip that Question and goes to next Question.
Pseudocode of main loop in my server code
A. A main while loop which runs once for each question.
B. Inside this first I am accepting login for 10 seconds.
Here I am assigning user Id and all other initialization stuff.
C. Then using `select` to check which are available for writing.
To available connections I am checking `RTT` and then sending question to each user.
D. Then I am waiting for some time to get answers.
Here I am using `select` to determine where the answer is available to read.
E. Then I am repeating steps C. and D.
Problem:
When I connect only to a single client my code works fine for any number of question.
But when I test this code on multiple client with the same client code:
Login for everyone is OK.
Sending First Question to everyone works fine.
Then while waiting for the answer I only received answer from one client. Each client shows that answer is been sent. For second client the second select function doesn't return with readable data availability.
Why for multi-client my code is not working. (According to me the error is somewhere in getting answer).
My Code:
The structure of the packets send can be understand easily from the variable names.
Server.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <error.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <time.h>
#define PORT "3490" //the port user will be connecting to
#define BACKLOG 10 //how many pending connection queue will hold
#define maxUser 10
#define LOGIN_OK "OK"
#define LOGIN_WrongPassword "NP"
#define LOGIN_WrongUsername "NU"
#define MAX_USERS 10
#define MAX_ANSWER_TIME 10
#define LOGIN_WAIT 10
#define TOTAL_QUES "3"
int users[MAX_USERS][3] = {}; //index is userID, 0 is no user
void sigchld_handler(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
//get sockaddr, IPv4 or IPv6
int timer;
void alarm_handler(int s) {
timer = 0;
}
wrongRecv(ssize_t recvd, ssize_t expctd)
{
if(recvd != expctd)
{
printf("Recvd(%zd) bytes not equal to expected(%zd) bytes\n",recvd,expctd);
//getchar();
}
}
//void nextQues(char* quesMsg, char* ques, char* optA, char* optB, char* optC, char* optD)
int nextQues(char* quesMsg, int QID)
{
char ques[40], optA[10], optB[10], optC[10], optD[10], quesId[5];
sprintf(quesId,"%d",QID);
strncpy(ques, "This is the question?",22);
strncpy(optA, "OptionA", 7); strncpy(optB, "OptionB", 7); strncpy(optC, "OptionC", 7); strncpy(optD, "OptionD", 7);
strncpy(quesMsg,quesId,5);
strncpy(quesMsg + 05,ques,40);
strncpy(quesMsg + 45,optA,10);
strncpy(quesMsg + 55,optB,10);
strncpy(quesMsg + 65,optC,10);
strncpy(quesMsg + 75,optD,10);
return 0;
}
//void answerCheck(char* ques, char* optA, char* optB, char* optC, char* optD, char* usrResponse, int rtt, int timeTaken)
void answerCheck(int fd, char usrResponse[6], int rtt, int timeTaken)
{
int responseTime, i;
char actualAnswer[1];
char quesId[5];
printf("fd(%d) quesid(%s) response(%c) rtt(%d) timeTaken(%d)\n", fd, usrResponse, usrResponse[5], rtt, timeTaken );
strncpy(quesId, usrResponse, 5);
actualAnswer[0] = 'B';//we have quesId we can find actual answer on basis of it
if(actualAnswer[0] == usrResponse[5])
{
//printf("%s\n","+++++" );
responseTime = timeTaken - rtt;
//printf("Response Time(%d)\n",responseTime);
//save it with user id
//finding userid
for(i = 0; i < MAX_USERS; i++) {
if(users[i][1] == fd) {
users[i][2] = responseTime;//saving it
//printf("%d\n",i );
}
}
}
}
int compareAnswer() {
int i, min = 2 * MAX_ANSWER_TIME, userIndex;
for(i = 0; i < MAX_USERS; i++) {
if(users[i][2] < min) {
min = users[i][2];
userIndex = i;
}
}
//Increasing Score
users[userIndex][0]++;
//returning fd
return users[userIndex][1];
}
void users_deleteFd(int fd) {
int i;
for (i = 0; i < MAX_USERS; ++i)
{
if(users[i][1] == fd) {
users[i][1] =0;
return;
}
}
}
int rtt_check(int new_fd)
{
ssize_t send_ret, recv_ret;
char rtt_check[1];
time_t rtt1, rtt2;
rtt1 = time(NULL);
send_ret = send(new_fd, "r", 1, 0);
if(send_ret == 0)
{
return -2;
}
wrongRecv(send_ret, 1);
//printf("%s\n","Between two phase of rttCheck" );
recv_ret = recv(new_fd, rtt_check, 1,0);
rtt2 = time(NULL);
if(recv_ret == 0)
{
return -2;
}
wrongRecv(recv_ret,1);
//printf("diff(%d)\n",(int) difftime(rtt2,rtt1));
return (int) difftime(rtt2,rtt1);
}
int login(char user[], char pass[])
{
//for user
static int Id = 0; //when have function getUserID, make it not static and also remove Id++;
if(!strcmp(user,"abhishek") && !strcmp(pass,"abhishek")) {
//Id = getUserID(user);
return ++Id;
}else if(!strcmp(user,"abhishek")){
return 0; //wrong password
}
return -1; //wrong username
}
int totalQues;
int login_setup(int new_fd)
{
//login inititalizations
char login_det[16];
char username[9],password[9], login_statMsg[7], totalQuesMsg[5] = TOTAL_QUES;
totalQues = atoi(totalQuesMsg);
//for user
int userId;
//for wrongRecv
ssize_t send_ret,recv_ret;
//getting username and password
recv_ret = recv(new_fd,login_det,16,0);
if(recv_ret == 0)
{
return -2;
}
wrongRecv(recv_ret,16);
//extracting username nad password
strncpy(username,login_det,8);
strncpy(password,login_det+8,8);
username[8]='\0'; password[8]='\0';
//printf("username(%s) and password(%s)\n",username,password);
if( (userId = login(username,password)) > 0) {
//printf("%d\n",userId);
//sending status
strncpy(login_statMsg, LOGIN_OK, 2);
strncpy(login_statMsg + 2, totalQuesMsg , 5);
send_ret = send(new_fd, login_statMsg,7,0);
if(send_ret == 0)
{
return -2;
}
wrongRecv(send_ret,7);
//TODO error checking then handling if error
//users[userId][0] = 0; //score
users[userId][1] = new_fd; //file descriptor associated with this user
//users[userId][2] = 0; //answer time
return 1;
}
else if(userId == -1) { //wrong username
strncpy(login_statMsg, LOGIN_WrongUsername, 2);
strncpy(login_statMsg + 2, totalQuesMsg , 5);
send_ret = send(new_fd, login_statMsg,7,0);
if(send_ret == 0)
{
return -2;
}
wrongRecv(send_ret,7);
return 0;
}
else{
strncpy(login_statMsg, LOGIN_WrongPassword, 2);
strncpy(login_statMsg + 2, totalQuesMsg , 5);
send_ret = send(new_fd, login_statMsg,7,0);
if(send_ret == 0)
{
return -2;
}
wrongRecv(send_ret,7);
return 0;
}
//TODO erorr handling of above two case
//TODO make login a loop
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main(void)
{
int listen_fd, new_fd; // listen on sock_fd, new connection on new_fd
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr;//connection's address info
socklen_t sin_size;
int yes=1;
char s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;//IPv4 or IPv6
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; // use my IP
if((rv = getaddrinfo(NULL,PORT, &hints, &servinfo)) != 0){ //getting which IPv server supports
fprintf(stderr, "getaddrinfo: %s\n",gai_strerror(rv));
return 1;
}
//loop through all the result and bind to the first we can
for(p = servinfo; p != NULL; p = p->ai_next){
if((listen_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
perror("server : socket");
continue;
}
if(setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror("set sockopt");
exit(1);
}
if(bind(listen_fd, p->ai_addr, p->ai_addrlen) == -1){
close(listen_fd);
perror("server: bind");
continue;
}
break;
}
if(p == NULL) {
fprintf(stderr, "server:failed to bind\n");
return 2;
}
freeaddrinfo(servinfo);//all done with this structure
if(listen(listen_fd, BACKLOG) == -1){
perror("listen");
exit(1);
}
//printf("listen_fd(%d)\n",listen_fd );
// sa.sa_handler = sigchld_handler; // reap all dead processes
// sigemptyset(&sa.sa_mask);
// sa.sa_flags = SA_RESTART;
// if(sigaction(SIGCHLD, &sa, NULL) == -1){
// perror("sigaction");
// exit(1);
// }
printf("server waiting for connections.....\n");
fd_set master; //master file descriptor list
fd_set read_fds; //temp file descriptor list for select()
int fdmax;
FD_ZERO(&master); //clear the master and temp sets
FD_ZERO(&read_fds);
FD_SET(listen_fd, &master);
//keep track of the bigge file descriptor
fdmax = listen_fd; // so far it is this one
ssize_t recv_ret, send_ret;
//for login
int loginStatus;
struct sigaction sa;
sa.sa_handler = alarm_handler;
sigemptyset(&sa.sa_mask);
//sa.sa_flags = SA_RESTART;
if(sigaction(SIGALRM, &sa, NULL) == -1){
perror("sigaction");
exit(1);
}
//login while
alarm(LOGIN_WAIT);//accepting login only for 10 seconds
timer = 1;
printf("\n-----------------------------Waiting for users to login for %d seconds.-----------------------------\n",LOGIN_WAIT);
while(timer) {
sin_size = sizeof their_addr;
new_fd = accept(listen_fd, (struct sockaddr *)&their_addr, &sin_size);
if(new_fd == -1){
//perror("accept");
break;// this break is very important , as we are using alarm(Signals) and accept is a blocking function
//If accept is in blocked sate and our signal comes then accept will exit returning error. So
//if error then we have to break else next satements will run on falsy values.
//In reality we dont need this as I alredy set the SA_RESTART flag in sigaction which means
//after returning from the signal handler restart the activity on which you are previously
//instead of starting execution from next line.
}else {
inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s);
printf("server : got connection from %s\n", s);
//LOGIN //need to call login function via thread because this
//may stop the function if user doesnot respond
loginStatus = login_setup(new_fd);
//adding to select checkup
if(loginStatus) {
printf("User Loginned Succesfully\n");
}
}
}
printf("-----------------------------Login Closed. Now starting the QUIZ.-----------------------------\n");
//for randome seek
srand(time(NULL));
//for main loop counter
int i, win_fd;
//for questions
int QID = 0;
int maxQues_Len = 40, maxOpt_len = 10, maxQuesId_len = 5;//including '\0' this time
char quesMsg[80], answer[6];//score doesnot include \0
//char ques[40], optA[10], optB[10], optC[10], optD[10];
//for time calculation of each answer
ssize_t time_ques, time_ans;
//getting all avialable participants
fdmax = 0;
FD_ZERO(&master);
for(i = 0; i < MAX_USERS; i++) {
if( (new_fd = users[i][1]) != 0){
FD_SET(new_fd, &master);
if(new_fd > fdmax)
fdmax = new_fd;
//printf("%d\n",new_fd);
}
}
int current_rtt;
//while for main quiz
while(totalQues--) {
//checking who are ready for witing
if(select(fdmax+1, NULL, &master, NULL, NULL) == -1){//here select will return withh all the descriptors which are
//ready to write , all others have to miss this question
perror("select");
exit(1);
}
//setting which question to send
QID++;
//for sending questions to all
for(i = 0; i <= fdmax; i++) {
if(FD_ISSET(i, &master)) {
//rtt check
current_rtt = rtt_check(i);
if(current_rtt == -2) {//connection closed
FD_CLR(i, &master);
users_deleteFd(i);
continue;
}
//setting question
//nextQues(quesMsg, ques, optA, optB, optC, optD);
nextQues(quesMsg, QID);
printf("Sending Question QID(%s) fd(%d)\n",quesMsg,i);
//send a question
time_ques = time(NULL);
send_ret = send(i, quesMsg, maxQues_Len + 4 * maxOpt_len + maxQuesId_len, 0);
if(send_ret == 0) {//connection closed
FD_CLR(i, &master);
users_deleteFd(i);
continue;
}
wrongRecv(send_ret, maxQues_Len + 4 * maxOpt_len + maxQuesId_len);
}
}
//ASSUMING Question is send ot all the users at same time
//receiving and waiting for answers
alarm(MAX_ANSWER_TIME);
timer = 1;
FD_ZERO(&read_fds);
read_fds = master;
// unsigned int qq = read_fds.fd_count;
// for (int ii = 0; ii < qq; ++ii)
// {
// printf("%d\n",read_fds.fd_array[i] );
// }
while(timer) {
//printf("HURRAY\n");
if(select(fdmax+1, &read_fds, NULL, NULL, NULL) <=0){
perror("select");
//exit(4);
break;//break is important. Explained above
}
for(i = 0; i <= fdmax; i++) {
//printf("Recving answer I(%d)\n",i);
if(FD_ISSET(i, &read_fds)) {
//receiving answer
//TODO if we get answer to wrong ques
printf("Recving answer I(%d) fdmax (%d)\n",i,fdmax);
recv_ret = recv(i,answer,6,0);
time_ans = time(NULL);
wrongRecv(recv_ret,6);
printf("%s\n",answer );
if(recv_ret == 0)//connection closed
{
FD_CLR(i, &read_fds);
FD_CLR(i, &master);
users_deleteFd(i);
continue;
}else if(recv_ret > 0){
if(QID == atoi(answer)) { //we have received the answer to this question so remove the user from wait answer loop
FD_CLR(i, &read_fds);
//printf("%s i(%d)\n","#######",i );
answerCheck(i ,answer, current_rtt, (int) difftime(time_ans,time_ques));
//printf("Answer(%c)\n",answer[0]);
}
else{//we have recvd something unexpectable so ignore for NOW
}
}
//time_t cccc = time(NULL);
//printf("%s I(%d)\n",ctime(&cccc),i);
}
}
}
//comparing answers
win_fd = compareAnswer();
//sending score
}
return 0;
}
Client.c:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT "3490" //the port client will be connecting to
#define MAXDATASIZE 100 // max number of bytes we can get at once
//get sockaddr ,IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
if(sa->sa_family ==AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
wrongRecv(ssize_t recvd, ssize_t expctd)
{
if(recvd != expctd)
{
printf("Recvd(%zd) bytes not equal to expected(%zd) bytes\n",recvd,expctd);
getchar();
}
}
void rtt_check(int sockfd)
{
ssize_t send_ret, recv_ret;
char rtt_check[1];
recv_ret = recv(sockfd, rtt_check, 1,0);
wrongRecv(recv_ret,1);
sleep(1);//to check
send_ret = send(sockfd, "r", 1, 0);
wrongRecv(send_ret, 1);
return;
}
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct addrinfo hints, *servinfo, *p;
int rv;
char s[INET6_ADDRSTRLEN];
if(argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) {
fprintf(stderr,"getaddrinfo: %s\n",gai_strerror(rv));
return 1;
}
//lopp through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next) {
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1){
perror("client: socket");
continue;
}
if(connect(sockfd, p->ai_addr, p->ai_addrlen) == -1){
close(sockfd);
perror("client: connect");
continue;
}
break;
}
if(p ==NULL) {
fprintf(stderr,"client: failed to connect\n");
return 2;
}
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);
printf("client : connecting to %s\n", s);
freeaddrinfo(servinfo); // all done with this structure
char login_det[17] = "abhishekabhishek";
char login_retMsg[7], login_stat[3], totalQuesMsg[5];
int totalQues;
//sending login details
ssize_t send_ret,recv_ret;
send_ret = send(sockfd, login_det,16,0);
wrongRecv(send_ret,16);
//receiving login status
recv_ret = recv(sockfd,login_retMsg,7,0);
wrongRecv(recv_ret,7);
strncpy(login_stat, login_retMsg, 2);
login_stat[2] = '\0';
printf("Login Status(%s)\n",login_stat);
strncpy(totalQuesMsg, login_retMsg + 2, 5);
totalQues = atoi(totalQuesMsg);
printf("totalQues(%d)\n",totalQues);
if(!strcmp(login_stat,"OK")) { //login ok
char quesId[5];
int maxQues_Len = 40, maxOpt_len = 10, maxQuesId_len = 5;//including '\0' this time
char quesMsg[80], scoreMsg[1];//score doesnot include \0
char ques[40], optA[10], optB[10], optC[10], optD[10];
char answer[6];
while(totalQues--) {
//checking rtt
rtt_check(sockfd);
//receving question
recv_ret = recv(sockfd, quesMsg, maxQues_Len + 4 * maxOpt_len + maxQuesId_len ,0);
wrongRecv(recv_ret, maxQues_Len + 4 * maxOpt_len + maxQuesId_len);
strncpy(quesId,quesMsg,5);
strncpy(ques, quesMsg + 05, 40);
strncpy(optA, quesMsg + 45, 10);
strncpy(optB, quesMsg + 55, 10);
strncpy(optC, quesMsg + 65, 10);
strncpy(optD, quesMsg + 75, 10);
printf("QUESID(%s) Question(%s), A(%s) , B(%s) , C(%s) , D(%s)\n", quesId, ques, optA, optB, optC, optD);
//choose answer
scoreMsg[0] = 'B';
strncpy(answer,quesId, 5);
answer[5] = scoreMsg[0];
sleep(5);
//sending answer
send_ret = send(sockfd, answer,6,0);
wrongRecv(send_ret,6);
printf("%s\n","Answer Message Sent" );
// if((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
// perror("recv");
// exit(1);
// }
// buf[numbytes] = '\0';
// printf("client: received '%s'\n",buf);
}
}
//TODO wrong login
close(sockfd);
return 0;
}

The problem is that the call to select in the answer getting loop is modifying read_fds to hold just the file descriptor of the first client(s) to respond. Since you don't reset read_fds before calling select again, it will not recognize the other clients' response.

Related

Socket programming chat application

I made a chat application using sockets in c language. When i run server and client on same device it works fine. But, when i run client and server on different device client shows connect error. is this problem related to ip address.
server side code:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <signal.h>
#define MAX_CLIENTS 100
#define BUFFER_SZ 2048
static _Atomic unsigned int cli_count = 0;
static int uid = 10;
/* Client structure */
typedef struct{
struct sockaddr_in address;
int sockfd;
int uid;
char name[32];
} client_t;
client_t *clients[MAX_CLIENTS];
pthread_mutex_t clients_mutex = PTHREAD_MUTEX_INITIALIZER;
void str_overwrite_stdout() {
printf("\r%s", "> ");
fflush(stdout);
}
void str_trim_lf (char* arr, int length) {
int i;
for (i = 0; i < length; i++) { // trim \n
if (arr[i] == '\n') {
arr[i] = '\0';
break;
}
}
}
void print_client_addr(struct sockaddr_in addr){
printf("%d.%d.%d.%d",
addr.sin_addr.s_addr & 0xff,
(addr.sin_addr.s_addr & 0xff00) >> 8,
(addr.sin_addr.s_addr & 0xff0000) >> 16,
(addr.sin_addr.s_addr & 0xff000000) >> 24);
}
/* Add clients to queue */
void queue_add(client_t *cl){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i < MAX_CLIENTS; ++i){
if(!clients[i]){
clients[i] = cl;
break;
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Remove clients to queue */
void queue_remove(int uid){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i < MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid == uid){
clients[i] = NULL;
break;
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Send message to all clients except sender */
void send_message(char *s, int uid){
pthread_mutex_lock(&clients_mutex);
for(int i=0; i<MAX_CLIENTS; ++i){
if(clients[i]){
if(clients[i]->uid != uid){
if(write(clients[i]->sockfd, s, strlen(s)) < 0){
perror("ERROR: write to descriptor failed");
break;
}
}
}
}
pthread_mutex_unlock(&clients_mutex);
}
/* Handle all communication with the client */
void *handle_client(void *arg){
char buff_out[BUFFER_SZ];
char name[32];
int leave_flag = 0;
cli_count++;
client_t *cli = (client_t *)arg;
// Name
if(recv(cli->sockfd, name, 32, 0) <= 0 || strlen(name) < 2 || strlen(name) >= 32-1){
printf("Didn't enter the name.\n");
leave_flag = 1;
} else{
strcpy(cli->name, name);
sprintf(buff_out, "%s has joined\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
}
bzero(buff_out, BUFFER_SZ);
while(1){
if (leave_flag) {
break;
}
int receive = recv(cli->sockfd, buff_out, BUFFER_SZ, 0);
if (receive > 0){
if(strlen(buff_out) > 0){
send_message(buff_out, cli->uid);
str_trim_lf(buff_out, strlen(buff_out));
printf("%s -> %s\n", buff_out, cli->name);
}
} else if (receive == 0 || strcmp(buff_out, "exit") == 0){
sprintf(buff_out, "%s has left\n", cli->name);
printf("%s", buff_out);
send_message(buff_out, cli->uid);
leave_flag = 1;
} else {
printf("ERROR: -1\n");
leave_flag = 1;
}
bzero(buff_out, BUFFER_SZ);
}
/* Delete client from queue and yield thread */
close(cli->sockfd);
queue_remove(cli->uid);
free(cli);
cli_count--;
pthread_detach(pthread_self());
return NULL;
}
int main(int argc, char **argv){
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "127.0.0.1";
int port = atoi(argv[1]);
int option = 1;
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
struct sockaddr_in cli_addr;
pthread_t tid;
/* Socket settings */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(ip);
serv_addr.sin_port = htons(port);
/* Ignore pipe signals */
signal(SIGPIPE, SIG_IGN);
if(setsockopt(listenfd, SOL_SOCKET,(SO_REUSEPORT | SO_REUSEADDR),(char*)&option,sizeof(option)) < 0){
perror("ERROR: setsockopt failed");
return EXIT_FAILURE;
}
/* Bind */
if(bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
perror("ERROR: Socket binding failed");
return EXIT_FAILURE;
}
/* Listen */
if (listen(listenfd, 10) < 0) {
perror("ERROR: Socket listening failed");
return EXIT_FAILURE;
}
printf("=== WELCOME TO THE CHATROOM ===\n");
while(1){
socklen_t clilen = sizeof(cli_addr);
connfd = accept(listenfd, (struct sockaddr*)&cli_addr, &clilen);
/* Check if max clients is reached */
if((cli_count + 1) == MAX_CLIENTS){
printf("Max clients reached. Rejected: ");
print_client_addr(cli_addr);
printf(":%d\n", cli_addr.sin_port);
close(connfd);
continue;
}
/* Client settings */
client_t *cli = (client_t *)malloc(sizeof(client_t));
cli->address = cli_addr;
cli->sockfd = connfd;
cli->uid = uid++;
/* Add client to the queue and fork thread */
queue_add(cli);
pthread_create(&tid, NULL, &handle_client, (void*)cli);
/* Reduce CPU usage */
sleep(1);
}
return EXIT_SUCCESS;
}
client side code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define LENGTH 2048
// Global variables
volatile sig_atomic_t flag = 0;
int sockfd = 0;
unsigned char name[32];
void str_overwrite_stdout() {
printf("%s", "> ");
fflush(stdout);
}
void str_trim_lf (char* arr, int length) {
int i;
for (i = 0; i < length; i++) { // trim \n
if (arr[i] == '\n') {
arr[i] = '\0';
break;
}
}
}
void catch_ctrl_c_and_exit(int sig) {
flag = 1;
}
void send_msg_handler() {
unsigned char message[LENGTH] = {};
unsigned char buffer[3000] = {};
while(1) {
str_overwrite_stdout();
fgets(message, LENGTH, stdin);
str_trim_lf(message, LENGTH);
if (strcmp(message, "exit") == 0) {
break;
} else {
sprintf(buffer, "%s: %s\n", name, message);
send(sockfd, buffer, strlen(buffer), 0);
}
bzero(message, LENGTH);
bzero(buffer, LENGTH + 32);
}
catch_ctrl_c_and_exit(2);
}
void recv_msg_handler() {
char message[LENGTH] = {};
while (1) {
int receive = recv(sockfd, message, LENGTH, 0);
if (receive > 0) {
printf("%s", message);
str_overwrite_stdout();
} else if (receive == 0) {
break;
} else {
// -1
}
memset(message, 0, sizeof(message));
}
}
int main(int argc, char **argv){
if(argc != 2){
printf("Usage: %s <port>\n", argv[0]);
return EXIT_FAILURE;
}
char *ip = "152.168.0.100";
int port = atoi(argv[1]);
signal(SIGINT, catch_ctrl_c_and_exit);
printf("Please enter your name: ");
fgets(name, 32, stdin);
str_trim_lf(name, strlen(name));
if (strlen(name) > 32 || strlen(name) < 2){
printf("Name must be less than 30 and more than 2 characters.\n");
return EXIT_FAILURE;
}
struct sockaddr_in server_addr;
/* Socket settings */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
// Connect to Server
int err = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (err == -1) {
printf("ERROR: connect\n");
return EXIT_FAILURE;
}
// Send name
send(sockfd, name, 32, 0);
printf("=== WELCOME TO THE CHATROOM ===\n");
pthread_t send_msg_thread;
if(pthread_create(&send_msg_thread, NULL, (void *) send_msg_handler, NULL) != 0){
printf("ERROR: pthread\n");
return EXIT_FAILURE;
}
pthread_t recv_msg_thread;
if(pthread_create(&recv_msg_thread, NULL, (void *) recv_msg_handler, NULL) != 0){
printf("ERROR: pthread\n");
return EXIT_FAILURE;
}
while (1){
if(flag){
printf("\nBye\n");
break;
}
}
close(sockfd);
return EXIT_SUCCESS;
}
it is showing connect error when i run it on different machines.
So as far as I can tell, the codes that you posted are incomplete, but let's assume that, as you say, everything is working fine, it work in the local machine, but when you run the two programs in two different devices, the client fails to connect to the server.
Then I would ask you the question, as I also asked it in the comment: are the two devices in the same subnet? You have a home router, and both devices are connected to this router?
If not, then that will be the problem. I am not sure how deep you understand IP addresses and subnets, but here is a simple summary. When you look at the IP address on your PC, you see your private IP address that was given you by the router. Let's say, that you have two machines connected to the same router, one has the IP address of 192.168.0.101, the other 192.168.0.102. If 192.168.0.101 is the client and wants to connect to the server that runs on 192.168.0.102, it should work without problems.
But let's say that you send the server code to me. I run the server on my computer, which has the IP address of 192.168.0.103, you still run the client at home on 192.168.0.101. In this case, you won't be able to connect to my machine, because the 192.168.0.103 is the private IP address given out by my own router. For that to work, I would have to at least configure my router to open up a port and forward the packets coming to that port to my PC. Then I would have to give you the public IP and the port. And so if the firewall is configured so that it allows your packets into my network, then it might work.
Of course it is a bit more complicated than this, but this is the general idea.
If this is not the case for you, and the devices are on the same subnet, then we would simply need more information and the comlete code.

In TCP socket program,client send some data, but server need read multiple times. why?

I have a question about socket.I send N-size data from client to server, N-size less than 100 byte.So I think my data should not be split to multiple tcp packet.In my opinion, Client send data should be done at one times and Server can receive data at one time.But The result is not satisfactory.Real situation is the server need call read data.I don't understand it.Follow code:
epoll_server.cpp(only receive data.)
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <netdb.h>
#define BUFSIZE 1024
#define INITSIZE 1024
#define MAXEVENTCOUNT 10240
// add non-blocking to sockfd
int make_socket_non_blocking(int fd)
{
// get initial flag
int src_flags;
src_flags= fcntl(fd, F_GETFL,0);
if(src_flags == -1)
{
perror("fcntl get error.");
return-1;
}
// add non-blocking
int new_flags = src_flags | O_NONBLOCK;
int ret_value;
ret_value = fcntl(fd, F_SETFL, new_flags);
if(ret_value == -1)
{
perror("fcntl set error.");
return-1;
}
return 0;
}
// main function
int main(int argc, char* argv[])
{
int server_sockfd, client_sockfd;
int server_len;
struct sockaddr_in server_address;
// create server socket fd
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
// init server address struct
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(9567);
server_address.sin_addr.s_addr = INADDR_ANY;
server_len = sizeof(server_address);
// bind server address info for server fd
if((bind(server_sockfd, (struct sockaddr*)&server_address, server_len)) == -1)
{
perror("bind error");
exit(EXIT_FAILURE);
}
// let server is listened state
listen(server_sockfd, 5);
printf("server start waiting for connect...\r\n");
// only suggestion
int efd = epoll_create(INITSIZE);
if(-1 == efd)
{
printf("epoll_create error happen.\n");
return -1;
}
// set server_sockfd
struct epoll_event server_event, event;
server_event.data.fd = server_sockfd;
server_event.events = EPOLLIN | EPOLLET;
int ret_epollctl = epoll_ctl(efd, EPOLL_CTL_ADD, server_sockfd, &server_event);
if(-1 == ret_epollctl)
{
printf("epoll_ctl error happen when efd is adding server_sockfd.\n");
return -1;
}
/* event loop */
struct epoll_event* return_events;
// set timeout is 3000 ms
int timeout_msecond = 3000;
return_events = (struct epoll_event*)malloc(MAXEVENTCOUNT*sizeof(struct epoll_event));
int count = 0;
while(1)
{
int ret_epollwait = epoll_wait(efd, return_events, MAXEVENTCOUNT, timeout_msecond);
// part_1:epoll_wait error happen
if(-1 == ret_epollwait)
{
printf("logged epoll_wait error happen.\n");
continue;
}
// part_2:epoll_wait timeout
if(0 == ret_epollwait)
{
printf("logged epoll_wait timeout.\n");
continue;
}
// part_3:do some other event
int index = 0;
for(index = 0; index < MAXEVENTCOUNT; index++)
{
// part_3-1:hup ...
if((return_events[index].events & EPOLLERR)
|| (return_events[index].events & EPOLLHUP)
|| !(return_events[index].events & EPOLLIN) )
{
continue;
}
// part_3-2:is connection
if(return_events[index].data.fd == server_sockfd)
{
struct sockaddr_in client_address;
int client_len = sizeof(client_address);
// server accept connection from client
int client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, (socklen_t*)&client_len);
// part_3-2-1:connection error happen
if(-1 == client_sockfd)
{
if((EAGAIN == errno)
|| (EWOULDBLOCK == errno) )
{
continue;
}
else
{
printf("accept error occured.\n");
continue;
}
}
else // part_3-2-2:normal connection
{
// get clinet some information
char hostinfo_buf[BUFSIZE] = {0};
char servname_buf[BUFSIZE] = {0};
int tmp_ret = getnameinfo((struct sockaddr*)&client_address, client_len, hostinfo_buf, sizeof(hostinfo_buf), servname_buf, sizeof(servname_buf), NI_NUMERICHOST| NI_NUMERICSERV);
if(0 == tmp_ret)
{
printf("Accepted connection on descriptor %d:ip=%s, port=%s.\n", client_sockfd, hostinfo_buf, servname_buf);
}
// set client_sockfd to non-blocking
tmp_ret = make_socket_non_blocking(client_sockfd);
if(-1 == tmp_ret)
{
printf("set client_sockfd=%d to non-blocking error occured.\n", client_sockfd);
abort();
}
// set client_sockfd is EPOLLIN, EPOLLET
event.data.fd = client_sockfd;
event.events = EPOLLIN | EPOLLET;
tmp_ret = epoll_ctl(efd, EPOLL_CTL_ADD, client_sockfd, &event);
if(tmp_ret == -1)
{
printf("efd add %d has a error.\n", client_sockfd);
continue;
}
printf("add descriptor %d:ip=%s, port=%s successfully.\n", client_sockfd, hostinfo_buf, servname_buf);
}
continue;
}
// part_3-3:read data from client
printf("read data start++++\n");
int temp = 0;
// get recv_cache size start
int recvsize = 0;
socklen_t optlen = sizeof(recvsize);
int err = getsockopt(return_events[index].data.fd, SOL_SOCKET, SO_RCVBUF, &recvsize, &optlen);
printf("recv cache size :%d\n", recvsize);
// get recv_cache size end
while(1) // start while(1)
{
printf("%d times read data\n", ++temp);
char* recv_buffer = (char*)malloc(1024+1);
memset(recv_buffer, 0, 1025);
// int ret_read = read(return_events[index].data.fd, recv_buffer, sizeof(recv_buffer));
int ret_read = recv(return_events[index].data.fd, recv_buffer, sizeof(recv_buffer), 0);
// part_3-3-1:read return error
if(-1 == ret_read)
{
if(EAGAIN != errno)
{
printf("read data from %d error occured, errno=%d, %s.\n", return_events[index].data.fd, errno, strerror(errno));
}
break;
}
// part_3-3-2:no data
if(0 == ret_read)
{
continue;
}
// part_3-3-3:output data. If data is 'bye', connection will close.
if(ret_read > 0)
{
printf("%d client's data:size=%dbyte, content=%s\n", return_events[index].data.fd, ret_read, recv_buffer);
// part_3-3-3-1:close connection and remove client_sockfd
if((recv_buffer[0] == 'b')
&& (recv_buffer[1] == 'y')
&& (recv_buffer[2] == 'e') )
{
close(return_events[index].data.fd);
printf("close %d, ", return_events[index].data.fd);
int tmp_ret = epoll_ctl(efd, EPOLL_CTL_DEL, return_events[index].data.fd, NULL);
if(tmp_ret == -1)
{
printf("efd del %d has a error.\n", client_sockfd);
}
printf("remove descriptor %d successfully.\n", return_events[index].data.fd);
}
}
} // end of while(1)
printf("read data finish------\n");
}
}
free(return_events);
// close server_sockfd
shutdown(server_sockfd, 2);
return 0;
}
epoll_client.cpp(only send data.)
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFSIZE 1024
int main(int argc, char* argv[])
{
int sock_clientfd, ret_recvsize, i;
struct sockaddr_in dest, mine;
char send_buffer[BUFSIZE + 1];
// create socket fd
if ((sock_clientfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
perror("Socket");
exit(EXIT_FAILURE);
}
// init server address that client will connetct to.
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(9567);
if(argc != 2)
{
printf("Usage: %s <dest ip>\n", argv[0]);
printf("Usage: %s 127.0.0.1\n", argv[0]);
return -1;
}
printf("-----\n");
if (inet_aton(argv[1], (struct in_addr *) &dest.sin_addr.s_addr) == 0)
{
perror(argv[1]);
exit(1);
}
// connect to server
printf("will connect!\n");
if (connect(sock_clientfd, (struct sockaddr *) &dest, sizeof(dest)) != 0)
{
perror("Connect ");
exit(EXIT_FAILURE);
}
while(1)
{
bzero(send_buffer, BUFSIZE + 1);
printf("input message:");
fgets(send_buffer, BUFSIZE, stdin);
send_buffer[strlen(send_buffer) - 1] = '\0';
printf("%d\n", strlen(send_buffer));
int send_retsize = send(sock_clientfd, send_buffer, strlen(send_buffer), 0);
if(send_retsize == -1)
{
perror("send data to client error happen!");
exit(EXIT_FAILURE);
}
printf("send succ data:%s\n", send_buffer);
if((send_buffer[0] == 'b')
&& (send_buffer[1] == 'y')
&& (send_buffer[2] == 'e') )
{
printf("client active close connect.\n");
break;
}
}
// close sock_clientfd
close(sock_clientfd);
return 0;
}
Follow pircture is some run info:
epoll_server.png
epoll_client.png
The server read data is only 8 byte, Is the kernel design epoll is this?
I guess the reasons are as follows pirture:
The reason you don't receive everything that is available in one read is because you only read 8 bytes at a time.
char* recv_buffer = (char*)malloc(1024+1);
int ret_read = recv(return_events[index].data.fd, recv_buffer, sizeof(recv_buffer), 0);
// part_3-3-1:read return error
recv_buffer is a char* not an array, so sizeof recv_buffer equals the size of a pointer which in your case is 8.
Note that you should never rely on data arriving in packages. If your message protocol states that you should be getting 10 bytes never expect all 10 bytes to be available at once. You should always code in a way that can handle data being split up into multiple reads.
If the thread handles a single socket then a simple do { read... } while (total_bytes_received < expected_bytes); will suffice.
If the thread handles multiple connections, then you need to save the bytes you have read and then continue to manage other sockets that are ready before returning to your handling loop that will use select/epoll to wait for more data.

Using Select for Multiple Socket Connections C

I am trying to build a server and client for a chat server using sockets. I know that I am supposed to use select() to get input from multiple sockets, but I am not sure how to do it and still read from them properly. As my code sits, it reads perfectly fine from one client, but when two are open it ignores everything that the second client does, until the first is closed.
I am wondering how to properly implement select to make sure that I can take input from multiple sockets. Thanks in advance.
#include "../../include/my.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void interrupt();
void interrupt()
{
my_str("Server exiting.\n");
exit(0);
}
int str_to_num(char* str)
{
int i = 0;
int numDigits = 0;
int ret = 0;
while (str[numDigits] != '\0')
{
numDigits++;
}
while (i < numDigits)
{
int digit = str[i];
if (digit < 48 && digit > 57)
return -1;
digit -= 48;
ret += digit * my_pow(10, numDigits - i - 1);
i++;
}
return ret;
}
char* add_null_term(char* str)
{
int i = 0;
while (str[i] != '\0')
i++;
str[i] = '\0';
return str;
}
int main(int argc, char **argv)
{
int sockfd, newsockfd, portnum;
int len;
char buffer[256];
/*char *username = (char*)malloc(256*sizeof(char));*/
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
/*check args*/
if (argc < 2)
{
my_str("Usage: ./server <port_number>\n");
exit(0);
}
portnum = str_to_num(argv[1]);
if (portnum < 1024)
{
perror("Ports below 1024 are reserved for root access.\n");
exit(0);
}
if (portnum < 1 || portnum > 65535)
{
my_str("Port must be between 1 and 65535\n");
exit(0);
}
signal(SIGINT, interrupt);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((char*)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portnum);
serv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr))<0)
{
perror("bind");
exit(0);
}
listen(sockfd, 5);
clilen = sizeof(cli_addr);
while(1)
{
if ((newsockfd=accept(sockfd, (struct sockaddr*)&cli_addr, &clilen)) < 0)
{
perror("accept");
exit(0);
}
usleep(2000);
my_str("Server received: ");
while ((len = read(newsockfd, &buffer, 256)) > 0)
{
buffer[my_strlen(buffer)] = '\0';
if (my_strcmp(buffer, "/") == 0)
{
my_str("Error: command ");
my_str(buffer);
my_str("not found.\n");
bzero((char*)&buffer, sizeof(buffer));
}
/*if (count == 0)
{
my_strcpy(username, buffer);
my_str("setting nickname to: ");
my_str(username);
bzero((char*)&buffer, sizeof(buffer));
my_str(buffer);
count++;
my_str("\n");
}*/
/*else if (my_strncmp(buffer, "/me", 3) == 0)
{
my_str(username);
my_str(" ");
my_str(&buffer[4]);
bzero((char*)&buffer, sizeof(buffer));
my_str("\n");
}
else if (my_strncmp(buffer, "/nick", 5) == 0)
{
my_str("Changing nickname of ");
my_str(username);
my_strcpy(username, &buffer[6]);
my_str(" to ");
my_str(username);
bzero((char*)&buffer, sizeof(buffer));
my_str("\n");
}*/
/*else
{*/
/*my_str(username);
my_str(": ");*/
my_str(buffer);
bzero((char*)&buffer, sizeof(buffer));
my_str(buffer);
my_str("\n");
/*}*/
}
my_str("\nServer: Message end. Waiting for next connection.\n");
}
return 0;
}
If your question is HOW to use select() in a server...
Here is a snippet off the top of my head of one technique
to get you started... bear in mind all error checking is not
present, and it is not optimized to function in a round robin
basis...
listen(sockfd, SOMAXCONN)
fd_set my_set;
fd_set wk_set;
FD_ZERO(&my_set); /* initialize fd_set */
FD_SET(sock_fd, &my_set) /* put listener into fd_set */
max_fd = sockfd;
while( TRUE )
{
memcpy(&wk_set, &my_set, sizeof(my_set));
rc = select(max_fd + 1, &wk_set, NULL, NULL, NULL )
if ( rc == -1 )
{
if ( errno == EINTR )
continue;
printf("select failed: errno = %d\n", errno);
exit(1);
}
for(i = 0; i < max_fd; i++) /* check everything in wk_set */
{
if ( FD_ISSET(i, &wk_set)
{
if ( i == sockfd ) /* is it the listener socket?? */
{
wk_sock=accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);
if ( wk_sock == -1 )
{
printf("accept failed: errno=%d\n", errno);
exit(1);
}
FD_SET(wk_sock, &my_set); /* put into fd_set */
if ( wk_sock > max_sock )
max_sock = wk_sock;
}else{
/* ready to read from this socket */
recv_len = recv(i, .... )
if ( recv_len == 0 ) /* client disconnected */
{
close(i);
FD_CLR(i, &my_set);
if (i == max_fd )
{
for(x=0;x<max_fd;x++) /* find new max_fd */
{
if ( FD_ISSET(x, &my_set )
max_fd = x;
}
}
} else {
handle message from client
}
}
}
}
}

Soft terminating a simple webserver

I'm building my own webserver. For now, my minimalist code is:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#define SERVER_PORT 80
int main () {
int nReqSocketId, nReqSize = 1024, nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
char *sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress;
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
if (nMainSocketId == 0) {
fprintf(stderr, "Error during the creation of the socket\n");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
fprintf(stderr, "The port %d is busy\n", SERVER_PORT);
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (1) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen");
close(nMainSocketId);
exit(1);
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (nReqSocketId < 0) {
perror("server: accept");
close(nMainSocketId);
exit(1);
}
recv(nReqSocketId, sRequest, nReqSize, 0);
if (nReqSocketId > 0){
printf("The Client is connected...\n\n%s\n", sRequest);
}
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
Can I create a "soft closing mechanism" making the webserver to print the "Goodbye!" phrase located after the infinite loop? When I type the "q" letter, for example…
Why not eliminate all of those write functions and just use a single send()?
All you would need to do is store your response in a buffer, then send buffer:
// Global
#define MAX 2048
char response[MAX]; // No need for char*
// In Main
memset(response, 0, MAX); // **EDIT**
strcpy(response, "HTTP/1.1 200 OK\n");
strcat(response, "Content-length: 50\n");
strcat(response, "Content-Type: text/html\n\n");
strcat(response, "<html><body><h1>Hello world!!!</h1></body></html>\n");
// Now simply send the whole response in one go:
send(nReqSocketId, response, strlen(response), 0);
Also, you could also simply make this a non persistent connection like so:
// Global
#define MAX 2048
char response[MAX]; // No need for char*
// In Main
memset(response, 0, MAX); // **EDIT**
strcpy(response, "HTTP/1.1 200 OK\n");
strcat(response, "Content-Type: text/html\n\n");
strcat(response, "<html><body><h1>Hello world!!!</h1></body></html>\n");
// Now simply send the whole response in one go again:
send(nReqSocketId, response, strlen(response), 0);
// Shutdown the socket so it cannot write anymore:
shutdown(nReqSocketId,1);
// Then totally close it when you are ready:
close(nReqSocketId);
The latter might be better suited for what you are currently doing; since you are not keeping multiple connections alive in your webserver anyways.
Once you close down a connection on the server side the client (i.e. the web browser) knows to stop expecting content and will finish the job properly.
Hope this helps.
Cheers!
PS-
This, of course, a response to your last question in this thread not so much the soft-termination part.
I would also have to suggest that you memset(response, 0, MAX) so that you have a nice clean slate every time you are responding.
Under an *IXish OS a signal will wake up certain blocking system calls and have them return.
They will indicate an eror and set errno to EINTR. Such mechanics you can use to gracefully shutdown your server.
Please see code from your answer slightly adjusted/corrected below:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#define SERVER_PORT 8080 /* Using a port>1024 one does not need to run this as root. */
void onInt()
{
/* Do nothing. */
}
void onQuit()
{
/* Do nothing. */
}
int main()
{
int nMainSocketId = -1, nReqSocketId = -1;
size_t nReqSize = 1024;
char * sRequest = malloc(nReqSize); /* TODO: add error checking */
socklen_t nAddrLen;
struct sockaddr_in oAddress = {0};
signal(SIGINT, onInt); /* TODO: add error checking */
signal(SIGQUIT, onQuit); /* TODO: add error checking */
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) /* 0 is a valid file/socket descriptor! */
{
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress)))
{
perror("server: bind() failed");
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (1)
{
int result = 0;
if (listen(nMainSocketId, 10) < 0)
{
perror("server: listen() failed");
close(nMainSocketId);
return 1;
}
result = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (result < 0)
{
if (EINTR == errno)
{
printf("Shutdown requested. Exiting ...\n");
break;
}
perror("server: accept failed()");
close(nMainSocketId);
return 1;
}
nReqSocketId = result;
result = recv(nReqSocketId, sRequest, nReqSize, 0); /* TODO: Check wether the request size was really read! */
if (result < 0)
{
perror("server: recv() failed");
close(nMainSocketId);
return 1;
}
else if (result == 0)
{
printf("The client is disconnected. Waiting for another connection ...\n");
close(nReqSocketId);
nReqSocketId = -1;
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
/* TODO: add error checking for ALL write()s */
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
nReqSocketId = -1;
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
Be aware that also the call to recv() could block waiting for data and then due to SIGINT be interupted. So the same logic as done when checking the result of accept() should be applied to recv().
#PandaSobao
I removed write() using fprintf() in conjunction with a file descriptor, I think this is the best way, because doesn't require strlen()... Look at the code below:
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#define REQUEST_SIZE 1024
#define SERVER_PORT 80
int bExiting, nMainSocketId;
void terminateServer () {
if (bExiting) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocketId);
bExiting = 1;
}
int main () {
int nRecvResult = -1, nReqSocketId = -1;
char sRequest[REQUEST_SIZE];
socklen_t nAddrLen;
struct sockaddr_in oAddress;
FILE *nRespFD;
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
signal(SIGINT, terminateServer);
signal(SIGQUIT, terminateServer);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) {
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("server: bind() failed");
terminateServer();
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (bExiting == 0) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen() failed");
terminateServer();
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (bExiting) { break; }
if (nReqSocketId < 0) {
perror("server: accept() failed");
terminateServer();
return 1;
}
nRecvResult = recv(nReqSocketId, sRequest, REQUEST_SIZE, 0);
if (nRecvResult < 0) {
perror("server: recv() failed");
terminateServer();
return 1;
}
if (nRecvResult == 0) {
printf("The client is disconnected. Waiting for another connection...\n");
close(nReqSocketId);
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
nRespFD = fdopen(nReqSocketId, "a+");
fprintf(nRespFD, "HTTP/1.1 200 OK\n");
fprintf(nRespFD, "Content-length: 50\n");
fprintf(nRespFD, "Content-Type: text/html\n\n");
fprintf(nRespFD, "<html><body><h1>Hello world!!!</h1></body></html>\n");
fclose(nRespFD);
close(nReqSocketId);
}
printf("Goodbye!\n");
return 0;
}
About shutdown() versus close(), could you explain better the difference?
Okay, so the second part of my response explains how to make your web server non-persistent. Persistent-Connections used to be known as Keep-Alive prior to HTTP 1.1, btw. Since I saw that you are closing your response socket every time after you send you might as well make the web server 'non-persistent.'
This means you do not have to send "Content-Length: X" because of shutdown(). You can shutdown a socket three different ways:
"The constants SHUT_RD, SHUT_WR, SHUT_RDWR have the value 0, 1, 2, respectively..."
By doing shutdown(SOCKET, 1) we are essentially sending a FIN ack to the client (browser) that lets it know that the socket is done writing. Therefore, no need to set a "Content-Length: X" header response.
Now, this is not a shutdown() VS close() thing. It is a shutdown() AND close() thing. You still have to close() the socket in order to destroy it. Shutdown does not do that for you.
In summary, if you have no need to keep track of multiple connections consider taking out "Content-Length:" from header. Just use shutdown(SOCKET, 1) as an "end of transmission" mechanism and close to destroy the socket afterwards.
/----------------------------------------------------------------------------------------------------------------------------------/
As far as creating a whole FILE* just to send string literals, I am not sure is worth it. Have you benched this and gotten some results? Regardless, I know of other ways around strcat() [ which includes a strlen() of course] and strlen() all together if performance is what you are looking for. I was not aware that was the case.
PS- strcat() is pretty fast in the beginning, only when concatenating a large buffer does it start to increase its complexity. Look up SIMD and how some those types of functions are optimized depending on your architecture.
Waiting on both stdin and the socket
This is a POSIX-specific behavior that explicitly doesn't work on Windows. It relies on the fact that on *n*x platforms, sockets are file descriptors, and it uses the select function.
Here is a simplified function that wraps select to wait on a single socket.
/*Waits on a single socket and stdin.*/
int waitOnStdinAndSocket(
int sockFd, /*[in] Socket descriptor*/
int *pInputOnStdin, /*[out] Set to a nonzero value if there is input on stdin*/
int *pInputOnSocket, /*[out] Set to a nonzero value if there is input on the socket*/
sturct timeval *timeout /*[in/opt] Timeout*/
) /*Returns a negative value on failure.*/
{
int ret;
fd_set fds;
*pInputOnStdin = 0;
*pInputOnSocket = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
FD_SET(sockFd, &fds);
ret = select(sockFd+1, &fds, NULL, NULL, timeout);
if(ret >= 0)
{
*pInputOnStdin = FD_ISSET(STDIN_FILENO, &fds);
*pInputOnSocket = FD_ISSET(sockFd, &fds);
}
return ret;
}
#Medinoc
Thank you very much!
After reading your reply I have tried a bit 'on the net and have found this GNU page: http://www.gnu.org/software/libc/manual/html_node/Server-Example.html.
So, after some attempts, I have been able to integrate all things with my "hello world" example. And here is the result:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <string.h>
#define SERVER_PORT 80
#define REQUEST_MAX_SIZE 1024
char sRequest[REQUEST_MAX_SIZE];
int bListening = 1, nMainSocket;
void terminateServer () {
if (bListening == 0) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocket);
bListening = 0;
}
void switchStdin () {
if (strcmp(sRequest, "q\n") == 0) {
terminateServer();
} else {
printf("Unknown request %s\n", sRequest);
}
}
void helloWorld (const int nRequestId) {
printf("The client is connected...\n\n%s\n", sRequest);
write(nRequestId, "HTTP/1.1 200 OK\n", 16);
write(nRequestId, "Content-length: 50\n", 19);
write(nRequestId, "Content-Type: text/html\n\n", 25);
write(nRequestId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
}
int main (void) {
int nOldReqSock, nNewReqSock, nReqLen, nUninitLen = REQUEST_MAX_SIZE;
fd_set oActiveFD, oReadFD;
socklen_t nAddrLen;
struct sockaddr_in oAddress;
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
/* Create the socket and set it up to accept connections. */
nMainSocket = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocket < 0) {
perror("socket");
return 1;
}
if (bind(nMainSocket, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("bind");
terminateServer();
return 1;
}
if (listen(nMainSocket, 10) < 0) {
perror("listen");
terminateServer();
return 1;
}
/* Initialize the set of active sockets plus STDIN. */
FD_ZERO(&oActiveFD);
FD_SET(STDIN_FILENO, &oActiveFD);
FD_SET(nMainSocket, &oActiveFD);
printf("\n W E L C O M E!\n\nType \"q\" to quit.\n");
while (bListening) {
/* Block until input arrives on one or more active sockets. */
oReadFD = oActiveFD;
if (select(FD_SETSIZE, &oReadFD, NULL, NULL, NULL) < 0) {
perror("select");
terminateServer();
return EXIT_FAILURE;
}
/* Service all the sockets with input pending. */
for (nOldReqSock = 0; bListening && nOldReqSock < FD_SETSIZE; ++nOldReqSock) {
if (FD_ISSET(nOldReqSock, &oReadFD)) {
if (nOldReqSock == nMainSocket) {
/* Connection request on original socket. */
nAddrLen = sizeof(oAddress); /* why??? */
nNewReqSock = accept(nMainSocket, (struct sockaddr *) &oAddress, &nAddrLen);
if (nNewReqSock < 0) {
perror("accept");
terminateServer();
return EXIT_FAILURE;
}
FD_SET(nNewReqSock, &oActiveFD);
} else {
/* Data arriving on an already-connected socket. */
nReqLen = read(nOldReqSock, sRequest, REQUEST_MAX_SIZE);
if (nReqLen < 0) {
/* Read error. */
perror("read");
terminateServer();
return EXIT_FAILURE;
} else if (nReqLen == 0) {
/* End-of-file. */
printf("End-of-file\n");
close(nOldReqSock); /* why??? */
FD_CLR(nOldReqSock, &oActiveFD); /* why??? */
continue;
} else {
/* Data read. */
if (nUninitLen > nReqLen) { memset(sRequest + nReqLen, 0, nUninitLen - nReqLen); }
nUninitLen = nReqLen;
}
if (nOldReqSock == STDIN_FILENO) {
/* Standard input received */
switchStdin(nReqLen);
} else {
/* TCP/IP request received */
helloWorld(nOldReqSock);
}
}
}
}
}
printf("Goodbye\n");
return 0;
}
I added also some /* why??? */ comments near the lines that I do not understand. Could I ask you to explain in short what are they?
After the suggestions of #Medinoc, #someuser and #Elchonon I corrected my code in the following way:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#define SERVER_PORT 80
int nStatus, nMainSocketId;
void onInt () {
printf("You have pressed CTRL-C.\n");
nStatus = 0;
shutdown(nMainSocketId, 2);
}
void onQuit () {
printf("You have pressed CTRL-\\.\n");
nStatus = 0;
shutdown(nMainSocketId, 2);
}
int main () {
int nReqSocketId, nReqSize = 1024;
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
char *sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress;
signal(SIGINT, onInt);
signal(SIGQUIT, onQuit);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nStatus = 1;
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
if (nMainSocketId == 0) {
fprintf(stderr, "Error during the creation of the socket\n");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
fprintf(stderr, "The port %d is busy\n", SERVER_PORT);
close(nMainSocketId);
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (nStatus) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen");
close(nMainSocketId);
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (nReqSocketId < 0) {
perror("server: accept");
close(nMainSocketId);
return 1;
}
recv(nReqSocketId, sRequest, nReqSize, 0);
if (nReqSocketId > 0){
printf("The Client is connected...\n\n%s\n", sRequest);
}
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
printf("Goodbye!\n");
close(nMainSocketId);
return 0;
}
The process sof-terminates, now!! But, unfortunately, the socket no! :( …so I get the following message whenever I press CTRL-C or CTRL-\ in order to quit:
server: accept: Invalid argument
Any suggestion to fix it?
EDIT
#alk
Thank you for your answer! You suggested me an alternative way to soft-terminate my webserver...:
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#define SERVER_PORT 80
int bExiting, nMainSocketId;
void terminateServer () {
if (bExiting) { return; }
printf("\n\nTerminating server...\n");
close(nMainSocketId);
bExiting = 1;
}
int main () {
int nRecvResult = -1, nReqSocketId = -1;
size_t nReqSize = 1024;
char * sRequest = malloc(nReqSize);
socklen_t nAddrLen;
struct sockaddr_in oAddress = {0};
printf("\n W E L C O M E!\n\nPress CTRL-C or CTRL-\\ to quit.\n");
signal(SIGINT, terminateServer);
signal(SIGQUIT, terminateServer);
oAddress.sin_family = AF_INET;
oAddress.sin_addr.s_addr = INADDR_ANY;
oAddress.sin_port = htons(SERVER_PORT);
nMainSocketId = socket(AF_INET, SOCK_STREAM, 0);
if (nMainSocketId < 0) {
perror("server: socket() failed");
return 1;
}
if (bind(nMainSocketId, (struct sockaddr *) &oAddress, sizeof(oAddress))) {
perror("server: bind() failed");
terminateServer();
return 1;
}
printf("HTTP server listening on port %d\n", SERVER_PORT);
while (bExiting == 0) {
if (listen(nMainSocketId, 10) < 0) {
perror("server: listen() failed");
terminateServer();
return 1;
}
nReqSocketId = accept(nMainSocketId, (struct sockaddr *) &oAddress, &nAddrLen);
if (bExiting) { break; }
if (nReqSocketId < 0) {
perror("server: accept failed()");
terminateServer();
return 1;
}
nRecvResult = recv(nReqSocketId, sRequest, nReqSize, 0);
if (nRecvResult < 0) {
perror("server: recv() failed");
terminateServer();
return 1;
}
if (nRecvResult == 0) {
printf("The client is disconnected. Waiting for another connection...\n");
close(nReqSocketId);
continue;
}
printf("The client is connected...\n\n%s\n", sRequest);
/* This is only a simple "Hello world"... */
write(nReqSocketId, "HTTP/1.1 200 OK\n", 16);
write(nReqSocketId, "Content-length: 50\n", 19);
write(nReqSocketId, "Content-Type: text/html\n\n", 25);
write(nReqSocketId, "<html><body><h1>Hello world!!!</h1></body></html>\n", 50);
close(nReqSocketId);
}
free(sRequest);
printf("Goodbye!\n");
return 0;
}
What do you think about it? Should I add something?
Still, I have yet another question. Can I can use fprintf() in conjunction with a file descriptor as alternative to write()? And... how?

Problems while implementing HTTP 1.0 server

I am trying to implement a simple HTTP server with C that
reads a request
checks if it is a GET request
reads the URL from the request
Checks if file is on server and tries to open it
I am using strtok for String tokenizing and I think it messes up the filepath. open and fopen always return error codes and are not able to open any files.
Here is my code:
/*
** parser.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.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 3499 // the port users will be connecting to
#define BACKLOG 10 // how many pending connections queue will hold
#define MAXLEN 1024 //upper limit of the length of the string
int main(void)
{
char input[MAXLEN]; //the line that is read from the client
char * token1; //GET request
char * token2; //filepath
char tmpstring[MAXLEN]; //filesize
int sockfd, new_fd; // listen on sock_fd, new connection on new_fd, file open on file_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int sin_size;
int yes=1;
int n; //the amount of read characters from the client
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
if (listen(sockfd, BACKLOG) == -1) {
perror("listen");
exit(1);
}
while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in);
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
n = readline(new_fd, input, MAXLEN); //n is the amount of read characters
if (n == -1) {
perror("Unable to read line");
}
//Check if it is a GET message
token1 = strtok(input," ");
if(strcmp(token1, "GET") != 0)
{
send(new_fd, "Bad request\n", 30, 0);
}
else
{
//Retrieve the file path
token2 = strtok(NULL, " ");
if(token2 == NULL)
{
send(new_fd, "File path not specified\n", 23, 0); //Check if filename is empty
}
send(new_fd, token2, strlen(token2), 0); //test
printf("%s", token2);
if(token2[0] == '/') //remove the initial slash
memmove(token2, token2 + 1, strlen(token2));
//char * path = "test.html"; //test line
//char * buff;
//int len = sprintf(buff, "1: %d 2: %d\n", strlen(token1), strlen(token2));
//send(new_fd, buff, len, 0);
//Check if file is on the server
if(open(token2, O_RDONLY) < 0) //Error opening file
{
if(errno == EACCES)
send(new_fd, "Access error\n", 30, 0);
else
send(new_fd, "Not existed\n", 30, 0);
}
else
{
FILE * requested_file = fopen(token2, "r");
if(requested_file == NULL) //
{
send(new_fd, "Error in fopen\n", 30, 0);
}
else
{
send(new_fd, "File found\n", 30, 0); //successful
}
fseek(requested_file, 0, SEEK_END); // move to the end of the file
int end= ftell(requested_file); // get the position of the end of file
int stringlen = sprintf(tmpstring, "file size: %d\n", end);
send(new_fd, tmpstring, stringlen, 0);
}
}
close(new_fd); //close connection
}
return 0;
}
//helper function for recieving text
int readline(int fd, char *buf, int maxlen)
{
int n, rc;
char c;
for (n = 1; n < maxlen; n++) {
if ((rc = read(fd, &c, 1)) == 1) {
*buf++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
if (n == 1)
return 0; // EOF, no data read
else
break; // EOF, read some data
} else
return -1; // error
}
*buf = '\0'; // null-terminate
return n;
}
So I'm placing a test.html in the same folder as the server. Then im telnetting to localhost and port 3499. This is the output when entering GET /test.html:
/test.html
Not existed
rError in fopen
Connection closed by foreign host.
try opening "test.html" instead of "\test.html"

Resources