Solved

Get IP Address of remote computer

Posted on 2003-11-19
7
55,772 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
Comment Utility
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
Comment Utility
0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
0
 

Accepted Solution

by:
ezicoder earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry I took so long.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
unable to delete all specified values regedit 38 149
List out all word 7 219
sumHeights  challenge 17 59
Detect file exist or not 3 76
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 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

12 Experts available now in Live!

Get 1:1 Help Now