Connecting to server
Posted: 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?
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();
}
}