![]() |
| | #1 |
| Registered User Join Date: Mar 2004
Posts: 332
| weird winsock output and winsock 2 not working Code: #include <winsock.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
bool sendAdress(void);
char listServer[] = "localhost";
bool sendAdress(void)
{
//first get your own IP
char localName[256];
LPHOSTENT localMachine;
//geting host name for local 'puter
gethostname(localName, sizeof(localName));
//making a hostent with above name
localMachine = gethostbyname(localName);
//getting adresses
char localAddrs[256];
int i = 0;
while (true)
{
if (!localMachine->h_addr_list[i])
{
break;
}
else
{
strcat(localAddrs,inet_ntoa(*(struct in_addr *) localMachine->h_addr_list[i]));
strcat(localAddrs,"\n");
i++;
}
}
//char * addrList = inet_ntoa(*(struct in_addr *) localMachine->h_addr_list[0]);
//print IP (hopefuly)
MessageBox(NULL,localAddrs,"your adress is...",MB_OK);
}
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
//first initiate winsock
WORD winsockVersion = MAKEWORD(1,1);
WSADATA wsaData;
WSAStartup(winsockVersion, &wsaData);
//main program
sendAdress();
WSACleanup();
return 0;
}
) and he gotm\WSOCK32.DLL followed by his LAN ip, a line break, then his net IP. second thing is i tried changing to winsock 2 (changed everything right in my source file) but when i compile i get a compile error like so: D:\PROGRA~1\DEVC__~1\INCLUDE\winsock2.h:46: unbalanced `#endif' does anyone know how i can fix this. using devc++ 4 thank you folks
__________________ I loathe pointers |
| whackaxe is offline | |
| | #2 |
| Magically delicious Join Date: Oct 2001
Posts: 856
| I had the same error years ago and recall that I had to just add an #endif to the appropriate place in the file (at the end?). Go open winsock2.h and look at the #if's and find their matching #endif's to find where one is missing. It's surprising that the authors made such a mistake and didn't catch it. Oh, and btw, you want to change that first strcat() to strcpy() (since you are concatenating data to the end of unknown data in the array the way it is now). |
| LuckY is offline | |
| | #3 |
| Registered User Join Date: Mar 2004
Posts: 332
| ok, got winsock2.h sorted out. it was a ifndef that didn't have a #in front of it on line 43 now i'm having problems with my linker i put -lws2_32 in the linker objects box in the project options, but im getting undefined reference errors for all of my network functions. any ideas?thanks *sorry for the ate reply btw
__________________ I loathe pointers |
| whackaxe is offline | |
| | #4 |
| Registered User Join Date: Jul 2003
Posts: 85
| Try libws2_32.a or lib/libws2_32.a for the lib. Libraries are named a bit differently in Dev-C++ :/ |
| scrappy is offline | |
| | #5 |
| Registered User Join Date: Mar 2004
Posts: 332
| oh well, i had those files, but was missing others so i just updated. works no. kindof... i've changed my code to this Code: #include <winsock2.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
bool sendAdress(void);
char listServer[] = "localhost";
bool sendAdress(void)
{
//first get your own IP
char localName[256];
LPHOSTENT localMachine;
//geting host name for local 'puter
gethostname(localName, sizeof(localName));
//making a hostent with above name
localMachine = gethostbyname(localName);
//getting adresses
char localAddrs[1024];
strcpy(localAddrs,"IP for computer name:\n\r");
strcat(localAddrs,localName);
strcat(localAddrs,"\r\n");
for(int i = 0;localMachine->h_addr_list[i]!= 0; i++)
{
strcat(localAddrs,inet_ntoa(*(struct in_addr *) localMachine->h_addr_list[i]));
strcat(localAddrs,"\n\r");
}
//print IP (hopefuly)
MessageBox(NULL,localAddrs,"did you know",MB_OK);
}
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
{
//first initiate winsock
WORD winsockVersion = MAKEWORD(2,0);
WSADATA wsaData;
WSAStartup(winsockVersion, &wsaData);
//main program
sendAdress();
WSACleanup();
return 0;
}
) and what he got was his computer name(normal) followed ONLY by his LAN IP. this should work shoudln't it?
__________________ I loathe pointers |
| whackaxe is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|