|
-
Apr 12th, 2013, 10:02 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Code to Extract TCP Client's IP Address
The TCP Listener project is starting to gel. And one of the things I did was to find some code which will determine the IP address of the client sending in the stream. I could swear that this worked yesterday.
Code:
Dim ipend = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList(0).ToString()
But today it's assigning the IP address of the machine which my TCP Listener is running on instead of the IP address of the client sending in the data. I really thought I saw this work successfully yesterday. Am I on drugs or what?
-
Apr 12th, 2013, 10:06 AM
#2
Re: Code to Extract TCP Client's IP Address
Are you using TCPClient? If so the client remote endpoint should have the ip address already.
Last edited by dbasnett; Apr 12th, 2013 at 12:44 PM.
-
Apr 12th, 2013, 10:33 AM
#3
Re: Code to Extract TCP Client's IP Address
-
Apr 12th, 2013, 11:53 AM
#4
Re: Code to Extract TCP Client's IP Address
What you're doing, Vladimir, is getting the host machine's (local, sometimes) IP address. I recommend doing a quick search for what you want, and I actually found a few different articles that would probably be very helpful to you!
EDIT: Reading the MSDN page on the TcpClient reveals the underlying Client Property which is actually a Socket which contains a RemoteEndPoint property which is defined as:
The EndPoint with which the Socket is communicating.
You can cast the EndPoint as an IPEndPoint and use the Address property to get the IP Address!
Last edited by formlesstree4; Apr 12th, 2013 at 11:57 AM.
-
Apr 12th, 2013, 02:12 PM
#5
Thread Starter
Hyperactive Member
Re: Code to Extract TCP Client's IP Address
formlesstree4,
That second link to codeproject was the actual code I found yesterday in my search. But there were some comments by other users at the bottom of that page where one guy said he did it the hard way. All that code could be written with one line. And I must have been dreaming because I was certain that yesterday I used this and it was showing the IP address of the machine which sent in the data stream. Even when I tested it from a remote machine outside if this lan it gave a weird IP address of one of the external routers at this place which must be owned by UC Fresno. I guess I was smoking something because today it's only giving up the IP of the machine my program is running on.
OK, so I'm trying to understand what you wrote there...
Code:
Dim s as Socket
Dim ipend = (IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
Sorry to be so dense, this is what it seems to want to get the next line working with s. But then when I
Code:
lvi.SubItems.Add(ipend)
I get a very long error about strings conversion again.
Last edited by Vladamir; Apr 12th, 2013 at 02:18 PM.
-
Apr 12th, 2013, 02:14 PM
#6
Re: Code to Extract TCP Client's IP Address
 Originally Posted by Vladamir
formlesstree4,
That second link to codeproject was the actual code I found yesterday in my search. But there were some comments by other users at the bottom of that page where one guy said he did it the hard way. All that code could be written with one line. And I must have been dreaming because I was certain that yesterday I used this and it was showing the IP address of the machine which sent in the data stream. Even when I tested it from a remote machine outside if this lan it gave a weird IP address of one of the external routers at this place which must be owned by UC Fresno. I guess I was smoking something because today it's only giving up the IP of the machine my program is running on.
OK, so I'm trying to understand what you wrote there...
Code:
s.Connect(lep)
(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
Sorry to be so dense, but I cannot translate this. I've tried lots of stuff, but I cannot recreate this code in my project, it keeps assigning what I type to something else.
1) What have you tried?
2) What actual is being assigned?
3) The 2nd line is just doing a conversion and not actually assigning it to any variable.
-
Apr 12th, 2013, 02:19 PM
#7
Thread Starter
Hyperactive Member
Re: Code to Extract TCP Client's IP Address
Sorry about that, I'm making so many changes on the fly. The above post shows my latest corrections to my otherwise ramblings of today. Thanks again for your assistance and patience in this.
-
Apr 12th, 2013, 02:31 PM
#8
Re: Code to Extract TCP Client's IP Address
Can you post the error message?
-
Apr 12th, 2013, 02:47 PM
#9
Thread Starter
Hyperactive Member
Re: Code to Extract TCP Client's IP Address
Overload resolution failed because no accessible 'Add' can be called with these arguments:
'Public Function Add(text As String) As System.Windows.Forms.ListViewItem.ListViewSubItem': Value of type 'System.Net.IPAddress' cannot be converted to 'String'.
'Public Function Add(item As System.Windows.Forms.ListViewItem.ListViewSubItem) As System.Windows.Forms.ListViewItem.ListViewSubItem': Value of type 'System.Net.IPAddress' cannot be converted to 'System.Windows.Forms.ListViewItem.ListViewSubItem'.
It's part of this code, which most is working successfully, so far. The blue code is the new code I added, the red one is the offending line.
Code:
Option Strict On
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Imports System.Net
Public Class Form1
Dim Listener As New TcpListener(81)
Dim Client As New TcpClient
Dim Message As String = ""
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim ListThread As New Thread(New ThreadStart(AddressOf Listening))
ListThread.Start()
End Sub
Private Sub Listening()
Listener.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim vWord() As String
Dim lvi As ListViewItem
If Listener.Pending = True Then
Message = ""
Client = Listener.AcceptTcpClient()
Dim Reader As New StreamReader(Client.GetStream())
While Reader.Peek > -1
Message = Message + Convert.ToChar(Reader.Read()).ToString
End While
Dim s As Socket
Dim ipend = (IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString()))
vWord = CType(ParseData(Message), String())
lvi = New ListViewItem(Now.ToString("ddd MM/dd/yyyy hh:mm tt "))
ListView1.Items.Add(lvi)
'lvi.SubItems.Add(vWord(1))
lvi.SubItems.Add(ipend)
lvi.SubItems.Add(vWord(10))
lvi.SubItems.Add(vWord(1))
lvi.SubItems.Add(vWord(2))
End If
End Sub
Private Function ParseData(ByVal Message As String) As Array
Message = Message.Substring(InStr(Message, "="))
Dim words As String() = Message.Split(CChar("&"))
Dim word As String
Dim upperBound As Integer = words.GetUpperBound(0)
Dim vWord(upperBound) As String
For i = 0 To upperBound
vWord(i) = words(i).Substring(InStr(words(i), "="))
Next
Return vWord
End Function
-
Apr 12th, 2013, 02:50 PM
#10
Re: Code to Extract TCP Client's IP Address
ipend is implied to be Object because you didn't declare it with an As statement:
Code:
Dim ipend As String = (IPAddress.Parse(CType(s.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()
-
Apr 12th, 2013, 02:59 PM
#11
Thread Starter
Hyperactive Member
Re: Code to Extract TCP Client's IP Address
Thanks again formlesstree4. I guess I don't understand all this typing stuff yet. I edited the line as you have shown but now it's giving me "Object reference not set to an instance of an object." error message. I'm clueless and again thanks for your patience. If I can get this working it will be fine and another item I've learned the hard way.
-
Apr 12th, 2013, 03:02 PM
#12
Re: Code to Extract TCP Client's IP Address
 Originally Posted by Vladamir
Thanks again formlesstree4. I guess I don't understand all this typing stuff yet. I edited the line as you have shown but now it's giving me "Object reference not set to an instance of an object." error message. I'm clueless and again thanks for your patience. If I can get this working it will be fine and another item I've learned the hard way.
What on earth...where's the error happening this time? I'm just glancing over the code quickly and nothing stands out. Do me a favor, and go to the TOP of the code page and add the following lines (before any Import statements and Class declarations):
Code:
Option Strict On
Option Explicit On
Those should literally be Lines 1 and 2.
EDIT: I see the problem:
That's not correct at all.
You should be pulling from the Client Object you declared earlier:
Code:
Client = Listener.AcceptTcpClient()
Right there. Try doing this instead:
Code:
Dim ipend As String = (IPAddress.Parse(CType(Client.Client.RemoteEndPoint, IPEndPoint).Address.ToString())).ToString()
-
Apr 12th, 2013, 03:11 PM
#13
Thread Starter
Hyperactive Member
Re: Code to Extract TCP Client's IP Address
Thanks. That's got it. I was just about to propose that it must have something to do with s. When I tried to type in Client before it would not take it, that is only Client. But when I typed in Client.Client it took it and now all is well. Can't thank you enough.
-
Apr 12th, 2013, 03:13 PM
#14
Re: Code to Extract TCP Client's IP Address
 Originally Posted by Vladamir
Thanks. That's got it. I was just about to propose that it must have something to do with s. When I tried to type in Client before it would not take it, that is only Client. But when I typed in Client.Client it took it and now all is well. Can't thank you enough.
Not a problem. If all is good, you should mark the thread as resolved. If you don't know how, you go up to the Thread Tools menu (at the top of the page), and click the mark the topic as resolved button.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|