sdl_net question
hi again, back some time ago, i programmed a bit with sockets in visual basic, where i had the option to check the current state of a socket (listening, not openend, connected, connection terminated by remote host, connection terminated from local, etc). i'm interested if i can mimic the same behavior via sdl_net, or at least, know if it's disconnected or connected. is there any function or workaround to get the state of a socket? (i'm currently developing under cpp) to be more general, i'm trying to build a simple chat app. i don't know if it's the best way, but my point is to check if a previous connection dropped,so in the array of sockets, one that's disconnected will accept the connection from the another new client. izua.
you looking for something like SDLNet_GetError?
If you're making a chat app in C++ I would suggest using a vector over an array so you don't have to hardcode the number of clients in the app, and not have to resize your array, so when a connection drops you just remove it from the vector.
If you're making a chat app in C++ I would suggest using a vector over an array so you don't have to hardcode the number of clients in the app, and not have to resize your array, so when a connection drops you just remove it from the vector.
AMP Minibowling - Free asynchronous multiplayer mobile minigolf+bowling
[twitter]eedok[/twitter]
thanks for the reply, eedok.
well, let's say i have an array of 10 sockets, where the first 5 are connected.
in the next few seconds, client 3 will disconnect.
i would like a way to make my function give the next client that will connect position 3 in the array, not 6 (make SDLNet_TCP_Accept on position 3, but there must be a way, to read through the array, and check if a sock is connected or disconnected. is this the right way to do it?). there will be at most 10 clients connected.
i am pretty new to C progamming, and genneraly, i'm pretty new to advanced techniques (i've done just some php and vb before). i really don't know how a vector works. i've read on wiki that it's a self-resizable array. it looks like a stack, to me.
can you give me some points from where to start reading about them?
thanks
izua.
well, let's say i have an array of 10 sockets, where the first 5 are connected.
in the next few seconds, client 3 will disconnect.
i would like a way to make my function give the next client that will connect position 3 in the array, not 6 (make SDLNet_TCP_Accept on position 3, but there must be a way, to read through the array, and check if a sock is connected or disconnected. is this the right way to do it?). there will be at most 10 clients connected.
i am pretty new to C progamming, and genneraly, i'm pretty new to advanced techniques (i've done just some php and vb before). i really don't know how a vector works. i've read on wiki that it's a self-resizable array. it looks like a stack, to me.
can you give me some points from where to start reading about them?
thanks
izua.
here is my current development for the chat server, i have several issues with it. i don't have a client (yet), i'm just telneting into it. port is 4715.
sorry if some questions are posted in the wrong place, but i didn't want to start a thread for each of them, regarding the same code.
first, the program works pretty simple - it goes into main(), where it launches a thread for accepting network connections, and it puts it into "pause" (press any key to continue). i don't understand why I see 3 threads in windows process manager, and not two. there's one main thread, and another one for networking. could it be from the "press any key to continue" prompt?
2) i have declared on line 14 the client sockets (clnt[10]) that will actually connect a client. i'm not sure what happens when an array is given an element bigger than the one it can handle (like clnt[15]=3), but from what i tried, i was able to connect 20 clients in the same time. this is odd.
shouldn't it have crashed when i connected the 11th client?
3) i really hate writing 3 commands for a string like "You are client X out of maximum 10". is there a quick concatenation workaround (like php uses, for example) to something like buffer="You are client " + value_from_itoa + " from a max of 10"?
thanks. here is the code.
it is compiled with devc++ v. 4.9.9.0,
compiler parameters: -Dmain=SDL_main
liniker parameters: -lmingw32 -lSDLmain -lSDL -lSDL_net
thank you.
izua.
sorry if some questions are posted in the wrong place, but i didn't want to start a thread for each of them, regarding the same code.
first, the program works pretty simple - it goes into main(), where it launches a thread for accepting network connections, and it puts it into "pause" (press any key to continue). i don't understand why I see 3 threads in windows process manager, and not two. there's one main thread, and another one for networking. could it be from the "press any key to continue" prompt?
2) i have declared on line 14 the client sockets (clnt[10]) that will actually connect a client. i'm not sure what happens when an array is given an element bigger than the one it can handle (like clnt[15]=3), but from what i tried, i was able to connect 20 clients in the same time. this is odd.
shouldn't it have crashed when i connected the 11th client?
3) i really hate writing 3 commands for a string like "You are client X out of maximum 10". is there a quick concatenation workaround (like php uses, for example) to something like buffer="You are client " + value_from_itoa + " from a max of 10"?
thanks. here is the code.
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <SDL/SDL.h>#include <SDL/SDL_net.h>#include <windows.h>#include <SDL/SDL_thread.h>//variablesIPaddress ip, *remoteIP;SDL_Thread *cons_thread; //connction thread handleint crtc; //current clientchar buffer[512],buffer2[512];//declara socketurileTCPsocket lstn,clnt[10];void check_cons() { while (1==1) { //wait for a connection SDL_Delay(10); if (clnt[crtc] = SDLNet_TCP_Accept(lstn)) { strcpy(buffer,"You are given client id #"); itoa((crtc+1),buffer2,10); strcat(buffer,buffer2); strcat(buffer," from a maxium of ten."); SDLNet_TCP_Send(clnt[crtc],buffer,strlen(buffer)); crtc++; } }}void check_data() { }void Init() {}int main (int argc, char *argv[]){crtc=0; //total clienti = 0//init for SDLif(SDL_Init(0)==-1) { MessageBox(NULL, "Error while initialising SDL", "Error", MB_ICONWARNING); exit(1);}if(SDLNet_Init()==-1) { MessageBox(NULL, "Error while initialising SDL_net", "Error", MB_ICONWARNING); exit(2);}//resolve ip to listenif (SDLNet_ResolveHost(&ip, NULL, 4715)){ MessageBox(NULL, "Unable to resolve host to listen", "SDL_net Error", MB_ICONWARNING); exit(EXIT_FAILURE);}//listenif (!(lstn = SDLNet_TCP_Open(&ip))){ MessageBox(NULL, "Unable to get socket in listen state", "SDL_net Error", MB_ICONWARNING); exit(EXIT_FAILURE);}//open thread for accepting connectioncons_thread = SDL_CreateThread((int (*)(void*))check_cons, (void*)NULL);system("pause");return(0);}
it is compiled with devc++ v. 4.9.9.0,
compiler parameters: -Dmain=SDL_main
liniker parameters: -lmingw32 -lSDLmain -lSDL -lSDL_net
thank you.
izua.
1. Yes, your system() call in fact launches another program called "pause". I Think that's a highly unusual way of prompting a keypress (it's probably intended for .bat files). In a console app you can use printf() to print "press any key" and getchar() to wait for a keypress.
2. It won't necessarily crash if there isn't anything important in the memory you're erroneously writing into. If you had another variable there then writing to clnt[10] or higher would modify its contents. This is bad. You shouldn't accept new connections if you already have ten clients.
3. The "C" way to output formatted text into a string is sprintf()... For example:
sprintf(buffer, "You're client %d out of 10", crtc);
2. It won't necessarily crash if there isn't anything important in the memory you're erroneously writing into. If you had another variable there then writing to clnt[10] or higher would modify its contents. This is bad. You shouldn't accept new connections if you already have ten clients.
3. The "C" way to output formatted text into a string is sprintf()... For example:
sprintf(buffer, "You're client %d out of 10", crtc);
thanks.
i now tried to open and read several connections at the same time. i used an array of TCPSocket-s. accepting them works right (as in prev code), but i can't figure out how to do the reading from the right. I used a for loop, but it only reads from the alternatively, not in the same time.
i also tried to compile the example that comes with sdl_net, but gui_widgets ain't working right (at least here).
how can i accept and read data from multiple sockets?
i now tried to open and read several connections at the same time. i used an array of TCPSocket-s. accepting them works right (as in prev code), but i can't figure out how to do the reading from the right. I used a for loop, but it only reads from the alternatively, not in the same time.
i also tried to compile the example that comes with sdl_net, but gui_widgets ain't working right (at least here).
how can i accept and read data from multiple sockets?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement