TCP IP VB.NET

Started by
-1 comments, last by silentcoast 16 years, 6 months ago
Im having problems for whatever reason trying to get a basic tcp ip client server setup. I dont want it to be ran in console rather in a windows form anyone know of any good links. Here is what im doing. I have a peice of software developed that is goinig to be a auto update software. Only thing im lacking is the network peice. Here is the set up i have now. I have the server code in a module and am trying to use this networkserver.main() in form1.vb now here is my networkserver modual server

Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Module networkserver
    Private Stream As NetworkStream

    Public Sub Main()

        ' Create a new listener on port 8000.
        Dim Listener As New TcpListener(8000)

        Console.WriteLine("About to initialize port.")
        Listener.Start()

        Console.WriteLine("Listening for a connection...")

        Try
            ' Wait for a connection request, 
            ' and return a TcpClient initialized for communication. 
            Dim Client As TcpClient = Listener.AcceptTcpClient()
            Console.WriteLine("Connection accepted.")

            ' Retrieve the network stream.
            Stream = Client.GetStream()

            ' Create a BinaryWriter for writing to the stream.
            Dim w As New BinaryWriter(Stream)

            ' Create a BinaryReader for reading from the stream.
            Dim r As New BinaryReader(Stream)

            If r.ReadString() = ClientMessages.RequestConnect Then
                w.Write(ServerMessages.AcknowledgeOK)
                Console.WriteLine("Connection completed.")
                Do
                Loop Until r.ReadString() = ClientMessages.Disconnect
                Console.WriteLine()
                Console.WriteLine("Disconnect request received.")
                w.Write(ServerMessages.Disconnect)
            Else
                Console.WriteLine("Could not complete connection.")
            End If

            ' Close the connection socket.
            Client.Close()
            Console.WriteLine("Connection closed.")

            ' Close the underlying socket (stop listening for new requests).
            Listener.Stop()
            Console.WriteLine("Listener stopped.")

        Catch Err As Exception
            Console.WriteLine(Err.ToString())
        End Try

        Console.ReadLine()
    End Sub
    Public Class ServerMessages

        Public Const AcknowledgeOK As String = "OK"
        Public Const AcknowledgeCancel As String = "Cancel"
        Public Const Disconnect As String = "Bye"

    End Class

    Public Class ClientMessages

        Public Const RequestConnect As String = "Hello"
        Public Const Disconnect As String = "Bye"

    End Class
End Module
For some reason when I click my button it goes into the try and then freezes up. Right around the Dim Client As TcpClient = Listener.AcceptTcpClient() Console.WriteLine("Connection accepted.") now ive tried looking at other code and what not and it all works why wouldnt this work? This code has been taken out of a book "Microsoft Visual Basic.net Programmer's CookBook" and when i run there project it works. So dont make sense to me. theres only 1 difference They dont have any windows forms they are just running it as a straight console app. I have forms but have commented all the console.write and still wont work so what do I need to do? Thanks any help would be great!!!! aim - silentcoast yahoo - aumudin@yahoo.com msn - aumudin@hotmail.com

This topic is closed to new replies.

Advertisement