Click here to Skip to main content
15,929,653 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: i need to make a connection to sql server 2000 from vb.net Pin
maxiachun12-Oct-05 14:51
maxiachun12-Oct-05 14:51 
GeneralRe: i need to make a connection to sql server 2000 from vb.net Pin
Dave Kreskowiak12-Oct-05 15:43
mveDave Kreskowiak12-Oct-05 15:43 
AnswerRe: i need to make a connection to sql server 2000 from vb.net Pin
K Baruah16-Oct-05 20:33
K Baruah16-Oct-05 20:33 
QuestionMessageBox Behind Forms with ApplicationModal On. Pin
jburkle12-Oct-05 7:23
jburkle12-Oct-05 7:23 
Answeroff subject Pin
icouldntthinkofagoodname12-Oct-05 8:02
icouldntthinkofagoodname12-Oct-05 8:02 
GeneralRe: off subject Pin
Dave Kreskowiak12-Oct-05 8:08
mveDave Kreskowiak12-Oct-05 8:08 
QuestionDatabinding Pin
kenexcelon12-Oct-05 3:57
kenexcelon12-Oct-05 3:57 
Questiontcplistener + thread Pin
cengelen12-Oct-05 1:27
cengelen12-Oct-05 1:27 
Hi
I am a beginner in .NET and I am making a program in .NET for communication (TCP) with a other device.
Now I have problems with threads and the tcplistener.

Here is what the program should do:
- a start button (for start listening on specified port)
- a stop button (to close connection and stop listening)
- if I click the start button and a connection is accepted, the connection must be retained untill I click the stop button, because I want to get more messages from my remote device.

Now I have the following code, but it fails to work with a working stop button (form hangs) and it closes my connection everytime the program send the message.

visual basic code:
Imports System.Net.Sockets<br />
Imports System.Text<br />
Imports System.Net<br />
<br />
<br />
Public Class Eventlogger<br />
    Const portNumber As Integer = 779<br />
    Dim ipAddress As ipAddress = Dns.Resolve("localhost").AddressList(0)<br />
    Dim tcpListener = New tcpListener(portNumber)<br />
    Private ListenThread<br />
    Private parent As frmMain<br />
    Private TcpClient As tcpClient<br />
    Private password = "test"<br />
    Private listening As Boolean = False<br />
<br />
<br />
<br />
    Public Sub setParent(ByVal frmMain As frmMain)<br />
        parent = frmMain<br />
    End Sub<br />
<br />
    Public Sub startListening()<br />
        parent.btnStartListening.Enabled = False<br />
        parent.btnStopListening.Enabled = True<br />
<br />
        Console.WriteLine("listener started")<br />
        ListenThread = New System.Threading.Thread(AddressOf Listen)<br />
        ListenThread.name = "listener"<br />
        ListenThread.Start()<br />
        listening = True<br />
<br />
<br />
    End Sub<br />
<br />
    Public Sub stopListening()<br />
        parent.btnStartListening.Enabled = True<br />
        parent.btnStopListening.Enabled = False<br />
        listening = False<br />
<br />
        Console.ReadLine()<br />
        tcpListener.Stop()<br />
        Console.WriteLine("listener stopped")<br />
        ListenThread.Join()<br />
<br />
    End Sub<br />
<br />
    Protected Sub Listen()<br />
        While (listening)<br />
            tcpListener.Start()<br />
            Console.WriteLine("Waiting for connection...")<br />
            Try<br />
<br />
<br />
                'Accept the pending client connection and return             <br />
                'a TcpClient initialized for communication. <br />
                TcpClient = tcpListener.AcceptTcpClient()<br />
                Console.WriteLine("Connection accepted.")<br />
<br />
                While (listening)<br />
<br />
<br />
                    ' Get the stream<br />
                    Dim networkStream As NetworkStream = TcpClient.GetStream()<br />
<br />
<br />
                    ' Read the stream into a byte array<br />
                    Dim bytes(TcpClient.ReceiveBufferSize) As Byte<br />
                    networkStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))<br />
