Advertisement

SDL_Net - sockets and send/resv

Started by July 31, 2006 09:50 AM
4 comments, last by Twixly 18 years, 6 months ago
Hey, trying to get a simple chat going for a small lan-game with friends and family. When using SDL_Net do I have to close and re-open the connection every time I send some char* chat ? I am trying to follow the SDL tutorials on a Wiki page, but redone some of it since its only one connection in that example.. http://gpwiki.org/index.php/SDL:Tutorial:Using_SDL_net#Server_side the url to the example... Is there some way to just keep a connection open and active for unlimited number of datasends/recieves?
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
Ha, of course there is otherwise the whole library would be useless.

Assuming you want to use TCP for a chat program, create the server using SDLNet_TCP_Open then try to connect to the server from the client using the same function. Have the server running some sort of loop that calls SDLNet_TCP_Accept. If this function returns something other than NULL then you've connected and it has returned a TCPsocket that is connected to the client. On the client end the call to SDLNet_TCP_Open should return a TCPsocket that is connected to the server (unless something went wrong). Using these valid sockets you can send and receive data using SDLNet_TCP_Send and SDLNet_TCP_Recv respectivly. When you are done then you close the connection, but until then you can send and recieve as much as you'd like.

Its also a good idea to use the socket sets to check for activity before calling the recieve function.
Advertisement
You don't have to close and reopen a connection to send/recv data, it'll stay open until you call close on it.

If you're handling more than one client I'd recommend to learn how to use SDLNet_SocketReady, so you don't have to use multithreading to handle your clients.
Thanks alot, I will keep trying untill I figure it out.
I tried to do without closing but I get an error then, maybe I missed something else :)

Using SocketSet now to check activity also, and it works aslong as I send just one line then close it again, so I am onto something atleast
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
Hello again, cant get it to work, I am not sure I understand the SocketSets after all...

Here is the client trying to send (string chatline) over to (sd) server.

    void SendChat()    {                    int clientLen = 0, clientResult = 0;        clientLen = strlen(chatline.c_str()) + 1;        clientResult = SDLNet_TCP_Send(sd,(void*)chatline.c_str(),clientLen);        printf("Line sent: %s\nBytes_sent: %i\n", chatline.c_str(), clientResult );    }


the server simply loops CheckForConnections and CheckActivity... its just a simple layout I just did from the SLD_Net documentation....

class CServer{    public:    TCPsocket serverSocket;    vector<TCPsocket> clientSockets;        SDLNet_SocketSet clientSocketSet;    IPaddress myServerIP, *remoteIP;    int connections;        char * chatline;    ofstream chatfile;            CServer::CServer(int port, int maxClients)    {        // Initilize SDL        if(SDL_Init(0)==-1) {            printf("SDL_Init: %s\n", SDL_GetError());        }                // Initilize SDL_Net        if(SDLNet_Init()==-1) {            printf("SDLNet_Init: %s\n", SDLNet_GetError());        }                // Set server to listenmode on port XXXX        if (SDLNet_ResolveHost(&myServerIP, NULL, port))        {            fprintf(stderr, "SDLNet_ResolveHost: %s\n", SDLNet_GetError());        }    	/* Open a connection with the IP provided (listen on the host's port) */    	if (!(serverSocket = SDLNet_TCP_Open(&myServerIP)))    	{    		fprintf(stderr, "SDLNet_TCP_Open: %s\n", SDLNet_GetError());    	}                chatfile.open("chat.log");        chatfile.flush();        chatfile.close();        connections = 0;        clientSockets.resize(MAX_CLIENTS);        // Make a socketSet for maxClients        clientSocketSet = SDLNet_AllocSocketSet(maxClients);    }        CServer::~CServer()    {        SDLNet_FreeSocketSet(clientSocketSet);        SDLNet_Quit();        SDL_Quit();    }            void LookForConnections()    {        if(clientSockets[connections] = SDLNet_TCP_Accept(serverSocket))        {            connections = SDLNet_TCP_AddSocket(clientSocketSet, clientSockets[connections]);                printf("connections now : %i\n", connections);                if(connections==-1) {                    printf("SDLNet_AddSocket: %s\n", SDLNet_GetError());                    // perhaps you need to restart the set and make it bigger...                }                            /* Get the remote address */            if ((remoteIP = SDLNet_TCP_GetPeerAddress(clientSockets[connections-1])))                /* Print the address, converting in the host format */                printf("Host connected: %s %d\n", SDLNet_ResolveIP(remoteIP), SDLNet_Read16(&remoteIP->port));              else                fprintf(stderr, "SDLNet_TCP_GetPeerAddress: %s\n", SDLNet_GetError());        }    }            void CheckActivity(int milliseconds)    {                        int result, len;        char msg[MAXLEN];                int numready = SDLNet_CheckSockets(clientSocketSet, milliseconds);        if(numready==-1) {            //printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());            //most of the time this is a system error, where perror might help you.            //perror("SDLNet_CheckSockets");        }        else if(numready) {            //printf("There are %d sockets with activity!\n",numready);            // check all sockets with SDLNet_SocketReady and handle the active ones.            if(SDLNet_SocketReady(clientSocketSet)) {                //printf("Found active socket, starting recieve..\n");                    for(int i = 0; i < numready;i++){                                                result = SDLNet_TCP_Recv(clientSockets,msg,MAXLEN);                        if(result <= 0) {                            //printf("No incoming data\n");                        }                        else {                            chatfile.open ("chat.log", ofstream::app);                            msg[result] = 0;                            len = strlen(msg);                            chatfile.write(msg, len);                            chatfile << '\n';                            chatfile.close();                        }                    }                }            }    }};


If you can see what I get wrong and point it out I would be glad!
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net
solved it now, after reading even more and looking at the documentation
Windows - Dev-Cpp 4.9.9 - SDL - SDL_Net

This topic is closed to new replies.

Advertisement