Network Client draw issue, Network Server draws fine.
Matias Persson

Hi there, just recently got into networking!
I have a problem where in my client, drawing to the screen doesn't happen wheras in the server it works just fine.

Here's relevant client source code.

How I decide what text should be;

#SelectExpand
1if (!clientSocket) 2{ 3 text = "Server seems to be down"; 4} else { 5 if(gotServerResponse != 0) { 6 if(strcmp(buffer, "Accept" == 0) { 7 text = "Connected to the server"; 8 } else { 9 text = "Server is full"; 10 } 11 } else { 12 text = "Server failed to respond"; 13 } 14}

Stuff needed for drawing fonts is there

#SelectExpand
1al_init_font_addon(); 2al_init_ttf_addon(); 3ALLEGRO_FONT *font; 4font = al_load_font("data/fonts/font.ttf", 0, 24);

Client Loop

#SelectExpand
1do { 2 ALLEGRO_EVENT ev; 3 al_wait_for_event(event_queue, &ev); 4 5 if (ev.type == ALLEGRO_EVENT_TIMER) { 6 redraw = true; 7 } 8 9 if (redraw && al_is_event_queue_empty(event_queue)) { 10 al_clear_to_color(al_map_rgb(0, 0, 0)); 11 12 al_draw_text(font, al_map_rgb(255, 255, 255), 20, 20, 0, text.c_str()); 13 14 al_flip_display(); 15 } 16}while(clientRunning);

Very frustrating, I do exactly the same in the server and it works, except that I treat it like a server of course, but somehow it doesn't work in the client!
Any suggestions ???

Edit:
Another thing I should probably note is that the logs gives no errors, whatsoever;

Server Log

#SelectExpand
1Program initialized 2Socket set allocated with size: 500, of which 499 are available for use by clients. 3Successfully resolved host to IP: 0.0.0.0:1337 4Successfully created the server socket. 5Waiting for clients... 6There are currently 1 socket(s) with data to be processed. 7Found a free spot at element: 0 8Client connected, there are now 1 client(s) connected. 9There are currently 1 socket(s) with data to be processed. 10Found a free spot at element: 1 11Client connected, there are now 2 client(s) connected. 12There are currently 1 socket(s) with data to be processed. 13Found a free spot at element: 2 14Client connected, there are now 3 client(s) connected. 15There are currently 1 socket(s) with data to be processed. 16Client 0 disconnected. 17 18Server is now connected to: 2 client(s). 19 20There are currently 1 socket(s) with data to be processed. 21Client 1 disconnected. 22 23Server is now connected to: 1 client(s). 24 25There are currently 1 socket(s) with data to be processed. 26Client 2 disconnected. 27 28Server is now connected to: 0 client(s).

Client Log

#SelectExpand
1Program initialized 2Successfully allocated socket set. 3Successfully resolved host to IP: 127.0.0.1:1337 4Successfully resolved IP to host: Matias-PC 5Connection okay, about to read connection status from the server... 6There are 1 socket(s) with data on them at the moment. 7Got a response from the server... 8Got the following from server: OK(3 bytes) 9Joining server now... 10 11Client shutting down

GullRaDriel

Full network code or it will be hard to spot the problem. I don't want to steal your code, I already have some (buggy) of my own ;-)
It's just that I think your problem lies in your network management and that with just the logs and some pseudo code any analysis is biased.
;D

Matias Persson

Full server code:
http://ideone.com/bjVgAU

Full client code:
http://ideone.com/hr1ADW

Haha, I'm not worried you'll steal the code ;)
Just getting into networking and the code is pretty ugly, and seeing that you most definitely have more experience in programming than me, I'm sure you can easily do better than this :D

GullRaDriel

Okay. I'm reading.

First thought: how does SDL handle partials send/receive ? I had to make a recv function that loops until an error or all data arrived.
In your code, you quit directly when you don't have the right amount of octets received, but that's not the way it works.
Here is my code (I do not use SDL but sockets directly, you get the idea at least)

