5,427,303 members and growing! (17,822 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Intermediate

How to get the clients IP address using TcpClient in Managed C++

By Albert Pascual

How to get the clients IP address using TcpClient in Managed C++
C++/CLI, VC6, VC7, C++, .NET, Windows, Win2K, WinXP, Visual Studio, MFC, Dev

Posted: 10 Apr 2002
Updated: 10 Apr 2002
Views: 149,849
Bookmarked: 22 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
115 votes for this Article.
Popularity: 8.53 Rating: 4.14 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 9.1%
3
1 vote, 9.1%
4
9 votes, 81.8%
5

Introduction

In managed C++ using sockets, Microsoft is giving you a whole set of new tools to use Sockets. So you don't have to create a class to handle Client and Server communications. These classes are TcpListener for the server and TcpClient.

 
TcpListener * pTcpListener;
TcpListener = new TcpListener(80);
TcpListener->Start(); 
TcpClient * pTcpClient; 
pTcpClient = m_TcpListener->AcceptTcpClient();

This opens port 80 and listens for connections. When a client connects to port 80, the function AcceptTcpClient() returns with a TcpClient class.

Those two classes together are very powerful. You should create a thread to use the TcpClient and wait again to accept another client.

The problem I had is I need it to get the IP address of the Client. I couldn't find the way to get it from the TcpClient, even after I get the Stream like this:

 NetworkStream * networkStream = pTcpClient->GetStream();
networkStream->Read(bytes, 0, (int) pTcpClient->ReceiveBufferSize);

Now that I had a NetworkStream I thought I could get the IP address, well I was wrong again. networkStream->getSocket() is a private member of the class NetworkStream.

So, to resolve this problem I had to create a derived class from NetworkStream:

//MyNetworkStream.h

#pragma once

__gc class MyNetworkStream : public NetworkStream
{
public:
	MyNetworkStream(void) : NetworkStream(0) { };
	MyNetworkStream(System::Net::Sockets::Socket * s) : NetworkStream(s) { };
	
	System::Net::Sockets::Socket * get_MySocket();
	String * get_IPAddress(void);
};



//MyNetworkStream.cpp file

#using <System.dll>
#using <mscorlib.dll>
using System::Net::Sockets::NetworkStream;
using System::String;

#include "myNetworkStream.h"



System::Net::Sockets::Socket * MyNetworkStream::get_MySocket()
{
	return(this->get_Socket());
}

String * MyNetworkStream::get_IPAddress(void)
{
	System::Net::Sockets::Socket *soc = get_Socket();
	System::Net::EndPoint		*Endp = soc->get_RemoteEndPoint();

	return(Endp->ToString());
}

So when you have this class, you only have to do something like that to get the client's IP address and socket:

NetworkStream * networkStream = pTcpClient->GetStream(); 
MyNetworkStream * myStream =  static_cast<MyNetworkStream *>(networkStream);
ClientIP = myStream->get_IPAddress();
Console::Write(S"Client IP Address ");
Console::WriteLine(ClientIP);
networkStream->Read(bytes, 0, (int) pTcpClient->ReceiveBufferSize);

There you go! IP address and everything. Now if you don't use TcpClient, you could AcceptSocket() instead of AcceptTcpClient() to get the socket. When you have the socket you can use get_RemoteEndPoint(), but I thought that you will like to use TcpClient.

Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Albert Pascual


Sitebuilder
Al is just another Software Engineer working in C++, ASp.NET and C#. Enjoys snowboarding in Big Bear, and wait patiently for his daughters to be old enough to write code and snowboard.

Al is a Microsoft ASP.NET MVP

Blog
Occupation: Web Developer
Location: United States United States

Other popular C++ / CLI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHow to pass TcpClient^ to a function as an argument?memberAlwaysStudent20:06 10 Nov '07  
QuestionUrgent Question Answer?memberBNKRAJ0:31 30 Oct '07  
Generalwhat?memberSystem.Object13:21 21 Jul '06  
Generalusing TcpClient's propertymemberevoisard6:14 3 Jul '08  
Generalhi!sussscootz334519:25 12 Jul '05  
GeneralRe: hi!memberAlbert Pascual5:54 13 Jul '05  
GeneralRe: hi! (Need Help )memberpatilabhijit4:57 10 Apr '07  
GeneralThe code seems to be wrong.memberHuanlin Tsai1:46 7 May '05  
Generalneed some guide...memberchal_adiera0:10 24 Mar '05  
GeneralRe: need some guide...membermmatchyn14:54 18 Nov '05  
GeneralI need This in c++ directly using winsock, not using MFCsussAnonymous15:20 20 Mar '04  
Generalthanks for the descriptionmemberAdam Jones12:11 17 Nov '03  
GeneralThanks very muchsussAnonymous4:21 23 Mar '04  
GeneralRe: thanks for the description - in C++memberAndyKEnZ3:59 4 Dec '07  
Generalthe result includes port #memberduhduhduh21:02 20 Feb '03  
GeneralI need this in vb.netsussSoTTo5:39 29 Oct '02  
GeneralRe: I need this in vb.netmemberPete Bassett8:46 29 Oct '02  
GeneralRe: I need this in vb.netmemberPete Bassett8:52 29 Oct '02  
GeneralRe: I need this in vb.netsussKen Ng9:48 18 Jan '03  
GeneralRe: I need this in vb.netmemberlinge22:51 23 May '06  
GeneralRe: I need this in vb.netmemberPolymorpher19:47 13 Mar '06  
Generalcan u make this code in to Javamembersxdoss21:08 5 May '02  
GeneralRe: can u make this code in to JavamemberAnonymous19:14 13 Jun '02  
GeneralCan you do this with C#memberAdam Byrne5:48 29 Apr '02  
GeneralRe: Can you do this with C#memberAlbert Pascual12:31 29 Apr '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Apr 2002
Editor: Chris Maunder
Copyright 2002 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project