Sending XML data via socket

M

Markgoldin

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
 
N

Nicholas Paldino [.NET/C# MVP]

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?
 
M

Markgoldin

<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)

Markgoldin said:
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
 
I

Ignacio Machin ( .NET/ C# MVP )

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

can you post the code you are using to get it from the network stream?
Are you calling WriteLine somewhere?
 
M

Markgoldin

My code is posted in my message "Multiple socket clients".
Let me know if you can't see it I will post it here.

Thanks for the help.

message
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

can you post the code you are using to get it from the network stream?
Are you calling WriteLine somewhere?
 
N

Nicholas Paldino [.NET/C# MVP]

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)

Markgoldin said:
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
 
M

Markgoldin

What I am trying to do is to read the whole lot into a buffer.
Can I somehow read untill a special character is met? Something like char
13?
That way I dont have to send a number of bytes to read.

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
 
N

Nicholas Paldino [.NET/C# MVP]

Markgoldin,

You could, but the problem with that is that you would have to read from
the stream one character at a time (or rather, one byte at a time,
converting to a character when you have enough bytes). The reason for this
is that if you have say, 10 characters left in the stream, and you issue a
read for 100 bytes, the call will block until more bytes come in on the
stream.

This is why putting the length prefix in is a better option.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Markgoldin said:
What I am trying to do is to read the whole lot into a buffer.
Can I somehow read untill a special character is met? Something like char
13?
That way I dont have to send a number of bytes to read.

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.


in message 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
 
I

Ignacio Machin ( .NET/ C# MVP )

My code is posted in my message "Multiple socket clients".
Let me know if you can't see it I will post it here.

Thanks for the help.

message


can you post the code you are using to get it from the network stream?
Are you calling WriteLine somewhere?- Hide quoted text -

- Show quoted text -

post the code here
 
M

Markgoldin

Here is code I am using to create a socket listener:
public void Run()

{

go = true;

Random rand = new Random();

// Buffer for reading data

Byte[] bytes = new Byte[1024];

String data = null;

while (go)

{

TcpClient tcpc = FromAnotherClass.tcpl.AcceptTcpClient(); //accept
connection

data = null;

// Get a stream object for reading and writing

NetworkStream stream = tcpc.GetStream();

// Loop to receive all the data sent by the client.

int i;

while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)

{

// Translate data bytes to a ASCII string.

data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

// Process the data sent by the client.

if (data != "")

{

System.Console.WriteLine(data);

}

}

Thread.Sleep(1000 + rand.Next(2000));

}

}

message
My code is posted in my message "Multiple socket clients".
Let me know if you can't see it I will post it here.

Thanks for the help.

message


can you post the code you are using to get it from the network stream?
Are you calling WriteLine somewhere?- Hide quoted text -

- Show quoted text -

post the code here
 
M

Markgoldin

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
 

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