LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (http://www.linuxquestions.org/questions/linux-networking-3/)
-   -   hi help me regarding socket and timer programming (http://www.linuxquestions.org/questions/linux-networking-3/hi-help-me-regarding-socket-and-timer-programming-880794/)

shakthi 05-15-2011 02:30 AM

hi help me regarding socket and timer programming
 
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.

TB0ne 05-15-2011 11:40 AM

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.

shakthi 05-15-2011 10:51 PM

hey thats not same question, see properly i have added before for writing in file error.. now i got timer error..... see the full code properly...

CodeKrash 05-16-2011 01:00 PM

n o offense but trying to understand your problem with just a glance of garbled, non formatted code makes me sad.

shakthi 05-17-2011 12:57 AM

i dont know to format dude.. i think there is problem in alarmfunc().. kindly help me..

CodeKrash 05-17-2011 08:15 AM

your best bet is to create a standalone exe, and step through the function with a debugger

Nominal Animal 05-17-2011 04:15 PM

Quote:

Originally Posted by shakthi (Post 4358477)
i dont know to format dude..

You know perfectly well how to format your code, because you did it in your very first message.

shakthi 05-19-2011 11:24 PM

/*tcpserver.c*/

#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..

shakthi 05-19-2011 11:27 PM

Code:

/* tcpserver.c */

#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..


All times are GMT -5. The time now is 07:46 AM.