November 20, 2017, 11:00:35 PM

News : LinuxSolved.com Linux Help Community Forum..


Author Topic: hi help me regarding socket and timer programming  (Read 1641 times)

Offline shakthi

  • New Member
  • Posts: 1
hi help me regarding socket and timer programming
« on: May 16, 2011, 04:01:11 AM »
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);
}

void alarmfunc()
{
struct itimerval ms;
signal(SIGALRM,alarmfunc);
int i=0;
time_t mytime;
mytime=time(NULL);
fp=fopen("modelfile1.txt","a");
fprintf(fp,"\n\t%s \t\t\t %s\t\t\t%s","client_address","date","client_msg");
fclose(fp);
char server_chat_msg[1000],server_client_msg1[1000];
char server_date_msg2[1000];
char msg3[1000]="i got your msg";
printf("\n %s",msg3);
ms.it_interval.tv_sec=0;
ms.it_interval.tv_usec=0;
ms.it_value.tv_sec=2;
ms.it_value.tv_sec=0;
fp=fopen("modelfile1.txt","a");
int bytesrec=recv(*server_clientid,server_chat_msg,sizeof(server_chat_msg),0);
printf("\n server:received string is %s\n",server_chat_msg);
fprintf(fp,"\n\t");
strcpy(server_client_msg1,inet_ntoa(server_clientaddr.sin_addr));
fwrite(server_client_msg1,strlen(server_client_msg1),1,fp);
fprintf(fp,"\t");
strcpy(server_date_msg2,ctime(&mytime));
fwrite(server_date_msg2,strlen(server_date_msg2),1,fp);
fwrite(server_chat_msg,strlen(server_chat_msg),1,fp);
send(*server_clientid,msg3,sizeof(msg3),0);
fclose(fp);
setitimer(ITIMER_REAL,&ms,0);
signal(SIGALRM,alarmfunc);
}


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.