LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 05-15-2011, 02:30 AM   #1
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Rep: Reputation: 0
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.
 
Old 05-15-2011, 11:40 AM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,767

Rep: Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613Reputation: 2613
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.
 
Old 05-15-2011, 10:51 PM   #3
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
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...
 
Old 05-16-2011, 01:00 PM   #4
CodeKrash
LQ Newbie
 
Registered: May 2011
Posts: 21

Rep: Reputation: 1
n o offense but trying to understand your problem with just a glance of garbled, non formatted code makes me sad.
 
Old 05-17-2011, 12:57 AM   #5
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
i dont know to format dude.. i think there is problem in alarmfunc().. kindly help me..
 
Old 05-17-2011, 08:15 AM   #6
CodeKrash
LQ Newbie
 
Registered: May 2011
Posts: 21

Rep: Reputation: 1
your best bet is to create a standalone exe, and step through the function with a debugger
 
Old 05-17-2011, 04:15 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by shakthi View Post
i dont know to format dude..
You know perfectly well how to format your code, because you did it in your very first message.
 
Old 05-19-2011, 11:24 PM   #8
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
/*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..
 
Old 05-19-2011, 11:27 PM   #9
shakthi
LQ Newbie
 
Registered: May 2011
Posts: 11

Original Poster
Rep: Reputation: 0
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..
 
  


Reply

Tags
networking, server, socket, timer


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
timer in socket programming rrockz Programming 3 03-10-2011 10:59 AM
Can anybody know about configuring socket timer to release socket linjoy Linux - Networking 1 06-19-2010 03:43 AM
HPET timer programming in C sarans1987 Linux - Software 3 09-08-2009 09:38 AM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration