Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. hi all

hi all

Scheduled Pinned Locked Moved C#
sysadmindata-structureshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    magnifique
    wrote on last edited by
    #1

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

    R 1 Reply Last reply
    0
    • M magnifique

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

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups