Originally Posted by
chaos5687
I name it as "Other-pc", that worked on C# so I assumed it wouldn't cause any problems in C either.
It'd be great if you could give me an example on formatting the name
Do you mean formatting like "other-pc.some.text.net" ?
Just use the computer's name... My computers are named Volts, Amps, Watts, Joules and Ohms... So the Name parameter below would be simply "Amps" to get it's IP.
BUT...
You have to be using DNS from a local router or switch for this to work. Your ISP's DNS (for example) cannot look inside your network.
Code:
// resolve server name and port to useable LAN addr
BOOL GetHostAddr(PTCHAR Name, WORD Port, PSOCKADDR Addr)
{ ADDRINFOT hints = {0}; // hints for search
PADDRINFOT res = NULL; // pointer to search result
// fill in struct
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
// get ip address
if (GetAddrInfo(Name,NULL,&hints,&res))
return 0; \\ !0 == failure
// transfer to local IP struct
memcpy(Addr,res->ai_addr,sizeof(SOCKADDR));
((PSOCKADDR_IN) Addr)->sin_family = AF_INET;
((PSOCKADDR_IN) Addr)->sin_port = htons(Port);
// release memory
FreeAddrInfo(res);
return 1; }