Click here to Skip to main content
11,927,711 members (45,859 online)
Click here to Skip to main content
Add your own
alternative version


36 bookmarked

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

, 10 Apr 2002
Rate this:
Please Sign up or sign in to vote.
How to get the clients IP address using TcpClient in Managed C++
<!-- Add the rest of your HTML here -->


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);
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:

#pragma once

__gc class MyNetworkStream : public NetworkStream
	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()

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


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 ");
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!


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
Web Developer
United States United States
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


You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
arid0814-Jan-13 0:06
grouparid0814-Jan-13 0:06 
Questioninclude files? Pin
bkelly1321-Oct-10 18:16
memberbkelly1321-Oct-10 18:16 
GeneralIs there a way to sent stream out through a specific IP in a multi-IP server Pin
ErickJackob3-May-09 17:10
memberErickJackob3-May-09 17:10 
General[Message Deleted] Pin
it.ragester2-Apr-09 22:42
memberit.ragester2-Apr-09 22:42 
QuestionHow to pass TcpClient^ to a function as an argument? Pin
AlwaysStudent10-Nov-07 20:06
memberAlwaysStudent10-Nov-07 20:06 
QuestionUrgent Question Answer? Pin
BNKRAJ30-Oct-07 0:31
memberBNKRAJ30-Oct-07 0:31 
Questionwhat? Pin
System.Object21-Jul-06 13:21
memberSystem.Object21-Jul-06 13:21 
Answerusing TcpClient's property Pin
evoisard3-Jul-08 6:14
memberevoisard3-Jul-08 6:14 
Generalhi! Pin
scootz334512-Jul-05 19:25
sussscootz334512-Jul-05 19:25 
GeneralRe: hi! Pin
Albert Pascual13-Jul-05 5:54
memberAlbert Pascual13-Jul-05 5:54 
GeneralRe: hi! (Need Help ) Pin
patilabhijit10-Apr-07 4:57
memberpatilabhijit10-Apr-07 4:57 
GeneralThe code seems to be wrong. Pin
Huanlin Tsai7-May-05 1:46
memberHuanlin Tsai7-May-05 1:46 
Generalneed some guide... Pin
chal_adiera24-Mar-05 0:10
memberchal_adiera24-Mar-05 0:10 
GeneralRe: need some guide... Pin
mmatchyn18-Nov-05 14:54
membermmatchyn18-Nov-05 14:54 
GeneralI need This in c++ directly using winsock, not using MFC Pin
Anonymous20-Mar-04 15:20
sussAnonymous20-Mar-04 15:20 
Generalthanks for the description Pin
Adam Jones17-Nov-03 12:11
memberAdam Jones17-Nov-03 12:11 
GeneralThanks very much Pin
Anonymous23-Mar-04 4:21
sussAnonymous23-Mar-04 4:21 
GeneralRe: thanks for the description - in C++ Pin
AndyKEnZ4-Dec-07 3:59
memberAndyKEnZ4-Dec-07 3:59 
Generalthe result includes port # Pin
duhduhduh20-Feb-03 21:02
memberduhduhduh20-Feb-03 21:02 
GeneralI need this in Pin
SoTTo29-Oct-02 5:39
sussSoTTo29-Oct-02 5:39 
GeneralRe: I need this in Pin
Pete Bassett29-Oct-02 8:46
memberPete Bassett29-Oct-02 8:46 
GeneralRe: I need this in Pin
Pete Bassett29-Oct-02 8:52
memberPete Bassett29-Oct-02 8:52 
GeneralRe: I need this in Pin
Ken Ng18-Jan-03 9:48
sussKen Ng18-Jan-03 9:48 
GeneralRe: I need this in Pin
linge23-May-06 22:51
memberlinge23-May-06 22:51 
GeneralRe: I need this in Pin
Polymorpher13-Mar-06 19:47
memberPolymorpher13-Mar-06 19:47 
here you go, this comes from the reflection idea from one of the guys a little further up on this note. You cant port it directly in VB because of the case, but the reflection idea works great. I have tested this code and it works. Happy Hacking Wink | ;)

Public Structure sAddressInfo
Dim m_IP As String
Dim m_Port As String
End Structure

Public Shared Function IPAndPortFromTCPClient(ByVal theClient As TcpClient) As sAddressInfo
Dim pi As PropertyInfo = AESTypes.NetworkStream_t.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)
Dim theSocket As Socket = pi.GetValue(theClient.GetStream, Nothing)
Dim theEnd As EndPoint = theSocket.RemoteEndPoint
Dim theAddress As String = theEnd.ToString
Dim theInfo As New sAddressInfo

theInfo.m_IP = theAddress.Split(":")(0)
theInfo.m_Port = theAddress.Split(":")(1)

Return theInfo
End Function


-- modified at 16:33 Thursday 16th March, 2006

Sorry, Public Shared ReadOnly NetworkStream_t As Type = Type.GetType("System.Net.Sockets.NetworkStream")...but the original works better, sometimes this fails for some reason so use this
Dim pi As PropertyInfo = theClient.GetStream.GetType.GetProperty("Socket", BindingFlags.NonPublic Or BindingFlags.Instance)

-- modified at 16:35 Thursday 16th March, 2006

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 11 Apr 2002
Article Copyright 2002 by Albert Pascual
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid