Solved

Get IP Address of remote computer

Posted on 2003-11-19
7
55,778 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 155
while loop over for loop 7 119
ShiftLeft challenge 21 95
Example code 13 43
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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.

759 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