Connecting to server

JanMulder
Posts: 15
Joined: Wed Jan 26, 2011 2:52 pm

Connecting to server

Post by JanMulder » Wed Jul 13, 2011 10:16 am

Hi,

I'm running a server on my pc. I forwarded a port. Then I made a client application for PC, which worked. Then I copied that code to my DS, changed it a little (Wifi_InitDefault, sys/socket etc). But when using connect() it always returns -1 with errno 113. "No route to host". Is this a common problem? How do I fix it?

Code: Select all

int WiFiLibSock;
bool Connected;
char ReceivedData[512];
char DataToSend[512];

struct sockaddr_in WiFiLibAddr, WiFiLibAddr2;

int CreateSocket(char *IP, unsigned short int Port) {
	WiFiLibAddr.sin_family = AF_INET;
	WiFiLibAddr.sin_port = htons(Port);
	if (inet_addr(IP) == -1) return -1;
	WiFiLibAddr.sin_addr.s_addr = inet_addr(IP);
	memset(&(WiFiLibAddr.sin_zero), '\0', 8);
	WiFiLibAddr2.sin_family = AF_INET;
	WiFiLibAddr2.sin_port = htons(Port);
	char HostName[128];
    gethostname(HostName, 128);
    struct hostent *host = gethostbyname(HostName);
    memcpy(&WiFiLibAddr2.sin_addr, (struct in_addr**)*host->h_addr_list, sizeof(struct in_addr));
	memset(&(WiFiLibAddr2.sin_zero), '\0', 8);
	WiFiLibSock = socket(AF_INET, SOCK_STREAM, 0);
	if (WiFiLibSock == -1) return -2;
	return 0;
}

int BindSocket(void) {
	return bind(WiFiLibSock, (struct sockaddr *)&WiFiLibAddr2, sizeof(WiFiLibAddr2));
}
	
int ConnectSocket(void) {
	return connect(WiFiLibSock, (struct sockaddr *)&WiFiLibAddr, sizeof(struct sockaddr));
}

bool ConnectWiFi(void) {
	int time = 0;
	if (Wifi_AssocStatus() != ASSOCSTATUS_ASSOCIATED) {
		Wifi_AutoConnect();
		while (Wifi_AssocStatus() != ASSOCSTATUS_ASSOCIATED) {
			if (time == 300) {
				Wifi_DisconnectAP();
				Wifi_DisableWifi();
				return false;
			}
			time++;
			PA_WaitForVBL();
		}
	}
	return true;
}

void InitWifi(void) {
	Wifi_EnableWifi();
	Wifi_InitDefault(true);
}

int Connect(char *IP, unsigned short int Port) {
	int err;
	err = CreateSocket(IP, Port);
	if (err < 0) return err;
	err = BindSocket();
	if (err < 0) return (err-2);
	err = ConnectSocket();
	if (err < 0) return (err-3);
	Connected = true;
	return 0;
}

void BreakConnection(void) {
	Connected = false;
	Wifi_DisconnectAP();
	Wifi_DisableWifi();
}

void MultiWiFiRun(void) {
	if (Connected) {
		send(WiFiLibSock,DataToSend,strlen(DataToSend),0);
		int len = recv(WiFiLibSock,ReceivedData,512,0);
		if (len == 0) BreakConnection();
	}
}

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Connecting to server

Post by elhobbs » Wed Jul 13, 2011 1:45 pm

are you able to get any of the dswifi samples to work? there may be a problem with your ds connecting to your AP.

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Connecting to server

Post by WinterMute » Wed Jul 13, 2011 4:00 pm

Uninstall your whole toolchain & reinstall without PALib.

http://devkitpro.org/wiki/PAlib
Help keep devkitPro toolchains free, Donate today

Personal Blog

JanMulder
Posts: 15
Joined: Wed Jan 26, 2011 2:52 pm

Re: Connecting to server

Post by JanMulder » Wed Jul 13, 2011 4:34 pm

elhobbs wrote:are you able to get any of the dswifi samples to work? there may be a problem with your ds connecting to your AP.
I am able to connect to the internet, I can download files etc. It seems that it's impossible to use any other port than the default ones: 80, 23 etc. But I can't use those to connect to my PC.
WinterMute wrote:Uninstall your whole toolchain & reinstall without PALib.

http://devkitpro.org/wiki/PAlib
That would mean for me to relearn everything I can do now. And just libnds is not always less complicated than PAlib as it shows in the example in the link. A simple button press is a whole lot of functions and variables. Sorry..

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Connecting to server

Post by elhobbs » Wed Jul 13, 2011 5:16 pm

sounds like a firewall issue - was your client app run from a different system than the server app when you were testing?

more importantly, wintermute knows what he is talking about - PALib is horribly broken and not supported by anyone. no one here is going to help you if you insist on using PALib.

there are other examples of PALib and wifi in the forums if you search - removing PALib was the only way to get it to work.

JanMulder
Posts: 15
Joined: Wed Jan 26, 2011 2:52 pm

Re: Connecting to server

Post by JanMulder » Wed Jul 13, 2011 6:06 pm

Don't get me wrong. I know PAlib is bad. It has lots of not working, inefficient functions. I know it all.

In the code I posted I didn't use PAlib functions except for PA_WaitForVBL() which can easily be changed.
elhobbs wrote:sounds like a firewall issue - was your client app run from a different system than the server app when you were testing?
Yes. Even from a different network.
I'm pretty sure PAlib isn't the problem here.

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Connecting to server

Post by elhobbs » Wed Jul 13, 2011 8:01 pm

I am guessing that you did not look at any of the other cases in this forum where people tried to get dswifi working with PALib and ran into erractic results - eliminating PALib from the project resolved the issue. You can guess that PALib is not the issue, but I think at this point you are on your own.

WinterMute
Site Admin
Posts: 1845
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: Connecting to server

Post by WinterMute » Wed Jul 13, 2011 8:22 pm

Whether or not you think PALib is the issue is irrelevant. We don't provide support to PALib users.
Help keep devkitPro toolchains free, Donate today

Personal Blog

JanMulder
Posts: 15
Joined: Wed Jan 26, 2011 2:52 pm

Re: Connecting to server

Post by JanMulder » Wed Jul 13, 2011 8:39 pm

Wait, so if I had changed PA_WaitForVBL() to swiWaitForVBlank() I had been supported? That doesn't really make sense does it? What's the problem with PAlib exactly?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Connecting to server

Post by elhobbs » Wed Jul 13, 2011 8:46 pm

read the wiki/PALib link in wintermute's original response.

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests