Welcome! Log In Create A New Profile

Advanced

Simple Network connection

Posted by burningman 
Simple Network connection
November 11, 2010 06:43PM
Hi, i was wondering if someone could help me with this. only thing i really need, is a simple class to initilize a connect TO a server. i took a look at the sockettest example, and its very confusing to read as theres not many comments or explainations for whats bieng done. the example works, but the wii app waits for a connection FROM another device, but i want to connect TO another device. i more or less understand how to use the net_send/net_recv functions, but its the setting up of the connect im having trouble.

- BurningMan
Re: Simple Network connection
November 11, 2010 07:05PM
Have you written any of the code yet? If so can we see it? It will be more benifitial to you if you edit your existing code, than sombeody gives you working code.
Re: Simple Network connection
November 11, 2010 07:14PM
I havent written any code myself. ive had some experience using similar class functions like send and recv in realbasic (OOP style coding similar to C/C++ with classes and such). but none of the wii netcode i have found (not very much) has not been in any form usable (at least by me). i had tried including the WiiNetwork code (link for it on WiiBrew Dev Tools List), but it doesnt want to compile, says things are undefined when they are in the include file and such. theres no sample with that so its hard to see what exactly i need to do to get it up and running. unless ive missed something, which i may have, im stumped. if you or anyone could even help with using that code, it would be MUCH appreciated.
Re: Simple Network connection
November 11, 2010 09:05PM
it works almost exactly the same as bsd sockets. Look up those and then try writing your code.
Re: Simple Network connection
November 12, 2010 05:21PM
i understand that, but the problem that i have with using that code right now, is that it givews me the following error:

linking ... template.elf
main.o: In function `main':
main.cpp:(.text.main+0x450): undefined reference to `tcp_connect(char*, unsigned short)'
collect2: ld returned 1 exit status
make[1]: *** [/d/template/template.elf] Error 1
"make": *** [build] Error 2


the only function im using right now is the tcp_connect, just to test to see if it would work.
Re: Simple Network connection
November 12, 2010 06:23PM
can we see that code, and also show the makefile.
Re: Simple Network connection
November 12, 2010 07:05PM
heres the code:


/*===========================================
        GRRLIB (GX Version)
        - Template Code -

        Minimum Code To Use GRRLIB
============================================*/

/*

		Basics for My Wii RPG game
*/

//#include 
//#include 

#include 

#include 
#include 

#include 

#include    // Needed for gettime and ticks_to_millisecs

#include 
#include 

#include 

// include files for gfx
#include "gfx/arial_12.h"
#include "gfx/BMfont1.h"
#include "gfx/BMfont2.h"
#include "gfx/BMfont3.h"
#include "gfx/BMfont4.h"
#include "gfx/BMfont5.h"

#include "gfx/button.h"
#include "gfx/button_over.h"
#include "gfx/dialogue_box.h"

#include "gfx/player1_point.h"

// include file for network functions
#include "net/http.h"

// RGBA Colors
#define GRRLIB_BLACK   0x000000FF
#define GRRLIB_MAROON  0x800000FF
#define GRRLIB_GREEN   0x008000FF
#define GRRLIB_OLIVE   0x808000FF
#define GRRLIB_NAVY    0x000080FF
#define GRRLIB_PURPLE  0x800080FF
#define GRRLIB_TEAL    0x008080FF
#define GRRLIB_GRAY    0x808080FF
#define GRRLIB_SILVER  0xC0C0C0FF
#define GRRLIB_RED     0xFF0000FF
#define GRRLIB_LIME    0x00FF00FF
#define GRRLIB_YELLOW  0xFFFF00FF
#define GRRLIB_BLUE    0x0000FFFF
#define GRRLIB_FUCHSIA 0xFF00FFFF
#define GRRLIB_AQUA    0x00FFFFFF
#define GRRLIB_WHITE   0xFFFFFFFF
#define GRRLIB_ALPHA   0XFFFFFF50

// Defines for Max Controls on Dialogs
#define MAX_BUTTONS		10
#define MAX_IMAGES		10
#define MAX_TEXT		10
#define MAX_CANVAS		10

// Defines for Dialog Window Types
#define NOFRAME			0
#define FRAME			1

using namespace std;

ir_t ir1;
u32 wpaddown, wpadheld;


	GRRLIB_texImg *tex_Arial = GRRLIB_LoadTexture(Arial_12);
	
	GRRLIB_texImg *tex_BMfont1 = GRRLIB_LoadTexture(BMfont1);
    

    GRRLIB_texImg *tex_BMfont2 = GRRLIB_LoadTexture(BMfont2);
   

    GRRLIB_texImg *tex_BMfont3 = GRRLIB_LoadTexture(BMfont3);


    GRRLIB_texImg *tex_BMfont4 = GRRLIB_LoadTexture(BMfont4);


    GRRLIB_texImg *tex_BMfont5 = GRRLIB_LoadTexture(BMfont5);
	
	GRRLIB_texImg *tex_button = GRRLIB_LoadTexture(button);
	GRRLIB_texImg *tex_button_over = GRRLIB_LoadTexture(button_over);
	GRRLIB_texImg *tex_dialog = GRRLIB_LoadTexture(dialogue_box);
	
	GRRLIB_texImg *tex_pointer = GRRLIB_LoadTexture(player1_point);
	
//#include "WindowClass.h"


int str2int(string str,int num)
{
	//char * cstr;
	//cstr = new char [str.size()+1];
	//strcpy (cstr, str.c_str());
	
	//return itoa(num,cstr,10);
};

string int2str(int num)
{
	/*char * cstr;
	string str;
	int t=2;
	if (num>9) t=3;
	if (num>99) t=4;
	//cstr = new char [t];
	//strcpy (cstr, str.c_str());
	sprintf("%4d",cstr,num);
	str=(string)cstr;*/
	
	string Result;          // string which will contain the result

	ostringstream convert;   // stream used for the conversion
	
	convert << num;      // insert the textual representation of 'Number' in the characters in the stream

	Result = convert.str();
	
	return Result;//itoa(num,cstr,10);
};


class GuiWindow {
public:

string GUIName;

map  ButtonCallBack[MAX_BUTTONS];

GuiWindow(string str,int xpos, int ypos,GRRLIB_texImg *fnt,int winType)
{
	specs.title=str;
	specs.x=xpos;
	specs.y=ypos;
	specs.w=600;
	specs.h=400;
	specs.font=fnt;
	specs.winType=winType;
	int index;
	
	for (index=0;indexh, GRRLIB_WHITE, 1);
		GRRLIB_Printf(specs.x+6, specs.y+6, specs.font, GRRLIB_BLACK, 1, "%s",specs.title.c_str());
	}
	//funcMap[ptr.c_str()]();
	
	//calls the fucntion to draw the button controls on the window
	draw_buttons();
	
	draw_text();
	
	//GRRLIB_Rectangle (x+2, y+2, w-2, h-2, GRRLIB_AQUA,0);
};

/*	Process window control callbacks (selected or clicked on controls)
*/
void do_window()
{
};

private:

/*
	Draws the buttons on the Window
*/

void draw_buttons()
{
	int index;
	float scalex=0.5,scaley=0.7,degrees=0;
	
	for(index=0;index<MAX_BUTTONS;index++)
	{
		if (buttons[index].active)
			{
				//GRRLIB_Rectangle (buttons[index].x, buttons[index].y, buttons[index].w, buttons[index].h, GRRLIB_WHITE,1);
				GRRLIB_DrawImg (buttons[index].x, buttons[index].y,tex_button, degrees, scalex, scaley, RGBA(255,255,255,255));
				GRRLIB_Printf (buttons[index].x+4+((buttons[index].caption.length()*8)/2), buttons[index].y+8, buttons[index].font, GRRLIB_BLACK, 1, "%s",buttons[index].caption.c_str());
			}
	}
};

void draw_text()
{
int index;

	for (index=0;index<MAX_TEXT;index++)
	{
		if (text[index].active)
			GRRLIB_Printf (text[index].x, text[index].y, text[index].font, GRRLIB_BLACK, 1, "%s",text[index].caption.c_str());
	}
};

