Sign in to follow this  
Slider_SLO

SDL.net

Recommended Posts

Slider_SLO    136

Hey

I just wanna ask if anyone knows for some SDL.net tutorials for basic/beginners++, i allready went through top tutorials that I could find but still I have some problems...

Im having a problem that my client sends only one package to the server and cant send any other date and doesnt want to send same package aswell, its like the client locks down after the first package... Any ideas?

Share this post


Link to post
Share on other sites
Wooh    1088
If you know how TCP and UDP works you can probably read most things from the SDL_net documentation (warning: the description for SDLNet_TCP_Recv is wrong/misleading).

As for your problem it's hard to know what's going on. You mentioned package so I assume you are using UDP. How do you notice that it doesn't work. Do you get an error or is it just that the other side never receives what you sent? You know that UDP packages are not guaranteed to arrive?

Share this post


Link to post
Share on other sites
Slider_SLO    136
I made a mistake im not sending packages only strings and im using TCP, I know the basics of TCP/UDP and how it works...
Well my client app waits for the input and to send to the server and i can send the first string without any problems but after that I cant send anything else. It looks like that socket I created locks down after the first sting or something like that. Edited by Slider_SLO

Share this post


Link to post
Share on other sites
Slider_SLO    136

Im using SDLNet_TCP_Recv function in a loop to wait for the incoming string and it prints me out that string that comes in and im using function SDLNet_TCP_Send on the client side to send that string from client to server.

After i send my first string from client to server app everything is fine for the first string but for only one string for any other strings I want to send to the server I have to restart my client app.

Share this post


Link to post
Share on other sites
Slider_SLO    136

For the moment im not using it since I only created one way communication from client to server. Want to get this done at first before i move on to 2way communicating betwen multiple clients...

Share this post


Link to post
Share on other sites
Aldacron    4544

Sounds like you might have a logic problem somewhere or perhaps aren't checking error codes or some such. But without seeing any code, all anyone can do is guess. It would be helpful if you post some code relevant to the problem.

Share this post


Link to post
Share on other sites
Slider_SLO    136

This is my server app, it starts up SDL makes window etc. and then waits for the incoming data....

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include <cstring>
//SERVER--------------
int main(int argc,char** argv)
{
        freopen("CON","w",stdout);
        int running = 1;
        char buffer[512];
        SDL_Init(SDL_INIT_EVERYTHING);
        SDLNet_Init();
        SDL_WM_SetCaption("Server", NULL);
        SDL_Surface *screen, *background;
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
        background = SDL_DisplayFormat(SDL_LoadBMP("background.bmp"));
        

        IPaddress ip;
        SDLNet_ResolveHost(&ip,NULL,1234);

        TCPsocket server=SDLNet_TCP_Open(&ip);
        TCPsocket client;


        SDL_BlitSurface(background, NULL, screen, NULL);
        SDL_Flip(screen);

       
        

        while(running)
        {
                SDL_Event event;
                while(SDL_PollEvent(&event)) {
                       switch(event.type) {
                                case SDL_QUIT:
                                        running = 2;
                                        break;
                       }
                }

             client=SDLNet_TCP_Accept(server);
                if(client)
                {
                        if (SDLNet_TCP_Recv(client, buffer, 512) > 0)
                                    {
                                       printf("From Client: %s\n", buffer);
                                       SDLNet_TCP_Close(client);
                                        }
                       
                        
                        //SDLNet_TCP_Send(client,text,strlen(text)+1);
                        //SDLNet_TCP_Close(client);
                       //break;
                }

             if (running==2)
             {

             SDLNet_Quit();
             SDL_Quit();
                 }
            }

        SDL_FreeSurface(background);
        SDL_FreeSurface(screen);
       


        SDLNet_TCP_Close(server);


        SDLNet_Quit();
        SDL_Quit();


}
 

 

 

 

And this client starts window waits for the input to send string to server...

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_net.h>
#include <cstring>
//---------CLIENT----
int main(int argc,char** argv)
{
        SDL_Init(SDL_INIT_EVERYTHING);
        SDLNet_Init();
        //init graficne stvari
        SDL_Surface *screen, *background;
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
        background = SDL_DisplayFormat(SDL_LoadBMP("background.bmp"));
        
        bool b[4] = {0,0,0,0};
        bool running = true;
        char buffer[512];
        char* mesg="aaa aa a aa";
        char* mesg2="bbbb bb b";
        char* mesg3="cc ccccc c";
        char* mesg4="d d dddd d";
        
        freopen("CON","w",stdout);

        IPaddress ip;
        SDLNet_ResolveHost(&ip,"127.0.0.1",1234);

        
        SDL_BlitSurface(background, NULL, screen, NULL);
        SDL_Flip(screen);

        
        TCPsocket client=SDLNet_TCP_Open(&ip);



        while(running)
        {
            SDL_Event event;
                    while(SDL_PollEvent(&event)) {
                            switch(event.type) {
                                case SDL_QUIT:
                                    running = false;
                                    SDL_Quit();
                                    break;
                                case SDL_KEYDOWN:
                                    switch(event.key.keysym.sym)
                                    {
                                        case SDLK_w:
                                                b[0] = 1;
                                                break;
                                        case SDLK_a:
                                                b[1] = 1;
                                                break;
                                        case SDLK_s:
                                                b[2] = 1;
                                                break;
                                        case SDLK_d:
                                                b[3] = 1;
                                                break;
                                    }
                                    break;
                                case SDL_KEYUP:
                                     switch(event.key.keysym.sym)
                                     {
                                                case SDLK_w:
                                                        b[0] = 0;
                                                        break;
                                                        case SDLK_a:
                                                        b[1] = 0;
                                                        break;
                                                case SDLK_s:
                                                        b[2] = 0;
                                                        break;
                                                case SDLK_d:
                                                        b[3] = 0;
                                                        break;

                                     }
                                        
                            
                            
                            }
            if(b[0]){
            SDLNet_TCP_Send(client,mesg,strlen(mesg)+1);
            SDLNet_TCP_Close(client);
                }
            if(b[1]){
            SDLNet_TCP_Send(client,mesg2,strlen(mesg2)+1);
            SDLNet_TCP_Close(client);
                }
            if(b[2]){
            SDLNet_TCP_Send(client,mesg3,strlen(mesg3)+1);
            SDLNet_TCP_Close(client);
                }
            if(b[3]){
            SDLNet_TCP_Send(client,mesg4,strlen(mesg4)+1);
            SDLNet_TCP_Close(client);
                }
                            
        }

                
            }
    //    char text[10000];
        


    
        //while(SDLNet_TCP_Recv(client,text,10000))
        //        std::cout << text;
        
        

        
        SDL_FreeSurface(screen);
        SDL_FreeSurface(background);
        
        SDLNet_TCP_Close(client);
        SDLNet_Quit();
        SDL_Quit();
}
 

Share this post


Link to post
Share on other sites
Wooh    1088
The problem is that you close the client socket, both on the client and on the server, and then you try to use that socket again. After the socket has been closed you can't use it to send or receive. You would have to reconnect to the server each time you send or keep the connection open the whole time.

Share this post


Link to post
Share on other sites
Slider_SLO    136

ups that was a msitake i did when i was trying out something so just ignore those SDLNet_TCP_Close(client); functions, my original code is without em...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this