|
Home > Archive > Unix Programming > December 2006 > fdopen cause illegal seek
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
fdopen cause illegal seek
|
|
| darazbox@poczta.fm 2006-12-19, 7:32 am |
|
I have problem with fdopen function.
I want to write server/client app which is sending data thru tcp socket
and wait for answer from serwer.
When I read from socket using fdopen I recive illegal seek in errno. I
can read, but when I write to it, client are reciving empty message.
What is wrong?
I'm new in socket programming, so I can't find problem.
Here is my sample code:
thanx
server:
int sh = socket(PF_INET, SOCK_STREAM, 0);
if (sh<0)
{
exit(EXIT_FAILURE);
}
struct sockaddr_in serwer;
serwer.sin_family=AF_INET;
serwer.sin_port=htons(PORT);
serwer.sin_addr.s_addr=INADDR_ANY;
if (bind(sh, (struct sockaddr *) &serwer, sizeof(struct sockaddr_in)) <
0)
{
exit(EXIT_FAILURE);
}
if (listen(sh, QUERY_SIZE) < 0)
{
exit(EXIT_FAILURE);
}
struct sockaddr_in from;
socklen_t fromlen=sizeof(struct sockaddr_in);
int sh2 = accept(sh, (struct sockaddr *) &from, &fromlen);
// printf("error: %s\n",strerror(errno)); // success
FILE * net;
net=fdopen(sh2, "r+");
if (net==NULL) exit(EXIT_FAILURE);
// printf("error: %s\n",strerror(errno)); // fdopen don't
return NULL, but I have Illegal seek!
And from this point, every write to net success, but client recive
empty data.
I am sending data in this way:
int sockfd = socket (PF_INET, SOCK_STREAM, 0);
connect (sockfd, (struct sockaddr *) &adres, sizeof (adres))
send(sockfd, (message.str()).c_str(), (message.str()).length()-1, 0);
// sending data //
struct timeval czas_1,czas_2;
fd_set zestaw_1,zestaw_2;
czas_1.tv_sec = 4;
FD_ZERO(&zestaw_1);
FD_SET(sockfd, &zestaw_1);
bool quit = false;
while (quit == false) // now it is wait for answer from
serwer //
{
czas_2 = czas_1;
zestaw_2 = zestaw_1;
select (FD_SETSIZE, &zestaw_2, 0, 0, &czas_2);
if (FD_ISSET (sockfd, &zestaw_2))
{
char bufor[255]="";
int bajty = read (sockfd, bufor, 255);
if (bajty == 0)
{
printf("empty messsage\n");
}
else printf("answer: %s\n", bufor);
quit = true;
}
}
close(sockfd);
| |
| Casper H.S. Dik 2006-12-19, 7:32 am |
| darazbox@poczta.fm writes:
>When I read from socket using fdopen I recive illegal seek in errno. I
>can read, but when I write to it, client are reciving empty message.
>What is wrong?
There is one simple thing wrong: you check errno after a function
returns success.
A library call is allowed to change errno even when it returns
success (such as a non-NULL fdopen() return).
Typically, fdopen() performance several system calls to determine what
type of file desscriptor it has been handed; it may optimize for the
fact that it is seekable or not; it wants to determine the best buffersize,
etc.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| darazbox@poczta.fm 2006-12-19, 7:32 am |
|
Casper H.S. Dik napisal(a):
> darazbox@poczta.fm writes:
>
> A library call is allowed to change errno even when it returns
> success (such as a non-NULL fdopen() return).
>
> Typically, fdopen() performance several system calls to determine what
> type of file desscriptor it has been handed; it may optimize for the
> fact that it is seekable or not; it wants to determine the best buffersize,
> etc.
hmm.. thanx.. it's make sense.. I didn't know that..
then it is not fdopen cause problems..
but.. where is my problem? why I get always empty message using read
function?
| |
| Ralf Fassel 2006-12-19, 1:23 pm |
| * darazbox@poczta.fm
| but.. where is my problem? why I get always empty message using read
| function?
Did you make sure the sending side flushes after writing? Especially
the FILE* stdio functions buffer data, so you may require a fflush()
to actually send the data to the socket. IMHO, if you always want the
data written immediately, using stdio might not be the best option...
R'
| |
| darazbox@poczta.fm 2006-12-19, 1:23 pm |
|
Ralf Fassel napisal(a):
> Did you make sure the sending side flushes after writing? Especially
> the FILE* stdio functions buffer data, so you may require a fflush()
> to actually send the data to the socket. IMHO, if you always want the
> data written immediately, using stdio might not be the best option...
>
> R'
Thanx for answer.. but it didn't help..
Problem is the same even with fflush()
client side recive some data because I recive connection thru select
function, but I can't read this data.. I always have 0 bytes read..
> [...], IMHO, if you always want the
> data written immediately, using stdio might not be the best option...
What do you sugest in my situation?
| |
| Ralf Fassel 2006-12-19, 1:23 pm |
| * darazbox@poczta.fm
| > [...], IMHO, if you always want the
| > data written immediately, using stdio might not be the best option...
|
| What do you sugest in my situation?
Using read()/write() on the fd directly, though I doubt that it would
change much if the fflush() did not help.
R'
| |
| Casper H.S. Dik 2006-12-19, 1:23 pm |
| darazbox@poczta.fm writes:
>Thanx for answer.. but it didn't help..
>Problem is the same even with fflush()
>client side recive some data because I recive connection thru select
>function, but I can't read this data.. I always have 0 bytes read..
DO NOT CHECK errno UNLESS THE FUNCTION RETURNS AN ERROR INDICATION.
(Did my first post not show up?)
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| darazbox@poczta.fm 2006-12-20, 7:24 am |
|
Casper H.S. Dik napisal(a):
> DO NOT CHECK errno UNLESS THE FUNCTION RETURNS AN ERROR INDICATION.
>
> (Did my first post not show up?)
>
> Casper
Yes, I saw your post. You help me to realize that I have to check errno
only on function failure. It is true. Thanx. But this not solve the
problem.
I check again code and I did as Ralf said. I use write function
directly on fd and it help. Now read function recive all data..
I guess I have to learn more about this.. :/
thanx to all for help..
regards
|
|
|
|
|