Re: Probably simple problem with networking



it is probably not printing a few carriage returns. It is probably
printing over 300 blank characters, and simply wrapping the lines. You are
ignoring the number of bytes returned and you are converting the entire
buffer to a single string. You may want to truncate your string to the
number of actual bytes received.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Tomas Machala" <t.machala@xxxxxxxxxx> wrote in message
news:OEBLSm%23pFHA.748@xxxxxxxxxxxxxxxxxxxxxxx
Hi, I'm trying to make an application communicating over TCP/IP. It should
do only one thing - write received data to console and terminate itself when
"exit" received. Problem is that if I send some string to this application,
it'll receive that string and many unwanted "new line" characters on its
end.

Examle:

If I send

"some_string"

application display

"some_string






"

I can't find the reason why it does so. Here's my code:
using System;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace NetPokus
{
class NetPokus
{
[STAThread]
static void Main(string[] args)
{
string recv;
Odpovedi odp;
try
{
odp = new Odpovedi();
}
catch (SocketException ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
return;
}
while (true)
{
odp.Write("Waiting for command\n");
recv = odp.Read();
if (recv == "exit")
{
break;
}
else
{
Console.Write(recv);
}
}
odp.Close();
}
}
public class Odpovedi
{
TcpClient client;
NetworkStream nstr;
byte[] received;
byte[] toSend;
public Odpovedi()
{
client = new TcpClient("127.0.0.1", 1234);
nstr = client.GetStream();
}
public void Write(string msg)
{
toSend = Encoding.ASCII.GetBytes(msg);
nstr.Write(toSend, 0, toSend.Length);
}
public string Read()
{
received = new byte[client.ReceiveBufferSize];
nstr.Read(received, 0, client.ReceiveBufferSize);
return Encoding.ASCII.GetString(received);
}
public void Close()
{
client.Close();
}
}
}


.



Relevant Pages

  • Re: printing variable problems
    ... Marco van de Voort said ... printing a string that is prefixed with a given number of space ... characters, does not always start at the same position? ...
    (comp.lang.pascal.borland)
  • Re: A question in an interview
    ... Parsing them out of the string, ... with nested loops incrementing along the string. ... Since one is using 'printing' as a criteria, start at the back a string ... >>Implement a function that prints all posible combinations of the characters ...
    (microsoft.public.vc.mfc)
  • RE: replacing " within a string
    ... In your code you are trying to replace few characters. ... string is immutable. ... Try this example and try to view the value of myString after printing SSS. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: shortening a string
    ... > When printing it out, I want it to be limited to 10 characters ... > I only want this to happen when the string is longer than 10 ... I know how to do it in Perl through reg ...
    (comp.lang.php)
  • Re: Prothon should not borrow Python strings!
    ... """It does not make sense to have a string without knowing what encoding ... same cul de sac as Python. ... Prothon_String_As_ASCII // raises error if there are high characters ... Python's split between byte strings and Unicode strings is ...
    (comp.lang.python)