|
Network Client draw issue, Network Server draws fine. |
Matias Persson
Member #15,093
May 2013
|
Hi there, just recently got into networking! Here's relevant client source code. How I decide what text should be; 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 1al_init_font_addon();
2al_init_ttf_addon();
3ALLEGRO_FONT *font;
4font = al_load_font("data/fonts/font.ttf", 0, 24);
Client Loop 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! Edit: Server Log 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 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
Member #3,861
September 2003
|
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 ;-) "Code is like shit - it only smells if it is not yours" |
Matias Persson
Member #15,093
May 2013
|
Full server code: Full client code: Haha, I'm not worried you'll steal the code |
GullRaDriel
Member #3,861
September 2003
|
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. 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: "Code is like shit - it only smells if it is not yours" |
Matias Persson
Member #15,093
May 2013
|
strcpy_s is visual C++, just go with strcpy, and that is quite some advanced code you got there Anyway, the problem is that the client never draws to the screen that what I have told it to do; |
GullRaDriel
Member #3,861
September 2003
|
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"} NINJA EDIT: font = al_load_font("data/fonts/bookosb.ttf", 0 , 24 ); Should be ending by 24 , 0 !! 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 "Code is like shit - it only smells if it is not yours" |
Matias Persson
Member #15,093
May 2013
|
Ooo thank you so much!! |
GullRaDriel
Member #3,861
September 2003
|
You're welcome :-) "Code is like shit - it only smells if it is not yours" |
Matias Persson
Member #15,093
May 2013
|
There's lots of running A5 code on this board |
GullRaDriel
Member #3,861
September 2003
|
Maybe, but I do not dive in each ;-) "Code is like shit - it only smells if it is not yours" |
|