Link to home
Start Free TrialLog in
Avatar of aumudin
aumudinFlag for United States of America

asked on

VB.NET Pogramming question(how to get the ip)

Just wondering if there is a easier way to get the ip through vb.net code then having the client when he logs in send a text message of what his ip he is using. Here is my code

While (bExit = False)

                'accept an incoming connection
                connection = listener.AcceptTcpClient

                'Get Data
                socketStream = connection.GetStream

                'declare a byte array to store data
                Dim bytes(connection.ReceiveBufferSize) As Byte

                'Transfer Data from Stream to byte array
                socketStream.Read(bytes, 0, CInt(connection.ReceiveBufferSize))

                'Conver the Byte into String
                reader = Encoding.ASCII.GetString(bytes)

                'Indicate that Data was received successfully on the Form
                Me.txtData.Text &= vbCrLf & "Connection  received."

                ' Step 4: Display the message on the form
                Me.txtData.Text &= vbCrLf & "Message: " & reader

                'Show as a marker of end of message
                Me.txtData.Text &= vbCrLf & "User terminated connection" & vbCrLf

                'Close connection
                socketStream.Close()
                connection.Close()

            End While


What i'm wanting to do is every time a person connects it stores there ip in a array or in later projects a database. If I store it in the array I can store what clients are connected then when client 1 sends a message, the server sends it out to all the rest of the clients pretty much like a messaging program. Late r projects I also want to store there user name to see who does what but you get my idea I think. If you have any questions let me know.
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

What is 'connection' defined as?

Bob
Avatar of aumudin

ASKER

Here is the top chunk of my code

Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Public Class TCPServerForm
    Inherits System.Windows.Forms.Form

    Private connection As TcpClient ' Socket object handles connection
    Private readThread As Thread ' server thread

    'Stream through which to transfer data
    Private socketStream As NetworkStream

    'Objects for writing and reading data
    Private reader As String
    Dim listener As TcpListener

    'Boolean item to communicate with thread
    Dim bExit As Boolean = False
Where is the "connection" made to the server?

Bob
Avatar of aumudin

ASKER

From a client app , i will post the code that makes the connection.
But just too be sure im wondering if there is a way I dont have to send the ip lets say in text to the serve just a built in function like    connection.getip   obviously that dont exist but you get the point.


Private Sub btnSend_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnSend.Click

        Dim tcpClient As New System.Net.Sockets.TcpClient 'TCP Client Socket
        Dim StreamData As NetworkStream 'Declaration to Send data as a stream

        'Connect the TCP Client to ServerIP and Port
        tcpClient.Connect(Me.txtIP.Text, CInt(Me.txtport.Text))

I don't think that information is available anywhere, but that doesn't mean that it is not possible, it just means that it wasn't where I thought it would be.

Bob
Avatar of aumudin

ASKER

I mean somehow the server has to know the clients ip address if its requesting a connection. Especially if its going to talk back.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aumudin

ASKER

You know what I just found that as well, was just reading it actually. haha

I tried everything I could find but tcpclient i figured it would be in the connection peice. Well thank you so very much on going thru this and helping me out!