Making a constant listening tcp server tcp

Jeffo

Member
Joined
Apr 26, 2005
Messages
9
Programming Experience
1-3
I was wondering would adding a while loop that is always true with a tcp listener inside would allow it to always listen for connecitons off a client?
As at the moment with the method i am tryin it only listen to a client once then stops listening
Can anyone see what I am doing wrong?

VB.NET:
[size=2][color=#0000ff]Dim[/color][/size][size=2] start [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] [/size][size=2][color=#0000ff]Boolean

[/color][/size][size=2]start = [/size][size=2][color=#0000ff]True

[/color][/size][size=2][/size][size=2][color=#008000]' Must listen on correct port- must be same as port client wants to connect on.

[/color][/size][size=2][/size][size=2][color=#0000ff]Const[/color][/size][size=2] portNumber [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 8000

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] TcpListener [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] TcpListener(portNumber)

TcpListener.Start()

[/size][size=2][color=#0000ff]While[/color][/size][size=2] (start)

Console.WriteLine("Waiting for connection...")

[/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#008000]'Accept the pending client connection and return a TcpClient initialized for communication. 

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] tcpClient [/size][size=2][color=#0000ff]As[/color][/size][size=2] TcpClient = TcpListener.AcceptTcpClient()

Console.WriteLine("Connection accepted.")

[/size][size=2][color=#008000]'Get the stream

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] networkStream [/size][size=2][color=#0000ff]As[/color][/size][size=2] NetworkStream = tcpClient.GetStream()

[/size][size=2][color=#008000]' Read the stream into a byte array

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] bytes(tcpClient.ReceiveBufferSize) [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Byte

[/color][/size][size=2]networkStream.Read(bytes, 0, [/size][size=2][color=#0000ff]CInt[/color][/size][size=2](tcpClient.ReceiveBufferSize))

[/size][size=2][color=#008000]' Return the data received from the client to the console.

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] clientdata [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = Encoding.Unicode.GetString(bytes)

Console.WriteLine(("Client sent: " + clientdata))

[/size][size=2][color=#008000]

[/color][/size][size=2][/size][size=2][color=#008000]

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] responseString [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2] = "Connected to server."[/size][size=2][color=#008000]

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] ipadd [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer[/color][/size][size=2] = 998

sConString = tcpser.ConnectionString()

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] conName2 [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbConnection(sConString)

conName2.Open()

[/size][size=2][color=#0000ff]Try

[/color][/size][size=2]Console.WriteLine(("Client data sent: " + clientdata))

sSQL = "INSERT INTO Data (IPAddress,[Latency]) VALUES ('" & clientdata & "','" & ipadd & "')"

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] objDataAdapter [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbDataAdapter(sSQL, conName2)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] cmd [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] OleDb.OleDbCommand(sSQL, conName2)

[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sqlrep [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]Integer

[/color][/size][size=2]sqlrep = cmd.ExecuteNonQuery()

[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] ex [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

System.Console.WriteLine(ex.ToString)

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2]conName2.Close()

[/size][size=2][color=#008000][/color][/size][size=2][color=#008000]

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sendBytes [/size][size=2][color=#0000ff]As[/color][/size][size=2] [Byte]() = Encoding.ASCII.GetBytes(clientdata)

networkStream.Write(sendBytes, 0, sendBytes.Length)

Console.WriteLine(("Message Sent /> : " + clientdata))

tcpClient.Close()

Console.WriteLine("exit")

Console.ReadLine()

[/size][size=2][color=#0000ff]Catch[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] Exception

Console.WriteLine(e.ToString())

Console.ReadLine()

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Try

[/color][/size][size=2][/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]While

[/color][/size][size=2]TcpListener.Stop()

[/size]
 
Top Bottom