newbie : strange behaviour of server
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > newbie : strange behaviour of server




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    newbie : strange behaviour of server  
Boris Seljanow


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10: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;
}





[ Post a follow-up to this message ]



    Re: newbie : strange behaviour of server  
Fletcher Glenn


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10: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




[ Post a follow-up to this message ]



    Re: newbie : strange behaviour of server  
Lars Tetzlaff


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-23-04 10: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




[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:28 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register