C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2004, 06:20 AM   #1
Registered User
 
whackaxe's Avatar
 
Join Date: Mar 2004
Posts: 332
weird winsock output and winsock 2 not working

okay, so i have my little app set up and working good for me on my computer.

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;
}
it pops up correctly with my IP, then i sent it to my friend so he could try it for me (he has multiple IPs and i don't ) and he got
m\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   Reply With Quote
Old 07-07-2004, 09:43 AM   #2
Magically delicious
 
LuckY's Avatar
 
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   Reply With Quote
Old 07-08-2004, 05:20 AM   #3
Registered User
 
whackaxe's Avatar
 
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   Reply With Quote
Old 07-08-2004, 11:42 PM   #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   Reply With Quote
Old 07-09-2004, 11:10 AM   #5
Registered User
 
whackaxe's Avatar
 
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;
}
now i got another friend to test this (seeing as frend above has gone to a festival ) 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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 09:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22