<br />
<br />
<br />
                    ' Return the data received from the client to the console.<br />
                    Dim clientdata As String = Encoding.ASCII.GetString(bytes)<br />
                    Console.WriteLine((clientdata))<br />
<br />
                    If InStr(clientdata, "Password") Then<br />
                        Dim responseString As String = password & vbCrLf & "status" & vbCrLf<br />
                        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)<br />
                        networkStream.Write(sendBytes, 0, sendBytes.Length)<br />
                        Console.WriteLine(("Password send"))<br />
                    End If<br />
                End While<br />
<br />
<br />
                'Dim responseString As String = "Connected to server."<br />
                'Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)<br />
                'networkStream.Write(sendBytes, 0, sendBytes.Length)<br />
                'Console.WriteLine(("Message Sent /> : " + responseString))<br />
<br />
<br />
<br />
                'Any communication with the remote client using the TcpClient can go here.<br />
                'Close TcpListener and TcpClient.<br />
<br />
<br />
                TcpClient.Close()<br />
<br />
            Catch e As Exception<br />
                Console.WriteLine(e.ToString())<br />
                Console.ReadLine()<br />
            End Try<br />
        End While<br />
<br />
    End Sub<br />
    Private Sub closeTab()<br />
        tcpListener = Nothing<br />
    End Sub<br />
<br />
End Class




Could someone give me some hints to help me further with my program?


Christian
QuestionDLL creation form vb.net project Pin
carlos_rocha12-Oct-05 1:23
carlos_rocha12-Oct-05 1:23 
AnswerRe: DLL creation form vb.net project Pin
enjoycrack12-Oct-05 7:08
enjoycrack12-Oct-05 7:08 
Questionhow to creat a currency converter Pin
Member 235262112-Oct-05 1:04
Member 235262112-Oct-05 1:04 
AnswerRe: how to creat a currency converter Pin
Colin Angus Mackay12-Oct-05 1:31
Colin Angus Mackay12-Oct-05 1:31 
AnswerRe: how to creat a currency converter Pin
Dave Kreskowiak12-Oct-05 1:33
mveDave Kreskowiak12-Oct-05 1:33 
AnswerRe: how to creat a currency converter Pin
icDavid12-Oct-05 15:38
icDavid12-Oct-05 15:38 
Questioni need the steps and code for designing a currency converter Pin
Member 235262112-Oct-05 0:56
Member 235262112-Oct-05 0:56 
AnswerRe: i need the steps and code for designing a currency converter Pin
enjoycrack12-Oct-05 7:05
enjoycrack12-Oct-05 7:05 
Questionmonitor and control remote PC Pin
Ali el11-Oct-05 23:00
Ali el11-Oct-05 23:00 
AnswerRe: monitor and control remote PC Pin
Dave Kreskowiak12-Oct-05 1:30
mveDave Kreskowiak12-Oct-05 1:30 
GeneralI wanted to learn VB.NET Pin
Raju Malladada11-Oct-05 21:41
Raju Malladada11-Oct-05 21:41 
GeneralRe: I wanted to learn VB.NET Pin
jonathan1511-Oct-05 23:44
jonathan1511-Oct-05 23:44 
GeneralRe: I wanted to learn VB.NET Pin
K Baruah16-Oct-05 20:37
K Baruah16-Oct-05 20:37 
QuestionTree Structure Pin
dave@ennead11-Oct-05 20:21
dave@ennead11-Oct-05 20:21 
AnswerRe: Tree Structure Pin
Briga11-Oct-05 22:57
Briga11-Oct-05 22:57 
QuestionUncompress Tar.Z Pin
shivakumar_m11-Oct-05 19:52
shivakumar_m11-Oct-05 19:52 
AnswerRe: Uncompress Tar.Z Pin
Dave Kreskowiak12-Oct-05 1:19
mveDave Kreskowiak12-Oct-05 1:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.