Problem in Writing into the file..using fputs() call

X

xoinki

hi experts,
I need a little help in debugging this code.. would u
pleeze kindly help me?
here this program sends a datagram every 10 seconds and on reception it
cheks whether the source IP is already written in file or not.. if it
is not already entered into the file then a file is opened and that IP
is entered.
the problem is with this part..
if the filewrite() function is tested independently it is
working but in this program eventhough fputs() is returning success,
the required data is not written into the file.
how could this happen? is this related to blocking and nonblocking I/o?

i request u to kindly help me..
thanking u in advance



#include <curses.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>

#define PACKETSIZE 64

struct hostent *hname;
struct sockaddr_in addr;
int i;
int j=0;
fd_set rset;
void ping(int);
int cont=-1,arr[20];
int cnt=1;
int flag=0;
struct icmphdr1
{
u_int8_t type; /* message type */
u_int8_t code; /* type sub-code */
u_int16_t checksum;
int priority;
};

struct icmphdr1 *icmp;

struct prio
{
int priority;
char ip1[20];
};

struct prio arr1[10];

struct packet
{
struct icmphdr1 hdr;
char msg[PACKETSIZE-sizeof(struct icmphdr1)];
};

int pid=-1;
struct protoent *proto=NULL;

/*--------------------------------------------------------------------*/
/*--- checksum - standard 1s complement checksum
---*/
/*--------------------------------------------------------------------*/
unsigned short checksum(void *b, int len)
{ unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;

for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
void sort(int js)
{
int i;
int j;

for (i = 0; i < js - 1; ++i) /* bubble sort */
for (j = js - 1; j > i; --j)
if (arr1[j-1].priority > arr1[j].priority)
{ /* check the order */
int tmp = arr1[j-1].priority;
arr1[j-1].priority = arr1[j].priority;
arr1[j].priority = tmp;
}
printf("the sorted array are...");
for (i = 0; i < js; ++i)
printf("%d\t",arr1.priority);
}


/*THIS IS WHRERE THE PROBLEM OF WRITING EXISTS
************************/

int filewrite(char *arr,char *arr1)
{
FILE *fp;
printf("\nopening %s\n",arr);
fp=fopen(arr,"w") ;
printf("\nthe sent string is %s\n",arr1);
if(fputs("sdf",fp)!=EOF)
return 1;
else
return 0;
}

/* the file searchin operation done here *
* */
int filesear(char a[])
{
FILE *fp;
char cs[10];
char c;
int co=0,i,n,lflag=0;
char arr[20];

strcpy(arr,a);
printf("\nthe string recieved is %s\n",arr);
fp=fopen("helo1.txt","r");

while((c=getc(fp))!=EOF && n!=0)
{
lflag=1;
if((cs[co]=c)==' '|| cs[co]=='\n')
{
cs[co]='\0';
printf("\nthe cs is %s\n",cs);
if((n=strcmp(cs,arr))==0)
printf("\nequal\n");

for(i=0;i<co;i++)
cs=' ';
co=-1;
}

co++;

}
printf("\nafter while loop %s\t %d\n",cs,lflag);
printf("\nthe n=%d",n);
if(n==0)
return 0;
else
return 1;
}

/*the election algorithm must run *
* here */

//void callelec()
//{

//}


/*----------------------------------------------------------------*/
/*--- display - present echo info
---*/
/*--------------------------------------------------------------------*/
void display(int sd,void *buf, int bytes)
{ int i;
struct iphdr *ip= buf;
struct icmphdr1 *icmp = buf+ip->ihl*4;

printf("----------------\n");
fcntl(sd, F_SETFL, O_NONBLOCK);

switch(icmp->type)
{

case 0:
arr1[j].priority=icmp->priority;
strcpy(arr1[j].ip1,inet_ntoa(ip->daddr));

printf("\n\nthe prio is %d\tthe ip is
%s\n\n",arr1[j].priority,arr1[j].ip1);
j++;
if(j==5)
sort(5);
//callelec();
break;

default:
break;

}

//continue;

printf("\n");
printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
ip->ttl, inet_ntoa(ip->saddr));
printf("dst=%s\n", inet_ntoa(ip->daddr));
//if ( icmp->un.echo.id == pid )
{
printf("ICMP: type[%d/%d] checksum[%d] priority[%d] [%d]\n",
icmp->type, icmp->code, ntohs(icmp->checksum),icmp->priority,j);
}
}

/*--------------------------------------------------------------------*/
/*--- listener - separate process to listen for and collect
messages--*/
/*--------------------------------------------------------------------*/
void listener(void)
{ int sd;
struct sockaddr_in addr;
unsigned char buf[1024];
FILE *fp;
void *buf1;
buf1=buf;
struct icmphdr1 *icp;
struct iphdr *ip=buf1;
char c;
struct timeval tv;
sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
//arr[j++]=icp->priority;
//printf("the prio in isteneer %d\n",arr[j]);

//callprint(arr[j]);

if ( sd < 0 )
{
perror("socket");
exit(0);
}
for (;;)
{
int bytes, len=sizeof(addr);
int m;
tv.tv_sec=10;
tv.tv_usec=0;

bzero(buf, sizeof(buf));
FD_ZERO(&rset);
FD_SET(sd,&rset);

m=select(sd+1,&rset,NULL,NULL,&tv);

printf("the size of m is %d\n",m);

if(m==1)
{
bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr,
&len);
printf("recived\n");
if ( bytes > 0 )
{
int a,b;
printf("the before searchin\n");
a=filesear((char *)inet_ntoa(ip->saddr));
printf("after \n");
if(a==1)
{
b=filewrite("helo",(char *)inet_ntoa(ip->saddr));
if(b)
printf("\n WRITten\n");
else
printf("error\n");
}
else
{
printf("\ndointnotjjas\n");
}
display(sd, buf, bytes);
//printf("yess\n");
}
else
perror("recvfrom");
}
else
{
// char c='1';
FILE *fp;
char c;
int oldopts;
fp=fopen("helo.txt","w");

c='a';
putc(c,fp);

fclose(fp);
printf("asdfasdfasdfasdafsdf\n");
oldopts = fcntl(sd, F_GETFL, 0);
fcntl(sd, F_SETFL, oldopts | O_NONBLOCK);
}
}
exit(0);
}

