Sign in to follow this  

Bob Pendleton's Net2 Question

This topic is 4019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, maybe one could help me with this problem. I see there are more guys with the same problem throught the web, but I simply couldn't find the answer. Well, let's take a look at the code>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include <SDL/SDL.h>
#include "net2.h"
#include "fastevents.h"
#include "SDLUtils.h"
#include "trace.h"

//The attributes of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image( std::string filename ) 
{
    //Temporary storage for the image that's loaded
    SDL_Surface* loadedImage = NULL;
    
    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;
    
    //Load the image
    loadedImage = SDL_LoadBMP( filename.c_str() );
    
    //If nothing went wrong in loading the image
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );
        
        //Free the old image
        SDL_FreeSurface( loadedImage );
    }
    
    //Return the optimized image
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
    //Make a temporary rectangle to hold the offsets
    SDL_Rect offset;
    
    //Give the offsets to the rectangle
    offset.x = x;
    offset.y = y;
    
    //Blit the surface
    SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
  SDL_Event ev;
  char welcome[] = "hello there!-0123456789abcdefghijklmnopqrstuvwxyz";
  char buf[1024];
  char cbuf[1024];
  int len = 0;
  int count = 0;
  int s = -1;
  int next[0xffff];
  int n = -1;
  
  memset(next,0,sizeof(next));
  
  // This is a function which comes together with net2, it calls and error-checks SDL_Init(), SDLNet_Init(), FE_Init(),...
  mySDLInitOrQuit(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE);
  
  // Bind a server at port 6666
  NET2_TCPAcceptOn(6666);
  
  //Set up the screen
  screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
    
  //If there was in error in setting up the screen
  if( screen == NULL )
    return 1;
  
  //Load the images
  message = load_image("hello_world.bmp");
  background = load_image("background.bmp");
  
  //Apply the background to the screen
  apply_surface( 0, 0, background, screen );
  
  //Apply the message to the screen
  apply_surface( 180, 140, message, screen );
  
  //Update the screen
  if( SDL_Flip( screen ) == -1 ) {
    return 1;    
  }
  
  SDL_WM_SetCaption("<idle>",NULL);
  
  while (FE_WaitEvent(&ev))
  {
    switch (ev.type)
    {
    case SDL_USEREVENT:
      switch (NET2_GetEventType(&ev))
      {
      case NET2_TCPACCEPTEVENT:
        count++;
        sprintf(cbuf,"accept(%d)", NET2_GetSocket(&ev));
        SDL_WM_SetCaption(cbuf,NULL);
        s = NET2_GetSocket(&ev);
        if (-1 == NET2_TCPSend(s, welcome, sizeof(welcome) - 1)) {
          SDL_WM_SetCaption("WELCOME SEND FAILED",NULL);
        }
        next[s] = 0;
        break;

      case NET2_TCPRECEIVEEVENT:

        s = NET2_GetSocket(&ev);
        while (0 != (len = NET2_TCPRead(s, buf, sizeof(buf))))
        {
          int i;

          n = next[s];
          for (i = 0; i < len; i++)
          {
            if (welcome[n] != buf[i])
            {
              sprintf(cbuf,"out of sync %c != %c", welcome[n], buf[i]);
              SDL_WM_SetCaption(cbuf,NULL);
              
              SDL_FreeSurface(message);
              SDL_FreeSurface(background);
              
              exit(0);
            }
            n = (n + 1) % (sizeof(welcome) - 1);
          }
          next[s] = n;

          if (-1 == NET2_TCPSend(s, buf, len)) {
            SDL_WM_SetCaption("RECIEVE SEND FAILED",NULL);
          }
        }
        break;

      case NET2_TCPCLOSEEVENT:
        sprintf(cbuf,"close(%d)", NET2_GetSocket(&ev));
        SDL_WM_SetCaption(cbuf,NULL);
        NET2_TCPClose(NET2_GetSocket(&ev));
        count--;
        if (0 >= count)
        {
          SDL_FreeSurface(message);
          SDL_FreeSurface(background);
          
          exit(0);
        }
        break;

      case NET2_ERROREVENT:
        sprintf(cbuf,"Error: %s(%d)", NET2_GetEventError(&ev), NET2_GetSocket(&ev));
        SDL_WM_SetCaption(cbuf,NULL);
        SDL_FreeSurface(message);
        SDL_FreeSurface(background);
        
        exit(0);
        break;
      }
      break;

    case SDL_QUIT:
      SDL_FreeSurface(message);
      SDL_FreeSurface(background);
      
      SDL_Quit();
      exit(0);
    break;
    }
  }
  
  //Free the surfaces
  SDL_FreeSurface( message );
  SDL_FreeSurface( background );
  
  //Quit SDL
  SDL_Quit();
  
  return 0;    
}
That beginning stuff was took from some Lazy Foo's SDL tutorial, and it's there just as some kind of "debug" to see if everything with SDL is OK. After, that event-polling loop was copied from bob pendleton's documents, precisely from the tcp server example. When I compile it(without any warnings), sdl comes up to live with the bitmaps I loaded, and successfuly binds the server at port 6666. I open a connection to it throught telnet's windows client, the connection is made, but I don't receive the Welcome msg I should, and no events are sent to be polled. I mean, any non-net2 event is ok, it's polled and processed, but the net2 ones simple don't come up. I am under WinXP, the src was compiled with mingw32(Dev-CPP IDE). Plz help, thx!

Share this post


Link to post
Share on other sites

This topic is 4019 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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