473,327 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

TcpClient and IP

Hello
How can i read IP of incoming connection when i have TcpClient for that
connection ?

Thanx

Nov 15 '05 #1
7 7335
us**@domain.invalid wrote:
Hello
How can i read IP of incoming connection when i have TcpClient for
that connection ?

Thanx


you can get that from a socket object the RemoteEndPoint propery
returns a IPEndPoint structure you can use.

The slightly trick bit is getting the socket object, there are 2
options..

1./ The TcpClient class has a Socket property, but it is protected, so
you could create a custom class that derives from it.

2./ You can use the TcpListener.AcceptSocket method which returns a
socket. (you can create a network stream from the socket that you can
use to create BinaryReader and binaryWriter objects)

then you simply use the socket i.e.

(IpEndPoint)socket.RemoteEndPoint.Address.ToString ();

Hope that Helps.

Regards Tim.
Nov 15 '05 #2
Tim Jarvis wrote:

(IpEndPoint)socket.RemoteEndPoint.Address.ToString ();


oops, missed a pair of brackets..

((IpEndPoint)socket.RemoteEndpoint).address.TStrin g();
Nov 15 '05 #3
Yes and introducing a couple of new errors too :P

((IpEndPoint)socket.RemoteEndPoint).Address.ToStri ng();

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #4
Morten Wennevik wrote:
Yes and introducing a couple of new errors too :P

((IpEndPoint)socket.RemoteEndPoint).Address.ToStri ng();


lol, that will teach me to type quick and just hit send...

I hope though that my typo's aside the point was made :-)
Cheers Tim.
Nov 15 '05 #5
Ok, i tried to create my own TcpClient class:
public class MyTcpClient: System.Net.Sockets.TcpClient
{
public MyTcpClient()
{
}

public Socket ReturnSocket()
{
return Client;
}
}

and in server.cs code i have:
MyTcpClient handler = (MyTcpClient)listener.AcceptTcpClient();
(listener is TcpListener).

It compiles, but when i start program it hangs on that line.
Why ? How can i change TcpClient to MyTcpClient so i could use
ReturnSocket() ?

Thanx again :)

Nov 15 '05 #6
us**@domain.invalid wrote:
Ok, i tried to create my own TcpClient class:
public class MyTcpClient: System.Net.Sockets.TcpClient
{
public MyTcpClient()
{
}

public Socket ReturnSocket()
{
return Client;
}
}

and in server.cs code i have:
MyTcpClient handler = (MyTcpClient)listener.AcceptTcpClient();
(listener is TcpListener).

It compiles, but when i start program it hangs on that line.
Why ? How can i change TcpClient to MyTcpClient so i could use
ReturnSocket() ?

Thanx again :)


To be honest I wouldn't be inclined to use this method, I would be more
inclined to use the 2nd method I suggested, as the first is a bit of a
hack.

The reason your code is not working is because you have introduced a
new method of the class, what you need to do is simply change the
visibility of the Socket property from protected to public in the
tcpclient class, it's a hack because although the typecast will work,
the accepttcpclient method does not return your type, it returns the
tcpclient.

Regards Tim.
Nov 15 '05 #7


thx, i used #2 method and it works fine:)

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
by: Theo | last post by:
Hi, I am using a tcpclient and a tcplistener to send and receive packets. The client sends a packet and the listener replies with another one. I can send 2 packets and get an answer from the...
3
by: מורדי | last post by:
Hi, I'm writing a client/server application in which the client send a series of screenshots to the server to be saved using the tcpclient. in most cases the first screenshot is transmitted ok...
5
by: Horst Walter | last post by:
What is wrong here? IPAddress ipAddress = IPAddress.Parse("10.10.20.1"); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, this.port); this.tcpClient = new TcpClient(ipEndPoint); // PROBLEM HERE...
3
by: Ricardo Quintanilla | last post by:
i had a problem whom i do not know how to explain. i was using a TcpClient (System.Net.Sockets.TcpClient) object to send and receive data to an AS400 socket. Two months ago it started to work...
3
by: Danny Tuppeny | last post by:
Hi all, I'm trying to send a null character as a string delimiter through a TcpClient (code below). It's to connect to this poker bot room: http://games.cs.ualberta.ca/webgames/poker/bots.html...
0
by: Torsten Brasch | last post by:
Hi All and Happy New Year ;) I have a very strange problem with System.Net.Sockets.TcpClient(). For some reason, the number of bytes I can receive is limited to 5460 bytes. I made sure that the...
1
by: hamil | last post by:
I am having trouble using the TcpListener and TcpClient classes. At the end of this post is server code that runs, and a class whose purpose is described below. I need to know when the client...
2
by: craigkenisston | last post by:
I'm creating a TcpClient instance in an object which is created in a thread. I have a loop that goes about 6-7 cycles, creating the container object in each cycle and each object contains a...
5
by: puzzlecracker | last post by:
It looks like i need to get IPEndPoint first, but I cannot figure out from msdn the eventual obtainment of machine name and port number. Please suggest a solution. Thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.