fseek()

E

ericunfuk

I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my sender
app, the sender and the receiver are both on the same machine and I
have an Internet emulation gateway running on the same machine as
well.

What I'm confused about is I can't detect when the end of the file I'm
sending has been reached, my program seems to be running forever.

Bascially the window[] in my code fragment is generated by the
receiver app, it contains the sequence numbers of packets, a zero
value in window indicates that packet has been successfully received
by the receiver, non-zero values indicate the sequence numbers of
packets that were not received by the receiver, thus need to be resent
by the sender app.

So I need to use fseek() to seek through the file, using the values in
window[] as offsets, but my receiver is stupid it doesn't know the
biggest possible sequence number, it just asks for greater sequence
numbers if previous smaller sequence numbers have all been received,
so in window[] it might contain some sequence number that when used as
offset to fseek(), fseek() will seek beyond the actual end of the
stream representing the file(I maybe wrong here becasue of my poor
understanding of stream, file etc..), I tried to find the size of the
file first then try to stop invoke fseek() , if offset times packet
size is greater than my file_size, but that didn't stop the pain, it
still runs forever.

Thank you all for any help you could provide.

Eric
..........
...........
while(last_packet_acked == 0){

for(i=0;i<win_size;i++){
if(window==0){continue;}

offset = window-1;
printf("size:%u\n",offset*5);

if(offset*5 <= file_size){fseek(file,offset,SEEK_SET);}
if((bytes_read=fread(packet.data,1,sizeof(packet.data),file))!=0)
{
packet.client_seq_no = htonl(window);
n = sendto(sock,&packet,sizeof(struct dgram),0,(struct sockaddr
*)&gateway,sizeof(struct sockaddr_in));
if(n<0) error("Error sendto");
printf("SENT PACKET:%u\n",window);

}else{
packet.client_seq_no = htonl(window);
packet.flag = htons(FIN);
n = sendto(sock,&packet,sizeof(struct dgram),0,(struct
sockaddr *)&gateway,sizeof(struct sockaddr_in));
if(n<0) error("Error sendto");
printf("SENT PACKET:%u\n",window);
break;
}

}
...............
...............
 
M

Mark McIntyre

I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my sender

For the sockets part I suggest you ask again in comp.unix.programming,
where sockets are topical.
What I'm confused about is I can't detect when the end of the file I'm
sending has been reached, my program seems to be running forever.
while(last_packet_acked == 0){
The outer while loop runs as long as last_packet_acked is zero, where
do you set it to nonzero?
if(offset*5 <= file_size){fseek(file,offset,SEEK_SET);}

how did you determine file_size? Also, whats that brace doing at the
end of the line?
if((bytes_read=fread(packet.data,1,sizeof(packet.data),file))!=0)

This will get executed irrespective of whether offset*5 > file_size.
Is that your intention.

Frankly I found your code extremely difficult to read as the brace
style was confusing and your newsreader stipped much of the
indentation.

Break the programme down into smaller segments. Concentrate on walking
through the file first, forget about the sockets part till you
understand how to manipulate the stream of data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
E

ericunfuk

For the sockets part I suggest you ask again in comp.unix.programming,
where sockets are topical.


The outer while loop runs as long as last_packet_acked is zero, where
do you set it to nonzero?


how did you determine file_size? Also, whats that brace doing at the
end of the line?


This will get executed irrespective of whether offset*5 > file_size.
Is that your intention.

Frankly I found your code extremely difficult to read as the brace
style was confusing and your newsreader stipped much of the
indentation.

Break the programme down into smaller segments. Concentrate on walking
through the file first, forget about the sockets part till you
understand how to manipulate the stream of data.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?
 
R

Richard Tobin

ericunfuk said:
If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?

fseek() always clears the end-of-file indicator (unless it fails).

Imagine you've just read the end-of-file, and you seek back to the
middle. You don't want feof(file) to still be true, because a read
would now succeed.

-- Richard
 
C

CBFalconer

ericunfuk said:
I have posted a few other posts before this one today, I still
can't solve my problem, I think I need to elaborate my problem now.

I'm trying to send a file using UDP, below is a segment of my
sender app, the sender and the receiver are both on the same
machine and I have an Internet emulation gateway running on the
same machine as well.

I believe you have already been told that this is not topical
here. If you want to avoid plonk files and be able to get future
help on topical C questions, post this stuff elsewhere.
 
C

Chris Torek

I'm trying to send a file using UDP ...

If you are really using UDP, you are probably using sockets (which
lie outside the boundaries of Standard C, so you have to do something
"non-standard" to use them -- which means everything the Standard
tells you goes right out the window, at least potentially).

If you *are* using sockets, beware: fseek() does not work on sockets.
(This is one of the things that you must give up in order to use
networking.)
 
M

Mark McIntyre

If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?

The question indicates that you /still/ haven't understood the
difference between the end of a file, and the EOF flag in the
stream.The EOF flag is held by the little guy at the finishing line of
the race. He isn't the end of the race himself, he just tells you when
you got there.

As for your question: Imagine you're looking at a bookshelf. You can
move your finger to and fro over the shelf, and even right off the end
of the shelf into empty space. You're fseeking() to and fro, which is
allowed, so no alert flags get raised.

Now try to pick up a book to fread() it. You can only do that if
you're pointing inside the bookcase. If you're pointing outside the
bookcase, the EOF flag is waved to indicate you're trying to take a
book out of thin air.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top