I am trying to replce the follwing code that works fine
TcpClient oC = new TcpClient(ip, port); oC = new TcpClient(ip, port); StreamReader messageReader; try { messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII); reply = messageReader.ReadLine(); }
with
try { using (messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII)) { reply = messageReader.ReadLine(); } }
But I get an InvalidOperationException saying
The operation is not allowed on non-connected sockets.
What is the problem and how can I fix it?
More: I have oc.Connect before this code, so I am connected and when It wants to be used for the first time is works fine, it is only after that that I get that Exception, I played around a bit with it and now I get:
Cannot access a disposed object.\r\nObject name: ‘System.Net.Sockets.Socket’.
Just reiterating my comment on Jareds answer.
Ali Commented:
I Commented: