473,511 Members | 16,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Probably simple problem with networking

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

Nov 17 '05 #1
4 1420


Tomas Machala wrote:
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.
return Encoding.ASCII.GetString(received);
}


--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #2


Tomas Machala wrote:
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.
return Encoding.ASCII.GetString(received);
}


--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #3
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.*******@tiscali.cz> wrote in message
news:OE***************@TK2MSFTNGP12.phx.gbl...
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();
}
}
}
Nov 17 '05 #4
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.*******@tiscali.cz> wrote in message
news:OE***************@TK2MSFTNGP12.phx.gbl...
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();
}
}
}
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3803
by: Nuno Silva | last post by:
Hello, I'm starting to learn about java networking. What I want to do is for now, just make a server that listens to connections through a port, and accepts it's connections from the client. ...
4
6564
by: Stefan Poehn | last post by:
Hi does anybody know a tool that converts Java-Code to C++? I need a very simple converter that does NOT need to cover Threading, Swing, Exceptions, Reflection, RMI, Security, Networking and...
1
1533
by: Brian Henry | last post by:
Hello, I was tring to learn socket's (being i never used them before) and have a simple question. I want to create a listner that will get any data recieved and print it out. I've been able to...
8
1482
by: Martin Randall | last post by:
Hi, I'm trying to get a simple dataview test page working on my hosted site. The hosted site runs ASP.Net 2.0, no problem. The test page works on my development machine, no problem... but when I...
0
1243
by: Link | last post by:
hello i want to make simple server client system that send data like that one : http://www.eggheadcafe.com/articles/20020323.asp just no console app well i want to make form with 2...
0
1795
by: abdulics | last post by:
Hi, We have immediate openings with one of our Top Notch Telecom clients for their Research and Development Center in Bangalore. Please send me your detailed/brief profile to...
0
1662
by: abdulics | last post by:
Hi, We have immediate openings with one of our Top Notch Telecom clients for their Research and Development Center in Bangalore. Our top notch and reputed client deliver complex IT solutions on...
0
1918
by: mandarkraftware | last post by:
Hi All, Let me present you with list of consultants currently available. Name / Job Title Summary Ramchandran/Java · 7+ years experience in the field of Software Development. · Expertise...
80
2846
by: pereges | last post by:
Hello, I have the following structure - typedef struct { double x, y, z; }vector; In certain places, I could avoid triplification of code by using an array instead of x, y, z. For eg:
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7418
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.