Solved

Get IP Address of remote computer

Posted on 2003-11-19
7
55,773 Views
Last Modified: 2013-11-20
Currently I have an application that runs on our network. When someone logs on to the network it checks there system and gets the pc name. I would like to also get the IP address of that system. As well I may later like to ping that computer.

1) Is there a function to get the IP of this remote system?
2) IS the a function to ping a remote system and receive back the response?
0
Comment
Question by:Maritimer
7 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9779147
gethostbyname
The gethostbyname function retrieves host information corresponding to a host name from a host database.

Note  The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

struct hostent FAR *gethostbyname(
  const char FAR *name  
);
Parameters
name
[in] Pointer to the null-terminated name of the host to resolve.
Return Values
If no error occurs, gethostbyname returns a pointer to the hostent structure described above. Otherwise, it returns a NULL pointer and a specific error number can be retrieved by calling WSAGetLastError.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9779217
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9779224
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9779284
0
 

Accepted Solution

by:
ezicoder earned 50 total points
ID: 9785376
remove "http" string from URL

#include <Winsock2.h>

// do't forget to add WS2_32.LIB

// in your function

char szHostName[256];
char szIpAddress[256];
WSADATA wsaData;
HOSTENT *pHP;
SOCKADDR_IN myaddr;
myaddr.sin_family = AF_INET;

if ( SOCKET_ERROR != WSAStartup(0x202,&wsaData) )
{
     pHP = gethostbyname("www.google.com");
     memcpy((char FAR *)&(myaddr.sin_addr), pHP->h_addr, pHP->h_length);  
     wsprintf(szIpAddress,"%d.%d.%d.%d",myaddr.sin_addr.S_un.S_un_b.s_b1,myaddr.sin_addr.S_un.S_un_b.s_b2, myaddr.sin_addr.S_un.S_un_b.s_b3,myaddr.sin_addr.S_un.S_un_b.s_b4);
     WSACleanup();
}

Good Luck
0
 
LVL 1

Expert Comment

by:GavinThornton
ID: 9789575
Assuming your server app uses "SOCKET accept (  SOCKET s, struct sockaddr FAR* addr, int FAR* addrlen );" to accept connections from the clients you can get their ip address from the sockaddr.

e.g.

struct sockaddr_in      sRecvSAddr;
int iRecvSize;

// iServerSocket is your listening socket
if( accept(iServerSocket, (struct sockaddr*)&sRecvSAddr, &iRecvSize) < 0 )
{
    TRACE("Error");
}
else
{
    unsigned int iIP = sServerConnectContainer.sRecvSAddr.sin_addr.s_addr;
}


......... if you're using CSocket or something else, just look for the sockaddr_in structure.
0
 

Author Comment

by:Maritimer
ID: 9833611
Sorry I took so long.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 28
Expand LInux Boot partition remotly 3 86
FizzBuzz challenge 9 76
mapAB Challlenge 35 126
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now