Hoping someone can help me here. I've got this code written, and it works
fine for the first connection. But if I connect another client (while the
first is still connected), I get connected but nothing else, no data
exchange. If I disconnect the 2nd session, I get a System.IO.IOException.
If I disconnect the first session with the 2nd session still connected, then
all the data I typed in the 2nd session gets returned to me as if it was
working fine the whole time (like it was buffered or something).
Class Server
Public Sub StartServer()
Dim SmtpListener As New TcpListener(IpAddr, Port)
SmtpListener.Start(10)
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub
Private Sub AcceptCallback(ByVal ar As IAsyncResult)
Dim SmtpListener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim client As TcpClient = SmtpListener.EndAcceptTcpClient(ar)
Dim Handler As New ClientHandler(client)
Handler.Start()
SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener)
End Sub
Private Class ClientHandler
Dim sw As StreamWriter
Dim sr As StreamReader
Dim SmtpClient as TcpClient
Public Sub New(ByVal Client As TcpClient)
SmtpClient=Client
sw = New StreamWriter(SmtpClient.GetStream)
sr = New StreamReader(SmtpClient.GetStream)
End Sub
Public Sub Start()
Write2Client("Welcome to the server.")
While sr.EndOfStream = False
Write2Client("Received: " & sr.ReadLine)
End While
Write2Client("Connection Closed")
SmtpClient.Close
End Sub
Private Sub Write2Client(ByVal msg As String)
sw.WriteLine(msg)
sw.Flush()
End Sub
End Class
End Class 2 4045
Hi,
"Terry Olsen" <to******@hotmail.com> wrote in message
news:OP**************@TK2MSFTNGP09.phx.gbl... Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but nothing else, no data exchange. If I disconnect the 2nd session, I get a System.IO.IOException.
You can get Exception's when for example the connection wasn't closed
gracefully or some error happened, so you should wrap all socket functions
inside a try-catch, when an Exception is thrown you may assume the
connection is closed.
If I disconnect the first session with the 2nd session still connected, then all the data I typed in the 2nd session gets returned to me as if it was working fine the whole time (like it was buffered or something).
Well, you are using the asynchronous function BeginAccept, but once a Socket
is accepted you start using synchronous operations like
StreamReader.ReadLine, so your program is in a while loop for one socket
until the connection is closed.
Two possible solutions :
1) Use a thread for each accepted socket, pretty easy because the logic
remains the same and you can keep using StreamReader/StreamWriter.
See modifications to your code for this.
2) Use BeginRead and BeginWrite, but then i gets more complicated because
you may need to mantain state (or buffer) between calls and you can't use
StreamReader/StreamWriter. Class Server
Public Sub StartServer() Dim SmtpListener As New TcpListener(IpAddr, Port) SmtpListener.Start(10) SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener) End Sub
Private Sub AcceptCallback(ByVal ar As IAsyncResult) Dim SmtpListener As TcpListener = CType(ar.AsyncState, TcpListener) Dim client As TcpClient = SmtpListener.EndAcceptTcpClient(ar) Dim Handler As New ClientHandler(client) Handler.Start() SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener) End Sub
Private Class ClientHandler Dim sw As StreamWriter Dim sr As StreamReader Dim SmtpClient as TcpClient
Dim thread As Thread Public Sub New(ByVal Client As TcpClient) SmtpClient=Client sw = New StreamWriter(SmtpClient.GetStream) sr = New StreamReader(SmtpClient.GetStream) End Sub
Public Sub Start
thread = new Thread( AddressOf ThreadProc )
thread.Start()
End Sub
Private Sub ThreadProc() Write2Client("Welcome to the server.") While sr.EndOfStream = False Write2Client("Received: " & sr.ReadLine) End While Write2Client("Connection Closed") SmtpClient.Close End Sub
Private Sub Write2Client(ByVal msg As String) sw.WriteLine(msg) sw.Flush() End Sub End Class
End Class
HTH,
Greetings
I get it. My program never gets past the "Handler.Start" line in the
AcceptCallback routine until the session is terminated. I assumed that
using the BeginAcceptTcpClient call would spawn a new thread and that other
things could happen. Thanks for making the lightbulb come on! :)
"Bart Mermuys" <bm*************@hotmail.com> wrote in message
news:Ob**************@tk2msftngp13.phx.gbl... Hi,
"Terry Olsen" <to******@hotmail.com> wrote in message news:OP**************@TK2MSFTNGP09.phx.gbl... Hoping someone can help me here. I've got this code written, and it works fine for the first connection. But if I connect another client (while the first is still connected), I get connected but nothing else, no data exchange. If I disconnect the 2nd session, I get a System.IO.IOException.
You can get Exception's when for example the connection wasn't closed gracefully or some error happened, so you should wrap all socket functions inside a try-catch, when an Exception is thrown you may assume the connection is closed.
If I disconnect the first session with the 2nd session still connected, then all the data I typed in the 2nd session gets returned to me as if it was working fine the whole time (like it was buffered or something).
Well, you are using the asynchronous function BeginAccept, but once a Socket is accepted you start using synchronous operations like StreamReader.ReadLine, so your program is in a while loop for one socket until the connection is closed.
Two possible solutions : 1) Use a thread for each accepted socket, pretty easy because the logic remains the same and you can keep using StreamReader/StreamWriter. See modifications to your code for this.
2) Use BeginRead and BeginWrite, but then i gets more complicated because you may need to mantain state (or buffer) between calls and you can't use StreamReader/StreamWriter.
Class Server
Public Sub StartServer() Dim SmtpListener As New TcpListener(IpAddr, Port) SmtpListener.Start(10) SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener) End Sub
Private Sub AcceptCallback(ByVal ar As IAsyncResult) Dim SmtpListener As TcpListener = CType(ar.AsyncState, TcpListener) Dim client As TcpClient = SmtpListener.EndAcceptTcpClient(ar) Dim Handler As New ClientHandler(client) Handler.Start() SmtpListener.BeginAcceptTcpClient(AddressOf AcceptCallback, SmtpListener) End Sub
Private Class ClientHandler Dim sw As StreamWriter Dim sr As StreamReader Dim SmtpClient as TcpClient
Dim thread As Thread Public Sub New(ByVal Client As TcpClient) SmtpClient=Client sw = New StreamWriter(SmtpClient.GetStream) sr = New StreamReader(SmtpClient.GetStream) End Sub
Public Sub Start thread = new Thread( AddressOf ThreadProc ) thread.Start() End Sub
Private Sub ThreadProc() Write2Client("Welcome to the server.") While sr.EndOfStream = False Write2Client("Received: " & sr.ReadLine) End While Write2Client("Connection Closed") SmtpClient.Close End Sub
Private Sub Write2Client(ByVal msg As String) sw.WriteLine(msg) sw.Flush() End Sub End Class
End Class
HTH, Greetings
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Doug Wyatt |
last post by:
So I'll preface this with the fact that I'm a UNIX developer by training and
have just recently gotten in to C# development on Windows. I'm basically
running in to a problem whereby I suspect...
|
by: Marty |
last post by:
Hi,
I have this DLL made with VB.NET made as a Class Library project. I
know this is managed code.
I have my other made with VC++.NET, unmanaged code.
The point is that I want to use...
|
by: Tim Wagaman |
last post by:
I an having issuses with a loop I am running to keep checking for
messages coming across our line.
The goal:
Listen for messages on port 5001 and print the messages into a text
file. The port...
|
by: hamil |
last post by:
I am having trouble using the TcpListener and TcpClient classes.
At the end of this post is server code that runs, and a class whose purpose
is described below.
I need to know when the client...
|
by: Jerry Spence1 |
last post by:
I have been looking at examples of code to create a simple TCP listener on a
certain port.
However they all start off with this line:
Dim tcpListener As New TcpListener(portNumber)
When I...
|
by: Bjørn Eliasen |
last post by:
Hi,
I have an application running on all pc's in our company. Basically it is a TCPListener awaiting for sockets to connect and on connection performs the required tasks. The app works fine, but...
|
by: Thos |
last post by:
When I debug a standard socket server app that has a tcplistener using
VS.NET 2005, the debugger deadlocks (uses 100% of one of my CPUs) when
the code runs the tcplistener.Start(). method. I am...
|
by: Chizl |
last post by:
I'm building a web server and having some issues with the
TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using
MS Web Stress Tool to test against my web server and when I...
|
by: Darwin |
last post by:
Setting a server to listen on 8080 for incoming connections. Written in
VS2005 on a Multi-homed machine.
I get the warning:
Warning 1 'System.Net.Sockets.TcpListener.TcpListener(int)' is obsolete:...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |