Click here to Skip to main content
15,894,291 members
Home / Discussions / C#
   

C#

 
QuestionBinaryReader stuck on reading from NetworkStream Pin
Ady Shimony20-Apr-09 15:41
Ady Shimony20-Apr-09 15:41 
Hi all

I am having a strange problem.
I am writing a simple chat, client side and server side (it for testing and learning)

When trying to read a string from the NetworksStream using a BinaryReader, works ok if I am sending data from the client. if there is no data, the application hangs...

since I want to listen in one side and read all the time from the stream until the data will arrive, I have a little problem.

Why is it hang?

Here is the Code of the Chat module, after a good connect, when I first send data from the client and then read a the server - working good. try to read without sending the data first - hang for good...




namespace ChatEngine
{
public class Chat
{

static private BinaryReader binaryReader;

static private BinaryWriter binaryWriter;


static bool connected = false;

static public void WriteLine(string message)
{
if (connected)
binaryWriter.Write(message);

}

static public string ReadLine()
{
if (connected)
{
// this is the line that making the problems.
// if the stream is empty, the application zhangs.
return binaryReader.ReadString();

}
else
return null;

}


static public bool connect(IPAddress ip, bool server, int port)
{

try
{
if (server)
{
TcpListener listener = new TcpListener(ip, port);

listener.Start();
TcpClient client = listener.AcceptTcpClient();
NetworkStream ns = client.GetStream();

binaryReader = new BinaryReader(ns);
binaryWriter = new BinaryWriter(ns);

connected = true;

return true;

}
else
{

TcpClient client = new TcpClient();

client.Connect(ip, port);
NetworkStream ns = client.GetStream();

binaryReader = new BinaryReader(ns);
binaryWriter = new BinaryWriter(ns);


connected = true;

return true;
}
}
catch (SocketException e)
{
return false;
}

}

}
}


Thanks for any help,

Ady.
AnswerRe: BinaryReader stuck on reading from NetworkStream Pin
N a v a n e e t h20-Apr-09 16:45
N a v a n e e t h20-Apr-09 16:45 
GeneralRe: BinaryReader stuck on reading from NetworkStream Pin
Ady Shimony21-Apr-09 1:49
Ady Shimony21-Apr-09 1:49 
AnswerRe: BinaryReader stuck on reading from NetworkStream Pin
Rob Philpott20-Apr-09 22:35
Rob Philpott20-Apr-09 22:35 
GeneralRe: BinaryReader stuck on reading from NetworkStream Pin
Ady Shimony21-Apr-09 1:56
Ady Shimony21-Apr-09 1:56 
GeneralRe: BinaryReader stuck on reading from NetworkStream Pin
Rob Philpott21-Apr-09 2:48
Rob Philpott21-Apr-09 2:48 
GeneralRe: BinaryReader stuck on reading from NetworkStream Pin
Ady Shimony22-Apr-09 2:45
Ady Shimony22-Apr-09 2:45 
QuestionHow to free shared resources ? Pin
Moshe T20-Apr-09 14:01
Moshe T20-Apr-09 14:01 
AnswerRe: How to free shared resources ? Pin
Mycroft Holmes20-Apr-09 14:19
professionalMycroft Holmes20-Apr-09 14:19 
AnswerRe: How to free shared resources ? Pin
N a v a n e e t h20-Apr-09 16:53
N a v a n e e t h20-Apr-09 16:53 
QuestionGraphicsPath with Linecaps Pin
dh_20-Apr-09 11:49
dh_20-Apr-09 11:49 
AnswerRe: GraphicsPath with Linecaps Pin
Luc Pattyn20-Apr-09 13:50
sitebuilderLuc Pattyn20-Apr-09 13:50 
AnswerRe: GraphicsPath with Linecaps Pin
Henry Minute21-Apr-09 1:09
Henry Minute21-Apr-09 1:09 
GeneralRe: GraphicsPath with Linecaps Pin
dh_23-Apr-09 2:06
dh_23-Apr-09 2:06 
QuestionC# Web project Crystal Reports Pin
newbie1234567891020-Apr-09 9:53
newbie1234567891020-Apr-09 9:53 
AnswerRe: C# Web project Crystal Reports Pin
Rolando CC20-Apr-09 11:20
professionalRolando CC20-Apr-09 11:20 
GeneralRe: C# Web project Crystal Reports Pin
newbie1234567891021-Apr-09 2:54
newbie1234567891021-Apr-09 2:54 
GeneralRe: C# Web project Crystal Reports Pin
Rolando CC21-Apr-09 4:30
professionalRolando CC21-Apr-09 4:30 
GeneralRe: C# Web project Crystal Reports Pin
newbie1234567891021-Apr-09 4:44
newbie1234567891021-Apr-09 4:44 
GeneralRe: C# Web project Crystal Reports Pin
Rolando CC21-Apr-09 4:57
professionalRolando CC21-Apr-09 4:57 
GeneralRe: C# Web project Crystal Reports [modified] Pin
newbie1234567891021-Apr-09 6:27
newbie1234567891021-Apr-09 6:27 
QuestionReturning powershell special variables back to the c# caller. Pin
scope_creep20-Apr-09 8:44
scope_creep20-Apr-09 8:44 
Questionsound slider Pin
yogesh_softworld12320-Apr-09 8:06
yogesh_softworld12320-Apr-09 8:06 
AnswerRe: sound slider Pin
buachaill cliste20-Apr-09 8:59
buachaill cliste20-Apr-09 8:59 
GeneralRe: sound slider Pin
yogesh_softworld12320-Apr-09 9:19
yogesh_softworld12320-Apr-09 9:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.