Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TCP Server get IP Address?

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
Okay I have most of this working but I'm stuck on one problem. I have a TCP Instant Messenger I made in class so me and one friend could send messages to each other. It works flawlessly. Now my other friends want to join in and all message together. So I'm making a TCP Server that will accept messages from the messenger I already made. The server will accept the message, and then send it to all the computers connected to it through port 65535. I can't seem to find out how to get the IP Address of the computers connected. Would I need an array for this? Here's what I have so far. Yes this is a console application. Can anyone help me with this?

Code:
Imports System.Net 'For Sockets.
Imports System.Net.Sockets 'For TCP access.
Imports System.Threading 'Is necessary so program does not freeze while listening for connection.
Imports System.IO 'File reading and writing + stream.
Module Module1
    Dim Listener As New TcpListener(65535) 'Defines TCPListener for port 65535 on this computer.
    Dim Message As String = "" 'Defines empty message for later.
    Dim Client As New TcpClient 'Defines Global TCPClient for receiving messages.

    Sub Main()
        Dim connection_Thread As New Thread(New ThreadStart(AddressOf Listening_Thread))
        Dim working_Thread As New Thread(New ThreadStart(AddressOf Working))
        Console.WriteLine("[Info] Starting messenger server on port 65535...")
        Try
            connection_Thread.Start()
            Console.WriteLine("[Info] Connection Thread started successfully.")
            working_Thread.Start()
            Console.WriteLine("[Info] Worker Thread started successfully.")
        Catch ex As Exception
            MsgBox("[Info] An error has occured.")
            Environment.Exit(0)
        End Try
    End Sub

    Sub Listening_Thread()
        Listener.Start() 'Starts TCPListener.
    End Sub

    Sub Working()
        If Listener.Pending = True Then
            Message = "" 'Reset message.
            Client = Listener.AcceptTcpClient() 'Accepts pending TCP request.
            Dim Reader As New StreamReader(Client.GetStream()) 'Sets reader to TCPClient stream.
            While Reader.Peek > -1
                Message = Message + Convert.ToChar(Reader.Read()).ToString 'Converts bytes to chars in a loop until the end.
            End While
            Console.WriteLine(Message)
            Dim Client2 As New TcpClient(, 65535) 'Connects to computer.
            Dim Writer As New StreamWriter(Client.GetStream()) 'Sets stream to computer connected to.
        End If
    End Sub

End Module
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top