void draw_images()
{
};

void draw_canvas()
{
};

/* Window Specifications

Width, Height, X Position, Y Position, Title, Font, Window Type (frame/no frame)
*/
struct specs_t
{
 string title;
 int x,y,w,h,winType;
 GRRLIB_texImg *font;
 
} specs;

int total_buttons;
int total_images;
int total_canvas;

struct buttons_t
{
	bool active,focus;
	string caption;
	int x,y,w,h;
	GRRLIB_texImg *font;
	GRRLIB_texImg *img;
	GRRLIB_texImg *img_over;
	GRRLIB_texImg *img_click;
} buttons[MAX_BUTTONS];

struct text_t
{
	bool active,focus;
	int x,y,w,h;
	string caption;
	GRRLIB_texImg *font;
	u32 color;
} text[MAX_TEXT];

struct images_t
{
	bool active,focus;
	int x,y,w,h;
	GRRLIB_texImg *img;
} images[MAX_IMAGES];

struct canvas_t
{
	bool active,focus;
	int x,y,w,h;
	GRRLIB_texImg *img;
} canvas[MAX_CANVAS];


};



void pointer()
{
	GRRLIB_Printf(5, 125, tex_BMfont5, GRRLIB_BLACK, 1, "GRRLIB %s Template Modified by %s", GRRLIB_VER_STRING,"BurningMan");
}




int main(int argc, char **argv) {
int but,txt,xtxt,ytxt,version;

string tmp;

bool done=false;

//tcp_init();

//funcMap["point"]=pointer
;
//GuiButton but("exit");
GuiWindow win("Main Window",5,20,tex_BMfont5,FRAME);
//win.add_callback(pointer,"p");

but=win.create_button(0,"button1",20,150,tex_BMfont5,pointer,"p");

txt=win.create_text("text",150,190,tex_BMfont5);

xtxt=win.create_text("000",50,220,tex_BMfont5);
ytxt=win.create_text("000",50,240,tex_BMfont5);

//version=win.create_text((string)ver,50,300,tex_BMfont5);



    // Initialise the Graphics & Video subsystem
    GRRLIB_Init();
	
	
	
	GRRLIB_InitTileSet(tex_BMfont1, 32, 32, 32);
	GRRLIB_InitTileSet(tex_BMfont2, 16, 16, 32);
    GRRLIB_InitTileSet(tex_BMfont3, 32, 32, 32);
	GRRLIB_InitTileSet(tex_BMfont4, 16, 16, 32);
    GRRLIB_InitTileSet(tex_BMfont5, 8, 16, 0);
	GRRLIB_InitTileSet(tex_Arial, 8, 16, 0);
	 
    // Initialise the Wiimotes
    WPAD_Init();
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);
	WPAD_SetVRes(WPAD_CHAN_ALL, 640, 480);
	
	s32 tcp=tcp_connect("192.168.1.102",2002);
	
    // Loop forever
    while(!done) {

        WPAD_ScanPads();
        wpaddown = WPAD_ButtonsDown(0);
        wpadheld = WPAD_ButtonsHeld(0);

        WPAD_IR(WPAD_CHAN_0, &ir1);
		
		GRRLIB_FillScreen(GRRLIB_BLACK);    // Clear the screen
		
		win.draw("p");
		
		if (ir1.valid)
		{
			//GRRLIB_Circle(ir1.x, ir1.y, 10, GRRLIB_OLIVE, 1);
			// Draw a yellow four pixel dot where the wiimote is pointing
			//GRRLIB_Plot(ir1.x, ir1.y, GRRLIB_YELLOW);
			//GRRLIB_Plot(ir1.x + 1, ir1.y, GRRLIB_YELLOW);
			//GRRLIB_Plot(ir1.x, ir1.y + 1, GRRLIB_YELLOW);
			//GRRLIB_Plot(ir1.x + 1, ir1.y + 1, GRRLIB_YELLOW);
			
			GRRLIB_DrawImg (ir1.x-62, ir1.y-9,tex_pointer, 0, 1, 1, RGBA(255,255,255,128));		
		}
		
		tmp=int2str(ir1.x-62);
		win.change_text(xtxt,tmp);
		tmp=int2str(ir1.y-9);
		win.change_text(ytxt,tmp);
		//funcMap["point"]();
		

        // If [HOME] was pressed on the first Wiimote, break out of the loop
        if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) done=true;
		if (WPAD_ButtonsDown(0) & WPAD_BUTTON_UP) win.change_text(txt,"Up",150,32);
		if (WPAD_ButtonsDown(0) & WPAD_BUTTON_DOWN) win.change_text(txt,"Down",150,300);
		if (WPAD_ButtonsDown(0) & WPAD_BUTTON_LEFT) win.change_text(txt,"Left",0,150);
		if (WPAD_ButtonsDown(0) & WPAD_BUTTON_RIGHT) win.change_text(txt,"Right",300,150);
		//if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) win.change_text(txt,"Home",150,150);

        // ---------------------------------------------------------------------
        // Place your drawing code here
        // ---------------------------------------------------------------------

        GRRLIB_Render();  // Render the frame buffer to the TV
    }

    GRRLIB_Exit(); // Be a good boy, clear the memory allocated by GRRLIB
	    // Free some textures
   
    GRRLIB_FreeTexture(tex_BMfont1);
    GRRLIB_FreeTexture(tex_BMfont2);
    GRRLIB_FreeTexture(tex_BMfont3);
    GRRLIB_FreeTexture(tex_BMfont4);
    GRRLIB_FreeTexture(tex_BMfont5);
	GRRLIB_FreeTexture(tex_Arial);
	
	GRRLIB_FreeTexture(tex_button);
	GRRLIB_FreeTexture(tex_button_over);
	GRRLIB_FreeTexture(tex_dialog);
	
	GRRLIB_FreeTexture(tex_pointer);
   
    exit(0);  // Use exit() to exit a program, do not use 'return' from main()
}



