d***@poczta.fm
2006-12-19 10:21:18 UTC
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);
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);