Vb.net Tcp/ip

seen_44

New member
Joined
Sep 5, 2006
Messages
2
Programming Experience
1-3
Hello All,

I am wanting to transfer messages between a VB.NET application (App1) and a VB.NET application on a hand held device (PDA) (App2)

I can transfer data from 1 to the other, although I cant seem to keep the server session (App1) open and continuley listening for incoming connections.

Each time data is sent to the server (App1) from the client (App2) the server sends back a reply and then closes.

Im sure this is probably something simple, although I have been trying it for a while now and never seem to get anywhere.

The code I currently have is as follows:

Server Code (App1)

VB.NET:
Imports System.Net.Sockets
Imports System.Text
 
Class TCPSrv
SharedSub Main()
' Must listen on correct port- must be same as port client wants to connect on.
Const portNumber AsInteger = 8000
'Dim ipAdd As System.net.IPAddress = 127.0.0.1
Dim tcpListener AsNew TcpListener(portNumber)
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return a TcpClient initialized for communication. 
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) AsByte
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata AsString = Encoding.ASCII.GetString(bytes)
Console.WriteLine(("Client sent: " + clientdata))
Dim responseString AsString = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString))
 
'********************************
'* Code needs to be added in here somewhere to continusly listen for incoming data 
'********************************
 
 
tcpClient.Close()
tcpListener.Stop()
Console.WriteLine("exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
EndTry
EndSub
EndClass

Thank you in advance for your help.
 
Last edited by a moderator:
I'm new to this, but could it be where you are invoking the tcpListener.stop method that is causing your problem?
 
The stop method is what is stopping the listener,
I belive I need some sort of a loop before the stop to continuously check for incoming connections and take the appropriate action with them.

Not sure exactly how to do this.

Thank you
 
Try this

Sorry if im not easy to understand, Its early morning and I havent been to sleep

Im new to programming but I think this might give you some Ideas.

Put the code from the "Try ~ Catch" in a function that returns the received messaged, this Try Catch should be in side a while loop. Im not sure if this will work as I have never used sockets before, but the code within and including the loop should be inside a fuction and should return the string which should then be printed and the function called again,
This function should be called from a loop and shouldnt be stopped until the chat has ended....
Note how I have put the stop command after the loop, this should be done within the function inside an if statement maybe, theres many ways todo this I think...

Imports System.Net.Sockets
Imports System.Text

Class TCPSrv
SharedSub Main()
' Must listen on correct port- must be same as port client wants to connect on.
Const portNumber AsInteger = 8000
'Dim ipAdd As System.net.IPAddress = 127.0.0.1
Dim tcpListener AsNew TcpListener(portNumber)
tcpListener.Start()
Console.WriteLine("Waiting for connection...")
Try
'Accept the pending client connection and return a TcpClient initialized for communication.
Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection accepted.")
' Get the stream
Dim networkStream As NetworkStream = tcpClient.GetStream()
' Read the stream into a byte array
Dim bytes(tcpClient.ReceiveBufferSize) AsByte

Dim listening as boolean = true

While Listening = true

networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
' Return the data received from the client to the console.
Dim clientdata AsString = Encoding.ASCII.GetString(bytes)

if not clientdata = nothing then

Console.WriteLine(("Client sent: " + clientdata))
Dim responseString AsString = "Connected to server."
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
networkStream.Write(sendBytes, 0, sendBytes.Length)
Console.WriteLine(("Message Sent /> : " + responseString))

exit while
end if
loop



tcpClient.Close()
tcpListener.Stop()
Console.WriteLine("exit")
Console.ReadLine()
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
EndTry
EndSub
EndClassThank you in advance for

Hope I helped, looks a bit hard to understand me at the moment

Good Luck :)
 
Back
Top