/*--------------------------------------------------------------------*/
/*--- ping - Create message and send it.
---*/
/*--------------------------------------------------------------------*/
void ping(int a)
{ const int val=1;
int i, sd;
struct packet pckt;
struct sockaddr_in r_addr;

// proto = getprotobyname("OSPFIGP");
alarm(10);

bzero(&addr, sizeof(addr));
addr.sin_family = hname->h_addrtype;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(long*)hname->h_addr;

sd = socket(PF_INET, SOCK_RAW, proto->p_proto);

if ( sd < 0 )
{
perror("socket");
return;
}
if ( setsockopt(sd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) != 0)
perror("Set TTL option");
if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
perror("Request nonblocking I/O");
//for (;;)
{ int len=sizeof(r_addr);

printf("Msg #%d\n", cnt++);
if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr,
&len) > 0 )
printf("***Got message!***\n");
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = 0;
//pckt.hdr.un.echo.id = pid;
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg = i+'0';
pckt.msg = 0;
//pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
pckt.hdr.priority=rand()%10;

// printf("hi helo\n");
if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr,
sizeof(addr)) <= 0 )
perror("sendto");
cont++;
// printf("%d ji\n",cont);
//sleep(1);
}
}

/*--------------------------------------------------------------------*/
/*--- main - look up host and start ping processes.
---*/
/*--------------------------------------------------------------------*/
int main(int count, char *strings[])
{
/*struct hostent *hname;
struct sockaddr_in addr;*/

if ( count != 2 )
{
printf("usage: %s <addr>\n", strings[0]);
exit(0);
}
if ( count > 1 )
{
pid = getpid();
signal(SIGALRM,ping);
pid = getpid();
proto = getprotobyname("OSPFIGP");
hname = gethostbyname(strings[1]);
/*bzero(&addr, sizeof(addr));
addr.sin_family = hname->h_addrtype;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(long*)hname->h_addr;
signal(SIGALRM,send1); */
if ( fork() == 0 )
listener();
else
ping(2);
while(1)
{
pause();
}

}
else
printf("usage: myping <hostname>\n");
return 0;
}
 
R

rageratwork

In filewrite() you open the file but do not ever close it. The file
output is likely buffered and not written to the file until the buffer
is flushed (when the file is closed or fflush is called explicitly).

If you close the file before filewrite exits you will see the output
which is what I think you want to do.

If you really don't mean to close the file atleast call fflush.

Dave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top