I have a function within an application where a client connected to a
server continuously sends data. The code on the server side is of the
following form:
Socket socket = receiveSocket;
NetworkStream networkStream = new NetworkStream (socket);
System.IO.Strea mReader streamReader = new System.IO.Strea mReader
(networkStream) ;
while (socket.Connect ed)
{
streamReader.Re adLine();
}
// Do something else when no longer connected
I would like the server to stop reading from the buffer if the client
closes the connection. Instead, the server continues to read blank
lines continuously. No exceptions are thrown. How can I stop this
behaviour and obtain the behaviour that I would like? 2 2765
White Spirit,
I find that when it comes to socket-based streams, you can't use stream
readers to do this. They try and buffer bytes from the stream, no matter
what the underlying stream is. Since the socket disconnects, and you call
ReadLine, the StreamReader hangs on the call to Read on the stream, waiting
for the new line combo.
I recommend using the NetworkStream, and reading byte by byte yourself,
parsing apart the new line on your own. If the socket disconnects, then no
more bytes will be returned to you (if the socket is closed).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"White Spirit" <ws*****@homech oice.co.ukwrote in message
news:45******** @news1.homechoi ce.co.uk...
>I have a function within an application where a client connected to a server continuously sends data. The code on the server side is of the following form:
Socket socket = receiveSocket;
NetworkStream networkStream = new NetworkStream (socket);
System.IO.Strea mReader streamReader = new System.IO.Strea mReader
(networkStream) ;
while (socket.Connect ed)
{
streamReader.Re adLine();
}
// Do something else when no longer connected
I would like the server to stop reading from the buffer if the client
closes the connection. Instead, the server continues to read blank lines
continuously. No exceptions are thrown. How can I stop this behaviour
and obtain the behaviour that I would like?
Nicholas Paldino [.NET/C# MVP] wrote:
I recommend using the NetworkStream, and reading byte by byte yourself,
parsing apart the new line on your own. If the socket disconnects, then no
more bytes will be returned to you (if the socket is closed).
That would solve the problem in terms of program execution. I don't
understand why Socket.Connecte d wouldn't return false in the example
code, though. In the program I'm writing, it will leave a thread
running unnecessarily and because of the way the data is expected to be
received, I can't use a timeout to close it. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rajarshi Guha |
last post by:
Hi,
I'm writing some code using PyGTK2 that uses inpu_add_full() to watch a
socket. I've included the code below. I have two questions:
1) What does a condition == 16 mean? It does match any of the
gtk.gdk.INPUT_* values. What is the use of the condition variable in the
callback?
2) The source variable in the call back is an fd. I have some trivial code
that is able to read from a socket:
|
by: Daniel |
last post by:
after opening socket, sending data then closing socket 3000 times i get
"Only one usage of each socket address"
what am i doing wrong? is there some thing else i need to do to free up the
socket after i send data into it?
I simply want to open socket, send data, close socket and have the server
just handle one client thread to recieve connection, recieve data, and close
socket
|
by: Galore |
last post by:
Hello,
I'm trying to make a simple socket application, something like a chat
application, but when I run it, it shows the following error when the client
connects to the server: "Unable to read data from the transport
connection.". I'm posting the code, so if somebody can help I'll be very
grateful.
private void DoListen()
{
|
by: Patrick Olurotimi Ige |
last post by:
With the code below i get error:-
Invalid attempt to read data when reader is closed.
//Get a datareader
SqlDataReader objDataReader;
objDataReader =
objCommand.ExecuteReader(CommandBehavior.CloseConnection);
datalistoutput.DataSource = objDataReader;
|
by: Josema |
last post by:
Hi to all,
Im searching a hand to solve an Exception that i get when i try to give the
user to a gmail account...
Any help would be appreciated.. Thanks in advance
This is a piece of my code:
Server=new TcpClient("pop.gmail.com",995); //stablish a connection
| |
by: Macca |
last post by:
Hi,
I have a synchronous socket server which my app uses to read data from
clients.
To test this I have a simulated client that sends 100 byte packets.
I have set up the socket server so that its buffer is bigger than this.
I did this expecting the data to be read in one pass.
|
by: Macca |
last post by:
My app has an asynchronous socket server. It will have 20 clients connected
to the server. Each client sends data every 500 millisecondsThe Connections
once established will not be closed unless there is a problem with the
connection.
I need to know which client has sent the incoming data as each client has
its own buffer on my "server" app.
I am using the standard asynch socket code from MSDN to listen for
connections and they...
|
by: Dirk Reske |
last post by:
Hello,
in msdn stands, that the socket.available property can fire a
SocketException when the remote machine has closed the connection.
why can?
I have to check the number of receivable bytes bevor I receive them, but
when remote closes the connection, it ends in an endless loop...
while(socket.Available < RequiredBytes) {}
|
by: Arno |
last post by:
reposted with the right microsoft managed newsgroup ID: Sorry for the
inconvinience
Hi,
I've written a class for client-socket connection, but I get a lot of times
the error message "Unable to read data from the transport connection" when
restart reading the stream with socket.BeginRead in the Sub
SocketIncomingMsg. I'm debugging now for weeks, but I can't detect where
it's
|
by: Giulio Petrucci |
last post by:
Hi everytbody,
I'm experiencing some problems trying to asynchronously read data from a
Socket object. What I do now is:
1) call th BeginReceive(...) method on the Socket object;
2) within the asynchronous callback method, call the EndReceive method,
getting the number of read bytes (stored in a variable called rb)
3) if (rb>0) I can suppose there are still some other bytes to be read,
so call again the BeginReceive(...) method, else...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |