By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,645 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

fseek()

P: n/a
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[i]==0){continue;}

offset = window[i]-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.d ata),file))!=0)
{
packet.client_seq_no = htonl(window[i]);
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[i]);

}else{
packet.client_seq_no = htonl(window[i]);
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[i]);
break;
}

}
...............
...............

Mar 23 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 23 Mar 2007 16:27:13 -0700, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>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.d ata),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
Mar 23 '07 #2

P: n/a
On 23 Mar, 23:51, Mark McIntyre <markmcint...@spamcop.netwrote:
On 23 Mar 2007 16:27:13 -0700, in comp.lang.c , "ericunfuk"

<xuwenduan2...@gmail.comwrote:
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.d ata),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
If I dont fseek() beyond EOF, then EOF doesn't get cleared, right?

Mar 24 '07 #3

P: n/a
In article <11*********************@e1g2000hsg.googlegroups.c om>,
ericunfuk <xu***********@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Mar 24 '07 #4

P: n/a
ericunfuk wrote:
>
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Mar 24 '07 #5

P: n/a
In article <11**********************@y80g2000hsf.googlegroups .com>
ericunfuk <xu***********@gmail.comwrote:
>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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Mar 24 '07 #6

P: n/a
On 23 Mar 2007 17:28:45 -0700, in comp.lang.c , "ericunfuk"
<xu***********@gmail.comwrote:
>
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
Mar 24 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.