Results 1 to 4 of 4

Thread: VB.NET Console App error "No accessible 'Main' Method

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2025
    Posts
    7

    VB.NET Console App error "No accessible 'Main' Method

    I am a newbie, learning by using snippets of code. Trying to make this console app for a TCP Listener work. But keep getting Build error " BC30737 No accessible 'Main' method with an appropriate signature was found. I tried changing to Shared Sub Main() but no difference.

    Any advice appreciated.
    Code:
    Imports System.IO
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports System.Threading
    
    Public Class TcpServer
    
        Private listener As TcpListener
        Private listenThread As Thread
        Private isListening As Boolean = False
    
        Sub Main()
            Dim port As Integer = 1470
            Call StartServer(port)
    
        End Sub
        Public Sub StartServer(port As Integer)
            Try
                listener = New TcpListener(IPAddress.Any, port)
                listener.Start()
                isListening = True
                listenThread = New Thread(AddressOf ListenForClients)
                listenThread.Start()
                Console.WriteLine($"Server started on port {port}")
            Catch ex As Exception
                Console.WriteLine($"Error starting server: {ex.Message}")
            End Try
        End Sub
    
        Private Sub ListenForClients()
            While isListening
                Try
                    Dim client As TcpClient = listener.AcceptTcpClient()
                    Console.WriteLine("Client connected.")
                    ' Handle the client in a new thread or task
                    Dim clientThread As New Thread(AddressOf HandleClientComm)
                    clientThread.Start(client)
                Catch ex As SocketException When ex.SocketErrorCode = SocketError.Interrupted OrElse ex.SocketErrorCode = SocketError.ConnectionReset
                    ' Listener was stopped or connection reset
                    Console.WriteLine("Listener interrupted or connection reset. Restarting listener...")
                    StopServer()
                    StartServer(CType(listener.LocalEndpoint, IPEndPoint).Port) ' Restart on the same port
                Catch ex As Exception
                    Console.WriteLine($"Error accepting client: {ex.Message}")
                End Try
            End While
        End Sub
    
        Private Sub HandleClientComm(clientObj As Object)
            Dim client As TcpClient = CType(clientObj, TcpClient)
            Dim stream As NetworkStream = Nothing
            Try
                stream = client.GetStream()
                ' Implement your communication logic here (read/write to stream)
                ' Example:
                Dim buffer(1023) As Byte
                Dim bytesRead As Integer
                While client.Connected AndAlso (bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0
                    Dim receivedData As String = Encoding.ASCII.GetString(buffer, 0, bytesRead)
                    Console.WriteLine($"Received: {receivedData}")
                    ' Send a response
                    Dim responseBytes As Byte() = Encoding.ASCII.GetBytes("ACK: " & receivedData)
                    stream.Write(responseBytes, 0, responseBytes.Length)
                End While
            Catch ex As IOException
                Console.WriteLine($"Client disconnected: {ex.Message}")
            Catch ex As Exception
                Console.WriteLine($"Error handling client: {ex.Message}")
            Finally
                If Not stream Is Nothing Then
                    stream.Close()
                    stream.Dispose()
                End If
                If Not client Is Nothing Then
                    client.Close()
                    client.Dispose()
                End If
            End Try
        End Sub
    
        Public Sub StopServer()
            If Not listener Is Nothing AndAlso isListening Then
                isListening = False
                listener.Stop()
                If Not listenThread Is Nothing AndAlso listenThread.IsAlive Then
                    listenThread.Join() ' Wait for the listener thread to finish
                End If
                Console.WriteLine("Server stopped.")
            End If
        End Sub
    
    End Class

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: VB.NET Console App error "No accessible 'Main' Method


  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB.NET Console App error "No accessible 'Main' Method

    There's a reason that the Console App project template creates a module by default. Modules are basically equivalent to C# static classes, i.e. you cannot create an instance and all members are effectively Shared. A Main method in a module does not require an instance of that type to be created. Your code would require an instance of the TcpServer class to be created in order for that Main method to be called. You really ought to have stuck with the module for startup and then used the class only for functionality related to that class.

    By the way, the Call keyword is almost never required in VB.NET and isn't in your Main method.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2025
    Posts
    7

    Re: VB.NET Console App error "No accessible 'Main' Method

    Thank you for the advice. All working now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width