G
Guest
i am writing a very simple c/s socket program, i just wanted the client to
first write to the server, after server get the message it, write to the
client, the the client write again. here's the codes:
Server:
---------------------------------------------
NetworkStream ns = this.client.GetStream();
StreamReader reader = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string kidinfo = reader.ReadLine();
sw.Write("a");
sw.Flush();
ns.Flush();
sw.Close();
reader.ReadLine();
---------------------------------------
Client:
--------------------------------------
NetworkStream ns = this.client.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
sw.Write(this.interval.ToString());
sw.Flush();
sw.Close();
ns.Flush();
string ps = sr.ReadLine();
sw.Write("ok");
sw.Flush();
sw.Close();
--------------------------------------------------
running them, exceptions came out at the secong "sr.ReadLine()" of the
server and the first "sr.ReadLine()" of the client telling me that "Cannot
access a disposed object.", if i renew the StreamReader as "sr = new
StreamReader(ns)", exception came out telling me "stream is not readable".
it seems that the StreamReader can not work well after the StreamWriter
wrote something.
who can tell me why and how to do this?
thx in advanced
first write to the server, after server get the message it, write to the
client, the the client write again. here's the codes:
Server:
---------------------------------------------
NetworkStream ns = this.client.GetStream();
StreamReader reader = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
string kidinfo = reader.ReadLine();
sw.Write("a");
sw.Flush();
ns.Flush();
sw.Close();
reader.ReadLine();
---------------------------------------
Client:
--------------------------------------
NetworkStream ns = this.client.GetStream();
StreamReader sr = new StreamReader(ns);
StreamWriter sw = new StreamWriter(ns);
sw.Write(this.interval.ToString());
sw.Flush();
sw.Close();
ns.Flush();
string ps = sr.ReadLine();
sw.Write("ok");
sw.Flush();
sw.Close();
--------------------------------------------------
running them, exceptions came out at the secong "sr.ReadLine()" of the
server and the first "sr.ReadLine()" of the client telling me that "Cannot
access a disposed object.", if i renew the StreamReader as "sr = new
StreamReader(ns)", exception came out telling me "stream is not readable".
it seems that the StreamReader can not work well after the StreamWriter
wrote something.
who can tell me why and how to do this?
thx in advanced