Probably simple problem with networking

T

Tomas Machala

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();
}
}
}
 
H

Helge Jensen

Tomas said:
Hi, I'm trying to make an application communicating over TCP/IP. It

Why not use the TcpClient and friends?

public string Read()
{
received = new byte[client.ReceiveBufferSize];
nstr.Read(received, 0, client.ReceiveBufferSize);

read returns the number of bytes received, you ignore that.
 
N

Nick Malik [Microsoft]

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.
--
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();
}
}
}
 

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