Click here to Skip to main content
16,013,747 members
Articles / Programming Languages / C#
Article

Get a TCPServer's Connected Client's IP Address in C#

Rate me:
Please Sign up or sign in to vote.
1.04/5 (18 votes)
21 Sep 2007 87.1K   17   7
An article on getting tcpip client address on the server machine
Screenshot - img.jpg

Introduction

From time to time I need to obtain a TCP clients IP Address while working with a TCP server. I normally forget how to do this and as it is just a one liner — Google doesn't really have any straight forward examples — so I decided to post it here!

Using the Code

Using the code is as easy as taking the example and modifying it to work with your code. The below example will start a TCPServer on port 2000 and once a client has connected, it will send the clients IP Address back to the client.

C#
using System.Net.Sockets;

static NetworkStream stream;
static TcpClient client; 
static TcpListener server;
static int port = 2000;

// setup tcp server
IPAddress localAddr = IPAddress.Parse("127.0.0.1"); 
server = new TcpListener(localAddr, port);
server.Start(); 
client = server.AcceptTcpClient();

//Get the client ip address
string clientIPAddress = "Your Ip Address is: " + IPAddress.Parse(((
    IPEndPoint)client.Client.RemoteEndPoint).Address.ToString()));

stream = client.GetStream();

if (client.Connected) {
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(clientIPAddress);
    stream.Write(data, 0, data.Length);
}
//close sockets and clean up code
...a

Points of Interest

For futher reading please make sure to investigate multi-threaded and async TCP client/servers!

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


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionthank you Pin
captainst_nj24-May-15 20:59
captainst_nj24-May-15 20:59 
GeneralMy vote of 4 Pin
captainst_nj24-May-15 20:58
captainst_nj24-May-15 20:58 
Questionperfect Pin
1987198664-Mar-13 20:31
1987198664-Mar-13 20:31 
GeneralWas helpful Pin
theCPkid14-Jun-12 2:18
theCPkid14-Jun-12 2:18 
GeneralThis isnt an article, it belongs on a Post-It note Pin
leppie21-Sep-07 6:37
leppie21-Sep-07 6:37 
GeneralRe: This isnt an article, it belongs on a Post-It note Pin
WillemM21-Sep-07 6:47
WillemM21-Sep-07 6:47 
GeneralRe: This isnt an article, it belongs on a Post-It note Pin
Member 1112796723-May-15 20:10
Member 1112796723-May-15 20:10 

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.