hi all
-
i am try to write tcp client an server but any time it get to the streamReader it hungs. can someone help me with these code Client Code public string RunClient(string address,int port) { string response=""; //set up a listener on that address/port TcpClient tcpClient=new TcpClient(address,port); if(tcpClient!=null) { string message="Hello There"; //translate the message into UTFASCII and store it as a byte array byte[] bytes=Encoding.ASCII.GetBytes(message); NetworkStream stream=tcpClient.GetStream(); //send message to the connected tcpServer //the write flushes the stream automatically here stream.Write(bytes,0,bytes.Length); //get the response from the server StreamReader reader=new StreamReader(stream,Encoding.UTF8); try { response=reader.ReadToEnd(); } finally { //close reader reader.Close(); } //close client tcpClient.Close(); } //return response to text return response; } Server Code public static void RunServer(string address,int port) { //set up address IPAddress addr=IPAddress.Parse(address); //set up listener on that address/port TcpListener tcpListener=new TcpListener(addr,port); if(tcpListener !=null) { //start it up tcpListener.Start(); //wait for tcp client to connect TcpClient tcpClient=tcpListener.AcceptTcpClient(); byte[]bytes=new byte[1024]; //get the client stream NetworkStream ClientStream=tcpClient.GetStream(); Stream str=ClientStream; StreamReader reader=new StreamReader(ClientStream,Encoding.UTF8); try { string request=reader.ReadToEnd(); //just send an acknowlegment bytes=Encoding.UTF8.GetBytes("Thanks for the message"); ClientStream.Write(bytes,0,bytes.Length); } finally { //close the resder reader.Close(); } //stop listeneing tcpListener.Stop(); } }
-
i am try to write tcp client an server but any time it get to the streamReader it hungs. can someone help me with these code Client Code public string RunClient(string address,int port) { string response=""; //set up a listener on that address/port TcpClient tcpClient=new TcpClient(address,port); if(tcpClient!=null) { string message="Hello There"; //translate the message into UTFASCII and store it as a byte array byte[] bytes=Encoding.ASCII.GetBytes(message); NetworkStream stream=tcpClient.GetStream(); //send message to the connected tcpServer //the write flushes the stream automatically here stream.Write(bytes,0,bytes.Length); //get the response from the server StreamReader reader=new StreamReader(stream,Encoding.UTF8); try { response=reader.ReadToEnd(); } finally { //close reader reader.Close(); } //close client tcpClient.Close(); } //return response to text return response; } Server Code public static void RunServer(string address,int port) { //set up address IPAddress addr=IPAddress.Parse(address); //set up listener on that address/port TcpListener tcpListener=new TcpListener(addr,port); if(tcpListener !=null) { //start it up tcpListener.Start(); //wait for tcp client to connect TcpClient tcpClient=tcpListener.AcceptTcpClient(); byte[]bytes=new byte[1024]; //get the client stream NetworkStream ClientStream=tcpClient.GetStream(); Stream str=ClientStream; StreamReader reader=new StreamReader(ClientStream,Encoding.UTF8); try { string request=reader.ReadToEnd(); //just send an acknowlegment bytes=Encoding.UTF8.GetBytes("Thanks for the message"); ClientStream.Write(bytes,0,bytes.Length); } finally { //close the resder reader.Close(); } //stop listeneing tcpListener.Stop(); } }
To quote mdsn http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclassreadtoendtopic.asp[^] : "ReadToEnd assumes that the stream knows when it has reached an end. For interactive protocols, in which the server sends data only when you ask for it and does not close the connection, ReadToEnd might block indefinitely and should be avoided." If your sending and receiving text only, use StreamReader and StreamWriter's ReadLine and WriteLine methods an you should be ok. Rob Philpott.