Edited 1 time(s). Last edit at 11/12/2010 07:08PM by burningman.
Re: Simple Network connection
November 12, 2010 07:06PM
and here the makefile:


#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC")
endif

include $(DEVKITPPC)/wii_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:=	$(notdir $(CURDIR))
BUILD		:=	build
SOURCES		:=	source source/gfx
DATA		:=	data
INCLUDES	:=

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------

CFLAGS	= -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS	=	$(CFLAGS)

LDFLAGS	=	-g $(MACHDEP) -Wl,-Map,$(notdir $@).map

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
# the order can-be/is critical
#---------------------------------------------------------------------------------
LIBS	:= -lgrrlib -lfreetype -lpngu -lpng -ljpeg -lz -lfat
LIBS	+= -lwiiuse
#LIBS	+= -lmodplay -laesnd
LIBS	+= -lbte -logc -lm

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:=

#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)

export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR	:=	$(CURDIR)/$(BUILD)

#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
	export LD	:=	$(CC)
else
	export LD	:=	$(CXX)
endif

export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
					$(sFILES:.s=.o) $(SFILES:.S=.o)

#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE	:=	$(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD) \
					-I$(LIBOGC_INC)

#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
					-L$(LIBOGC_LIB)

export OUTPUT	:=	$(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol

#---------------------------------------------------------------------------------
run:
	wiiload $(TARGET).dol


#---------------------------------------------------------------------------------
else

DEPENDS	:=	$(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

#---------------------------------------------------------------------------------
# This rule links in binary data with the .jpg extension
#---------------------------------------------------------------------------------
%.jpg.o	:	%.jpg
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

#---------------------------------------------------------------------------------
# This rule links in binary data with the .png extension
#---------------------------------------------------------------------------------
%.png.o	:	%.png
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

-include $(DEPENDS)

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------



Edited 1 time(s). Last edit at 11/12/2010 07:08PM by burningman.
Re: Simple Network connection
November 12, 2010 07:15PM
the only thing i changed in the http.c/h files was added the function declares as well as a typedef for boolean as it is used but wasnt declared in the code
Re: Simple Network connection
November 12, 2010 07:28PM
Read up on bsd sockets. It is the exact same thing as the wii sockets. By looking at your code, you don't know them. Once you can make a few socket examples on your pc then the only difference is all of the wii functions add net_ to the beginning of the function name.
Re: Simple Network connection
November 12, 2010 10:23PM
Also check this old thread out. [forum.wiibrew.org]

The Wii's network functions are listed in it. Weird I remembered that thread but the search could not find it.
Re: Simple Network connection
November 13, 2010 03:37AM
would there need to be any changes to the code to compile it, say with the c++ compiler? i have some example code for bsd sockets, but for some reason it always says it connects to the ip address i give it even if i dont have a program on the other computer to accept a connection. the code i have is for c not c++, but it compiles fine, but doesnt seem to work.



Edited 1 time(s). Last edit at 11/13/2010 03:42AM by burningman.
Re: Simple Network connection
November 13, 2010 03:52AM
can we see the code?
Re: Simple Network connection
November 13, 2010 05:33AM
int initnet(char *address,int port,char data[256])
{

	int sockd;
	int count;
	struct sockaddr_in serv_name;
	char buf[MAX_BUF];
	int status;
	
	/* create a socket */
	sockd = net_socket(AF_INET, SOCK_STREAM, 0);
	
	if (sockd == -1)
	{
		//perror("Socket creation");
		return -1;
	}
	
	/* server address */ 
	serv_name.sin_family = AF_INET;
	//inet_aton(argv[1], &serv_name.sin_addr);
	inet_aton(address, &serv_name.sin_addr);
	serv_name.sin_port = port;//htons(atoi(argv[2]));
	
	/* connect to the server */ 
	status = net_connect(sockd, (struct sockaddr*)&serv_name, sizeof(serv_name));
	if (status == -1) 
	{ 
		//perror("Connection error"); 
		return -1; 
	}
	
	//count = net_read(sockd, buf, MAX_BUF);
	//count=data.length();
	count=strlen(data);
	//net_write(sockd, data, count);
	net_send(sockd, data, strlen(data)+1, MSG_DONTWAIT);
	close(sockd);
	
	return 0;
}
Re: Simple Network connection
November 13, 2010 07:07AM
put the connect part in a while loop until it connects
Re: Simple Network connection
November 13, 2010 08:57PM
Take a look at the client stub from WiiRadio. We wrote it to work on Linux, Windows and Wii so there are ifdef's. It seems to work pretty well.

[code.google.com]

Edit -

P.S.

On Wii before any network stuff you need to call if_config(). Unless it's been changed recently.



Edited 1 time(s). Last edit at 11/13/2010 09:00PM by scanff.
Re: Simple Network connection
November 14, 2010 02:21PM
i took a look at that code. i added the if_config(), but i was curious, do i pass it the localip and such or does it return those values. i might have missed it but i didnt see anything that would return the localip and such.
Re: Simple Network connection
November 14, 2010 08:01PM
if_config() returns those values for you.

Something like :

char            localip[16];
char            gateway[16];
char            netmask[16];

if (if_config ( localip, netmask, gateway, TRUE) < 0)
{
  // Error
  return;
}

printf("My local IP is %s, the netmask is %s and the gateway is %s\n",localip, netmask, gateway);

Re: Simple Network connection
November 14, 2010 09:01PM
ok, it somewhat works now. i run it in the dolphin emu and now it says 'error connecting' and says 'connected' on the real wii but the server pc app still doesnt seem to acknowledge that a connection was made. might be the server app though. will have to take a look at the code for that. thanks for the info for the wii stuff though.
Re: Simple Network connection
December 08, 2010 05:49AM
im still not having any luck with getting the wii to connect to my server app. ive used the same for a windows console app (with some exceptions WINSOCK shit) but all in all its the same code, and the windows code connects, but the wii version wont. it says it connects, no errors or anything, but the server app doesnt acknowledge that there was a connection.
Sorry, only registered users may post in this forum.

Click here to login