Ok, I looked int a link you have provided.
I am trying to adapt one of the examples from that link. Here is my code:
TcpClient tcpc = DataAdapterLauncher.tcpl.AcceptTcpClient(); //accept
connection
// Get a stream object for reading and writing
NetworkStream stream = tcpc.GetStream();
Byte[] buffer = new Byte[32768];
// Loop to receive all the data sent by the client.
int chunk;
int read = 0;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) != 0)
{
read += chunk;
System.Console.WriteLine(read);
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
System.Console.WriteLine("read2");
// End of stream? If so, we're done
if (nextByte == -1)
{
// Process the data sent by the client.
// Translate data bytes to a ASCII string.
System.Console.WriteLine("read4");
break;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
System.Console.WriteLine("read3");
}
}
In the Console it only says "read".
Here is the code fro the link:
/// <summary>
/// Reads data from a stream until the end is reached. The
/// data is returned as a byte array. An IOException is
/// thrown if any of the underlying IO calls fail.
/// </summary>
/// <param name="stream">The stream to read data from</param>
/// <param name="initialLength">The initial buffer length</param>
public static byte[] ReadFully (Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read=0;
int chunk;
while ( (chunk = stream.Read(buffer, read, buffer.Length-read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte==-1)
{
return buffer;
}
// Nope. Resize the buffer, put in the byte we've just
// read, and continue
byte[] newBuffer = new byte[buffer.Length*2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read]=(byte)nextByte;
buffer = newBuffer;
read++;
}
}
// Buffer is now too big. Shrink it.
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
I jsut can't figure out where my code is wrong.Can you please provide
help?Thanks.
Nicholas Paldino said:
Generally, you should post the code in the same thread. The reason for
this is that there can be a LOT of threads in a newsgroup and a lot of
messages.
Looking at the code you posted, how do you know how many total bytes
make up the message? In other words, when a message is sent, when do you
know the XML ends? In a situation like this, you should be sending the
length of the message first to indicate the number of bytes that make up
the message, and then read that number of bytes.
Also, it's not guaranteed that the Stream will return the number of
bytes that you request to read. Because of that, you might have to call
it multiple times. Check out Jon Skeet's site which goes into more
detail:
http://www.yoda.arachsys.com/csharp/readbinary.html
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Markgoldin said:
<Are there actual line breaks
that are being injected into the stream somehow
Yes, I think that is what happens. I am not sending line breaks at these
positions.
<What is the code that you are using to call from the stream?
I have my code posted in "Multiple socket clients" message.
If you can't see it I will post it again.
Thanks alot for help.
Nicholas Paldino said:
Markgoldin,
What are the line breaks representing? Are there actual line breaks
that are being injected into the stream somehow, or does it symbolize
the breaks in what you are reading from the server?
What is the code that you are using to call from the stream? Are you
checking the return value to see that all of the bytes that you
requested to read are actually being read, or are you just assuming that
the bytes are being read completely?
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
I am sending an XML data from not dontnet process to a .Net via socket
listener.
Here is a data sample:
<VFPData>
<serverdata>
<coderun>updateFloor</coderun>
<area>MD2</area>
<zone>BOXING</zone>
<status>Running</status>
<job>1000139233</job>
<sku>02219</sku>
<finished>88</finished>
</serverdata>
</VFPData>
My problem is that sometimes it come out of a listner like this:
<VFPData>
<serverdata>
<coderun>updateFloor</coderun>
<area>MD2</area>
<zone>BOXING</zone>
<status>Running</status>
<job>1000139233</job>
<sku>02219</sku>
<finished>88</finished>
</serverda
ta>
</VFPData>
or like this:
<VFPData>
<serverdata>
<coderun>updateFloor</coderun>
<area>MD2</area>
<zone>BOXING</zone>
<status>Running</status>
<job>1000139233</job>
<sku>02219</sku>
<fini
shed>92</finished>
</serverdata>
</VFPData>
I am sure that the sending process sends data in a proper format all
the time.
What do I need to do to keep XML well formed?
Thanks