#SelectExpand
1/*!\fn send_data( SOCKET s , char *buf, NSTRBYTE n ) 2 *\brief send data onto the socket 3 *\param s connected socket 4 *\param buf pointer to buffer 5 *\param n number of characters we want to send 6 *\return -1 on error, n on success 7 */ 8int send_data( SOCKET s, char *buf, NSTRBYTE n ) 9{ 10 int bcount = 0 ; /* counts bytes read */ 11 int br = 0 ; /* bytes read this pass */ 12 13 _assert( buf , return -1 ); 14 15 if( n == 0 ) 16 { 17 n_log( LOG_ERR , "Send of 0 is unsupported." ); 18 return -1 ; 19 } 20 21 while( (NSTRBYTE)bcount < n ) /* loop until full buffer */ 22 { 23 if ( ( br = send( s, buf, n - bcount, NETFLAGS ) ) > 0 ) 24 { 25 bcount += br; /* increment byte counter */ 26 buf += br; /* move buffer ptr for next read */ 27 } 28 else 29 if ( br <= 0 ) 30 { /* signal an error to the caller */ 31 n_log( LOG_ERR , "Socket %d receive Error: %d , %s" , s , br , strerror( errno ) ); 32 return -1 ; 33 } 34 } 35 36 return bcount; 37} /*send_data(...)*/ 38 39 40 41/*!\fn recv_data( SOCKET s , char *buf, NSTRBYTE n ) 42 *\brief recv data from the socket 43 *\param s connected socket 44 *\param buf pointer to buffer 45 *\param n number of characters we want 46 *\return -1 on error, n on success 47 */ 48int recv_data( SOCKET s, char *buf , NSTRBYTE n ) 49{ 50 int bcount = 0 ; /* counts bytes read */ 51 int br = 0 ; /* bytes read this pass */ 52 53 _assert( buf , return -1 ); 54 55 if( n == 0 ) 56 { 57 n_log( LOG_ERR , "Recv of 0 is unsupported." ); 58 return -1 ; 59 } 60 61 while( (NSTRBYTE)bcount < n ) 62 { /* loop until full buffer */ 63 64 if ( ( br = recv( s, buf, n - bcount, NETFLAGS ) ) > 0 ) 65 { 66 bcount += br; /* increment byte counter */ 67 buf += br; /* move buffer ptr for next read */ 68 } 69 else 70 if ( br <= 0 ) 71 { /* signal an error to the caller */ 72 n_log( LOG_ERR , "Socket %d receive Error: %d , %s" , s , br , strerror( errno ) ); 73 return -1 ; 74 } 75 } 76 77 return bcount; 78} /*recv_data(...)*/

I may edit or add things as I find them.

EDIT:
"Got the following from server: OK(3 bytes)" Yet it looks like the client have received the data, so even if partials send/recv are not handled it works. What is exactly the problem then ?
EDIT:
mwarf. now it's grawling about strcpy_s .I installed the sdl to compile your examples and see what you're doing ;)

Matias Persson

strcpy_s is visual C++, just go with strcpy, and that is quite some advanced code you got there :P

Anyway, the problem is that the client never draws to the screen that what I have told it to do;
al_draw_text(font, al_map_rgb(255, 255, 255), 20, 20, 0, text.c_str());

GullRaDriel

I think this have to do with the queue.
I have a compiling version I'm trying to play with (and I used strcpy ;-) )

EDIT:

FOUND IT.

You swapped the two last parameters in the al_load_font in the client.cpp which makes the font unreadable !!!!

{"name":"609796","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a77a5c74d4004b4fa41b44622dc7d2d7.png","w":1920,"h":1080,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/a\/7\/a77a5c74d4004b4fa41b44622dc7d2d7"}609796

NINJA EDIT:
The following code in client.cpp:

  font = al_load_font("data/fonts/bookosb.ttf", 0 , 24 );

Should be ending by 24 , 0 !! ;D

Last edit:

In case you're interested, here is a quick makefile I hacked to make your project build onto linux: https://www.allegro.cc/files/attachment/609797

Matias Persson

Ooo thank you so much!! :D
And thank you for that makefile, I am about to go into android game development too, so that can also be useful there :)

GullRaDriel

You're welcome :-)
(And it's nice because it also allowed me to peek a view on some running A5 code ;-) )

Matias Persson

There's lots of running A5 code on this board :P

GullRaDriel

Maybe, but I do not dive in each ;-)

Thread #615803. Printed from Allegro.cc