Socket - Client; Server; XML telegram problems

M

Martin Greul

Hello,

http://msdn.microsoft.com/de-de/library/system.net.sockets.networkstream.dataavailable.aspx

do{
numberOfBytesRead =
myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

myCompleteMessage.AppendFormat("{0}",
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

}
while(myNetworkStream.DataAvailable);

I thougt Socket is absolute sure, without Handshake

Problem
Client -- Server
My part -- Customer


I make a request with XML String, the response is not finish

If I add a sleep like this Thread.Sleep(120);
do{
numberOfBytesRead =
myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

myCompleteMessage.AppendFormat("{0}",
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

Thread.Sleep(120);
}
while(myNetworkStream.DataAvailable);
works well.

Customer say to me.
I must check every time, like a loop
How is the correct size of the sleep
What must socket make.
Send only Package
Telegram
Package1
Package2
Package3
Finish
or
all in the same time

Why use Microsoft the myNetworkStream.DataAvailable

How is the correct way


regards Martin
 
M

Martin Greul

Hello Pete,
[...]
Why use Microsoft the myNetworkStream.DataAvailable

I have no idea why they included that property. It's practically never
the right thing to do.
How is the correct way

Just read. When the sender has finished and is closing the connection,
you'll see a return value of "0" from the Read() method. Until then,
there might be more data, even if it's not there at that exact moment
(calling Read() will block the thread until data is available).

Google this newsgroup for "DataAvailable", "Socket", etc. for more
details. This kind of question comes up a lot.
as you make it, that you receive everything from the server?

Who is debt?
Customer - Server?
or
I - Client, because my implementation is wrong.
I need a statement for my chief.

Thanks.

Many greet Martin
++++++++++++++++++++++++++++++++++++++++++++++++++++

From the Windows Help ist this way correct.


// Check to see if this NetworkStream is readable.
if(myNetworkStream.CanRead){
byte[] myReadBuffer = new byte[1024];
StringBuilder myCompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;

// Incoming message may be larger than the buffer size.
do{
numberOfBytesRead =
myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

myCompleteMessage.AppendFormat("{0}",
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

}
while(myNetworkStream.DataAvailable);

// Print out the received message to the console.
Console.WriteLine("You received the following message : " +
myCompleteMessage);
}
else{
Console.WriteLine("Sorry. You cannot read from this
NetworkStream.");
}
 
M

Martin Greul

Hello Pete,
http://msdn.microsoft.com/de-de/library/system.net.sockets.networkstream.dataavailable.aspx
From the context, I assume you mean "who is wrong?" The phrase "who is
debt" has no useful meaning in English.
http://dict.leo.org/ende?lp=ende&la...both&pinyin=diacritic&search=schuld&relink=on

OK, I'm guilty, because I use the example from MSDN

myNetworkStream.DataAvailable is stupid!

How it is now correct?

do{
numberOfBytesRead =
myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length);

if ( numberOfBytesRead == 0 )
{
// finish, no data available
break;
}

myCompleteMessage.AppendFormat("{0}",
Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));

}
while(1);


Many greet Martin
 
D

Dan Holmes

Peter said:
[...]
Why use Microsoft the myNetworkStream.DataAvailable

I have no idea why they included that property. It's practically never
the right thing to do.
How is the correct way

Just read. When the sender has finished and is closing the connection,
you'll see a return value of "0" from the Read() method. Until then,
there might be more data, even if it's not there at that exact moment
(calling Read() will block the thread until data is available).

Google this newsgroup for "DataAvailable", "Socket", etc. for more
details. This kind of question comes up a lot.

Pete


http://msdn.microsoft.com/en-us/library/system.net.sockets.networkstream.read.aspx This is an MS example of using
DataAvailable.

Peter, i had a similar problem that i had to solve in this way (i hate this code but i couldn't figure a better way).

private void DoAcceptTcpClientCallback(IAsyncResult ar)
{
try
{
SNS.TcpListener listener = (SNS.TcpListener)ar.AsyncState;
SNS.TcpClient clientSckt = listener.EndAcceptTcpClient(ar);
listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), listener);
byte[] readbuffer = new byte[Initial_Buffer_Size];
SNS.NetworkStream ns = clientSckt.GetStream();
StringBuilder message = new StringBuilder();
int i = 0;
//kludgy way to handle the prank call.
//if a caller just calls but never sends any data the subsequent disconnect
//by the caller would not disconnect the stream nor does it throw an error.
//this is here to wait at the most 40ms for incoming data.
while (!ns.DataAvailable)
{
System.Threading.Thread.Sleep(10);
if (++i > 4) break;
}
while (ns.DataAvailable)
{
int bytesread = ns.Read(readbuffer, 0, readbuffer.Length);
message.Append(Encoding.UTF8.GetString(readbuffer, 0, bytesread));
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top