Jump to content

  • Log In with Google      Sign In   
  • Create Account

Slider_SLO

Member Since 12 Nov 2012
Offline Last Active Jun 01 2013 05:58 AM

Posts I've Made

In Topic: SDL.net

27 May 2013 - 09:54 AM

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...


In Topic: SDL.net

27 May 2013 - 08:02 AM

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();
}
 


In Topic: SDL.net

25 May 2013 - 07:58 AM

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...


In Topic: SDL.net

25 May 2013 - 07:40 AM

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.


In Topic: SDL.net

25 May 2013 - 04:52 AM

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.

PARTNERS