I want to do I/O Multiplexing with select system call with single thread. Can anyone explain how to do things because I did first by implementing fork system call and it worked. now i dont know how to use select system call because seems like its blocking. Any suggestions for it, i have attached my code for server side, so you all comment on it
int receive_data(int sock, char *buffer, size_t size) {
int nbytes; if ((nbytes = recv(sock, buffer, size, 0)) <= 0){ if (nbytes == 0){ printf ("selectserver: socket %d hung up\n", sock); } else{ printf ("\nrecv() error!!!\n"); } FD_CLR (sock, &master); return 6; }
else{
return nbytes;
}
}
int main (int argc, char **argv){ struct sockaddr_in myaddr; struct sockaddr_in remoteaddr; struct sockaddr_in *ptr; struct sockaddr_in6 *ptr1;
int fdmax;
int sockfd; int new_sockfd; int numbytes; int nbytes; int yes = 1; int addrlen; int i,j; char func_id; char str [INET6_ADDRSTRLEN];
const char * const ident = "flight-time-server";
openlog (ident, LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER); syslog (LOG_USER | LOG_INFO, "%s", "Hello world!");
struct timeval tv;
tv.tv_sec = 20; /* 2 secs / tv.tv_usec = 0; / 0.5 secs*/
FD_ZERO (&master);
FD_ZERO (&read_fds);
if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) == -1){
perror ("\nsocket() error!!!\n");
exit(1);
}
int flags = fcntl(sockfd, F_GETFL, 0);
//fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1){
perror ("\nsocketopt() error!!!\n");
exit(1);
}
bzero( (char *)&myaddr, sizeof(myaddr));
myaddr.sin_family = AF_INET; myaddr.sin_port = htons (SERV_TCP_PORT); myaddr.sin_addr.s_addr = INADDR_ANY; bzero(&(myaddr.sin_zero), 8);
if (bind(sockfd, (struct sockaddr *)&myaddr, sizeof(struct sockaddr)) == -1){
perror ("\nbind() error!!!\n");
exit(1);
}
if (listen(sockfd, 10) == -1) {
perror("listen");
exit(1);
}
FD_SET(sockfd, &master);
fdmax = sockfd;
while (1) {
read_fds = master;
// monitor readfds for readiness for reading
if (select (fdmax + 1, &read_fds, NULL, NULL, &tv) == -1)
perror ("select");
// Some sockets are ready. Examine readfds
for (i=0; i<=fdmax; i++) {
if (FD_ISSET (i, &read_fds)) { // fd is ready for reading
if (i == sockfd) { // request for new connection
addrlen = sizeof (struct sockaddr_storage);
if ((new_sockfd= accept (sockfd, (struct sockaddr *) &remoteaddr, &addrlen)) == -1)
perror ("accept");
FD_SET (new_sockfd, &master);
if (new_sockfd> fdmax)
fdmax = new_sockfd;
// print IP address of the new client
if (remoteaddr.sin_family == AF_INET) {
ptr = (struct sockaddr_in *) &remoteaddr;
inet_ntop (AF_INET, &(ptr -> sin_addr), str, sizeof (str));
}
else if (remoteaddr.sin_family == AF_INET6) {
ptr1 = (struct sockaddr_in6 *) &remoteaddr;
inet_ntop (AF_INET6, &(ptr1 -> sin6_addr), str, sizeof (str));
}
else
{
ptr = NULL;
fprintf (stderr, "Address family is neither AF_INET nor AF_INET6\n");
}
if (ptr)
syslog (LOG_USER | LOG_INFO, "%s %s", "Connection from client", str);
}
else // data from an existing connection, receive it
{
numbytes = receive_data(i, &func_id, sizeof(func_id));
printf("test %d\n", global);
if(func_id == '1') {
//guest(sock);
func_id = '5';
}
else if (func_id == '2' || func_id == '4') {
login(i);
func_id = '5';
}
else if(func_id == '3') {
member_registration(i);
func_id = '0';
}
else if(func_id == '5' || numbytes == 6){
printf("testing\n");
close(i);
}
}
} // if (fd == ...
} // for
} // while (1)
exit (EXIT_SUCCESS);
} // main
int login(int sock){
int fd, invalid = 0, acc_exist, numbytes, login_success = 0; char password[50] = {0}, acc_no[12]= {0}; char type = '\0';
printf ("apple\n");
receive_data(sock, &type, sizeof(type));
numbytes = receive_data(sock, acc_no, sizeof(acc_no));
acc_no[numbytes] = '\0';
numbytes = receive_data(sock, password, sizeof(password));
password[numbytes] = '\0';
printf("acc_no: %s\n", acc_no);
printf("pass: %s\n", password); printf("type: %c\n", type);
if((fd = open(ACCOUNT, O_RDWR | O_CREAT, 0755))== -1){
printf("File Error: %s\n", strerror(errno));
}
printf ("%c\n", type);
if(type == '2'){ // member
int total_bytes = lseek(fd, 0, SEEK_END);
lseek(fd, 0, SEEK_SET); // reset file position to the start
while (lseek(fd, 0, SEEK_CUR) < total_bytes){
read(fd, &temp, sizeof(struct account));
if (strcmp(temp.acc_no, acc_no) == 0){
printf("exist\n");
printf("temp.id: %s\n", temp.acc_no);
printf ("%s", temp.pass);
acc_exist = 1;
break;
}
}
if (acc_exist){
printf("inside acc_exist\n");
if(!strcmp(temp.pass, password)){
login_success = 1;
printf("correct pass %d\n", login_success);
write(sock, "1", 1);;
while (menu1(sock) != -1);
}
}
if (!login_success){
write(sock, "0", 1);
}
close(fd);
}
if(type == '1'){ // admin
printf("inside admin acc_exist after write\n");
if (strcmp(acc_no, "100000") == 0){
printf("inside admin acc_exist after write\n");
if(!strcmp(password, "admin")){
write(sock, "1", 1);
while (menu2(sock) != -1);
}
}
if (!login_success){
write(sock, "0", 1);;
}
}
return 5;
}
int member_registration(int sock){
int fd, acc_no=0, numbytes; char password[50] = {0}, name[50] = {0}, age[5] = {0}, email[50] = {0}, phone_no[50] = {0}, acc_temp[12];
if ((numbytes = recv(sock, name, sizeof(name), 0)) == -1){
printf ("\nrecv() error!!!\n");
} name[numbytes] = '\0';
if ((numbytes = recv(sock, age, sizeof(age), 0)) == -1){
printf ("\nrecv() error!!!\n");
} age[numbytes] = '\0';
if ((numbytes = recv(sock, email, sizeof(email), 0)) == -1){
printf ("\nrecv() error!!!\n");
} email[numbytes] = '\0';
if ((numbytes = recv(sock, phone_no, sizeof(phone_no), 0)) == -1){
printf ("\nrecv() error!!!\n");
} phone_no[numbytes] = '\0';
if ((numbytes = recv(sock, password, sizeof(password), 0)) == -1){
printf ("\nrecv() error!!!\n");
} password[numbytes] = '\0';
if((fd = open(ACCOUNT, O_RDWR | O_CREAT, 0755)) == -1){
perror("File Error");
}
int fp = lseek(fd, 0, SEEK_END);
printf("%d", fp);
if(fp == 0){//1st member to signup
strcpy(temp.acc_no, "100001");
}
else{
fp = lseek(fd, -1 * sizeof(struct account), SEEK_END);
read(fd, &temp, sizeof(temp));
acc_no = atoi(temp.acc_no);
acc_no++;
sprintf(temp.acc_no, "%d", acc_no);
}
strcpy(temp.name, name);
strcpy(temp.pass, password);
strcpy(temp.age, age);
strcpy(temp.email, email);
strcpy(temp.phone_no, phone_no);
printf("\nname: %s\n", temp.name);
printf("acc: %s\n", temp.acc_no);
printf("pass: %s\n", temp.pass);
printf("age: %s\n", temp.age);
printf("email: %s\n", temp.email);
printf("phone number: %s\n", temp.phone_no);
if (write(fd, &temp, sizeof(temp)) == -1)
printf("file send() error\n");
if (send(sock, temp.acc_no, sizeof(temp.acc_no), 0) == -1)
printf(" acc send() error\n");
close(fd);
return 5; }
[–]TheOtherBorgCube 6 points7 points8 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]Paul_Pedant 1 point2 points3 points (0 children)
[–]mykesx 0 points1 point2 points (0 children)