Unix Programming - newbie : strange behaviour of server

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > newbie : strange behaviour of server





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 newbie : strange behaviour of server
Boris Seljanow

2004-01-23, 5:07 pm

Hello, I got a problem which is confusing me pretty heavily :
I ` m doing the first step in client server programming, this is the
code of the server :
It does what it should do, it receives data send by a client and
prints it on the console excepted I comment out the line "printf("
size : %d", size); ( marked by an arrow ), if this command is not
included the server doens`t print any of the received chars on the
console ??
Could anybody explain me this phenomenon ?
Thanks for any help,
cheers,
Boris

#include "socketheader.h"
#define SERVER_PORT 2223
#define MAXLEN 100

int sock, fd, client_len,n,size;
char line [100];
char recvbuffer[100];

struct sockaddr_in server;
struct sockaddr_in client;


int main(int argn, char **argv)
{
sock = socket(AF_INET, SOCK_STREAM, 0);
if( sock < 0 )
{
perror("error : creating stream socket !\n");
exit(1);
}
else
{
printf("creating stream socket succesfull !\n");
}

server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(SERVER_PORT);

if( (bind( sock,(struct sockaddr *)&server, sizeof(server))) < 0 )
{
perror("error : binding socket !\n");
exit(2);
}
else
{
printf("binding socket succesfull !\n");
}

if( (listen(sock, 5)) < 0 )
{
perror("error : listen !\n");
}
else
{
printf("server listening !\n");
}

client_len = sizeof(client);

while(1)
{
if( (fd = accept( sock, (struct sockaddr *)&client, &client_len
)) < 0 )
{
perror("error : accepting connection !\n");
exit(3);
}
else
{
printf("connection accepted !\n");
}
do
{
memset(recvbuffer, 0, sizeof(recvbuffer));
if( (size=recv( fd, &recvbuffer, sizeof(recvbuffer), 0 ))<0 )
{
perror("error : Daten lesen !\n");
}
printf("size : %d\n",size);
<----------------------------------
printf("empfangen : %s ",recvbuffer);

/*
if( size == 100 )
{
printf("empfangen1 : %s ",recvbuffer);
}
else
{
printf("empfangen2 : %s ",recvbuffer);
}
*/


}while( size > 0 );
close(fd);

}
printf("server will shut down !\n");
exit(0);
return 0;
}
Fletcher Glenn

2004-01-23, 5:07 pm



Boris Seljanow wrote:
quote:

> Hello, I got a problem which is confusing me pretty heavily :
> I ` m doing the first step in client server programming, this is the
> code of the server :
> It does what it should do, it receives data send by a client and
> prints it on the console excepted I comment out the line "printf("
> size : %d", size); ( marked by an arrow ), if this command is not
> included the server doens`t print any of the received chars on the
> console ??
> Could anybody explain me this phenomenon ?
> Thanks for any help,
> cheers,
> Boris
>


<snipped>
quote:

> printf("size : %d\n",size);
> <----------------------------------
> printf("empfangen : %s ",recvbuffer);
>


<snipped>

There's no mystery here. stdout is line buffered, and if
you don't supply a newline in the output, then data will
accumulate in the stdout buffer until the buffer is filled
(usually about 4K characters). So, by supplying a newline
in your "size" printout, you then flush the buffer.
If you don't like newlines in your output, you could always
do an fflush(stdout).

--

Fletcher Glenn

Lars Tetzlaff

2004-01-23, 5:07 pm

Hi



Boris Seljanow wrote:

....
quote:

> memset(recvbuffer, 0, sizeof(recvbuffer));
> if( (size=recv( fd, &recvbuffer, sizeof(recvbuffer), 0 ))<0 )
> {
> perror("error : Daten lesen !\n");
> }
> printf("size : %d\n",size);
> <----------------------------------
> printf("empfangen : %s ",recvbuffer);
>


What you are printing here may mot be a C-String. A C-String is
terminated by a '\0'. If you are shure you are getting printable
charachters, you should

memset( recvbuffer, '\0', sizeof(recvbuffer)); /* set all positions to
terminating charachter */

and

recv( fd, &recvbuffer, sizeof(recvbuffer) - 1, 0 )) /* keep at least one
terminating charachter */


If you use printf, you are using the standard output stream (stdout).
This stream is bufferd. For debugging pupose you should use the
unbuffered stderr:

fprintf( stderr, ...

Lars Tetzlaff

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com