[SOLVED] hi help me regarding socket and timer programming
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi i have a server program which accept multiple client connection and am using polling.like every 2 secs it will look to client whether any data is received after it binded. i have used setitimer but there is runtime error i got.. the server accept all client connection but doesn't execute any msg which client sent.. please help me as soon as possible.. here my code...
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<time.h>
#include<sys/time.h>
#include<signal.h>
pthread_t server__thread1;
void *server_threadfunc1(void*);
void alarmfunc();
int server_socketid;
struct sockaddr_in server_ipaddr,server_clientaddr;
FILE *fp;
int *server_clientid,server_bindid;
int main(int argc,char *argv[])
{
int server_portno;
char msg[1000];
socklen_t server_clen=sizeof(server_clientaddr);
if(argc<2)
{
printf("\n please give portno");
exit(0);
}
server_portno=atoi(argv[1]);
server_socketid=socket(AF_INET,SOCK_STREAM,0);
if(server_socketid!=-1)
{
printf("\n server: Socket is created successfully");
printf("\n %d",server_socketid);
}
else
printf("\nServer: socket is not created");
server_ipaddr.sin_family=AF_INET;
server_ipaddr.sin_addr.s_addr=htonl(INADDR_ANY);
server_ipaddr.sin_port=htons(server_portno);
server_bindid=bind(server_socketid,(struct sockaddr*)&server_ipaddr,sizeof(server_ipaddr));
if(server_bindid==0)
printf("server:binded to client successfully\n");
else
printf("\n server not binded to client\n");
if(listen(server_socketid,10)==-1)
printf("error in listen");
else
printf("\n server listen to client\n");
while(1)
{
printf("\n waiting for the connection from the client");
server_clientid=(int*)malloc(sizeof(int));
if((*server_clientid=accept(server_socketid,(struct sockaddr*)&server_clientaddr,&server_clen))!=-1)
{
printf("\n server:received connection from is %s",inet_ntoa(server_clientaddr.sin_addr));
pthread_create(&server__thread1,NULL,(void*)&server_threadfunc1,(void*)server_clientid);
pthread_detach(server__thread1);
}
else
{
printf("error in creating");
}
}
close(server_socketid);
return 0;
}
void *server_threadfunc1(void *server_id)
{
struct itimerval ts;
int *server_id1=(int*)server_id;
ts.it_interval.tv_sec=0;
ts.it_interval.tv_usec=0;
ts.it_value.tv_sec=2;
ts.it_value.tv_sec=0;
setitimer(ITIMER_REAL,&ts,0);
signal(SIGALRM,alarmfunc);
while(1)
{
}
close(server_socketid);
close(*server_clientid);
}
if setitimer concept is wrong, please suggest me some other system call to execute this program... my concept is, every 2 sec server should lookup for client whether they send any message, if it is then it have to display else not... please help me.
Don't post the same question twice, in different forums. And maybe if you actually told us the error(s) you're getting, and provided details (version/distro of Linux, etc.), someone could help.
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<time.h>
#include<sys/time.h>
#include<signal.h>
pthread_t server__thread1[100];
int t=0;
void *server_threadfunc1();
void alarmfunc();
int server_socketid;
int *server_id1;
struct sockaddr_in server_ipaddr,server_clientaddr;
FILE *fp;
int i=0;
int server_clientid,server_bindid;
int main(int argc,char *argv[])
{
int server_portno;
char msg[1000];
socklen_t server_clen=sizeof(server_clientaddr);
if(argc<2)
{
printf("\n please give portno");
exit(0);
}
server_portno=atoi(argv[1]);
server_socketid=socket(AF_INET,SOCK_STREAM,0);
if(server_socketid!=-1)
{
printf("\n server: Socket is created successfully");
printf("\n %d",server_socketid);
}
else
printf("\nServer: socket is not created");
server_ipaddr.sin_family=AF_INET;
server_ipaddr.sin_addr.s_addr=htonl(INADDR_ANY);
server_ipaddr.sin_port=htons(server_portno);
server_bindid=bind(server_socketid,(struct sockaddr*)&server_ipaddr,sizeof(server_ipaddr));
if(server_bindid==0)
printf("server:binded to client successfully\n");
else
printf("\n server not binded to client\n");
if(listen(server_socketid,10)==-1)
printf("error in listen");
else
printf("\n server listen to client\n");
while(1)
{
printf("\n waiting for the connection from the client");
if((server_clientid=accept(server_socketid,(struct sockaddr*)&server_clientaddr,&server_clen))!=-1)
{
printf("\n server:received connection from is %s",inet_ntoa(server_clientaddr.sin_addr));
t=t+1;
pthread_create(&server__thread1[t],NULL,(void*)&server_threadfunc1,NULL);
pthread_detach(server__thread1[t]);
}
else
{
printf("error in creating");
}
}
close(server_socketid);
return 0;
}
void *server_threadfunc1()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = 8;
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL,&tout_val,0);
signal(SIGALRM,alarmfunc);
while(1)
{
}
close(server_socketid);
close(server_clientid);
}
void alarmfunc()
{
char server_chat_msg[1000];
char msg3[1000]=" I got your msg";
struct itimerval ct;
recv(server_clientid,server_chat_msg,sizeof(server_chat_msg),MSG_DONTWAIT);
printf("\n received msg is %s",server_chat_msg);
send(server_clientid,msg3,sizeof(msg3),0);
ct.it_interval.tv_sec=0;
ct.it_interval.tv_usec=0;
ct.it_value.tv_sec=4;
ct.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&ct,0);
}
the above one is my code.. working only fine.. but my problem is, when it starts to accept more than one client, old client cannot able to send data to new client..at a time it bind only with new client... please rectify my problem as possible..
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<unistd.h>
#include<pthread.h>
#include<string.h>
#include<time.h>
#include<sys/time.h>
#include<signal.h>
pthread_t server__thread1[100];
int t=0;
void *server_threadfunc1();
void alarmfunc();
int server_socketid;
int *server_id1;
struct sockaddr_in server_ipaddr,server_clientaddr;
FILE *fp;
int i=0;
int server_clientid,server_bindid;
int main(int argc,char *argv[])
{
int server_portno;
char msg[1000];
socklen_t server_clen=sizeof(server_clientaddr);
if(argc<2)
{
printf("\n please give portno");
exit(0);
}
server_portno=atoi(argv[1]);
server_socketid=socket(AF_INET,SOCK_STREAM,0);
if(server_socketid!=-1)
{
printf("\n server: Socket is created successfully");
printf("\n %d",server_socketid);
}
else
printf("\nServer: socket is not created");
server_ipaddr.sin_family=AF_INET;
server_ipaddr.sin_addr.s_addr=htonl(INADDR_ANY);
server_ipaddr.sin_port=htons(server_portno);
server_bindid=bind(server_socketid,(struct sockaddr*)&server_ipaddr,sizeof(server_ipaddr));
if(server_bindid==0)
printf("server:binded to client successfully\n");
else
printf("\n server not binded to client\n");
if(listen(server_socketid,10)==-1)
printf("error in listen");
else
printf("\n server listen to client\n");
while(1)
{
printf("\n waiting for the connection from the client");
if((server_clientid=accept(server_socketid,(struct sockaddr*)&server_clientaddr,&server_clen))!=-1)
{
printf("\n server:received connection from is %s",inet_ntoa(server_clientaddr.sin_addr));
t=t+1;
pthread_create(&server__thread1[t],NULL,(void*)&server_threadfunc1,NULL);
pthread_detach(server__thread1[t]);
}
else
{
printf("error in creating");
}
}
close(server_socketid);
return 0;
}
void *server_threadfunc1()
{
struct itimerval tout_val;
tout_val.it_interval.tv_sec = 0;
tout_val.it_interval.tv_usec = 0;
tout_val.it_value.tv_sec = 8;
tout_val.it_value.tv_usec = 0;
setitimer(ITIMER_REAL,&tout_val,0);
signal(SIGALRM,alarmfunc);
while(1)
{
}
close(server_socketid);
close(server_clientid);
}
void alarmfunc()
{
char server_chat_msg[1000];
char msg3[1000]=" I got your msg";
struct itimerval ct;
recv(server_clientid,server_chat_msg,sizeof(server_chat_msg),MSG_DONTWAIT);
printf("\n received msg is %s",server_chat_msg);
send(server_clientid,msg3,sizeof(msg3),0);
ct.it_interval.tv_sec=0;
ct.it_interval.tv_usec=0;
ct.it_value.tv_sec=4;
ct.it_value.tv_usec=0;
setitimer(ITIMER_REAL,&ct,0);
}
the above one is my code.. working only fine.. but my problem is, when it starts to accept more than one client, old client cannot able to send data to new client..at a time it bind only with new client... please rectify my problem as possible..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.