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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. TCP Client Persistent Conection

TCP Client Persistent Conection

Scheduled Pinned Locked Moved C#
csharpdotnetadobeagentic-aiquestion
3 Posts 2 Posters 1 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.
  • S Offline
    S Offline
    SakthiSurya
    wrote on last edited by
    #1

    Hi, I am using TCPClient for requesting web pages. ex: string Ahost = "http://www.Google.co.in"; Uri lastURI=new Uri(Ahost); TcpClient client = new TcpClient(); string host = lastURI.Host; string temp = lastURI.PathAndQuery; client.Connect(host, lastURI.Port); NetworkStream stream = client.GetStream(); string str = @"GET " + temp + " HTTP/1.1" + "\r\n" + @"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" + " \r\n" + @"Accept-Language: en-us" + "\r\n" + @"Accept-Encoding: gzip, deflate" + "\r\n" + @"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" + "\r\n" + @"Host: " +host+ "\r\n" + @"Connection: Keep-Alive" + "\r\n\r\n"; byte[] send = Encoding.ASCII.GetBytes(str); try { if (stream.CanWrite) { stream.Write(send, 0, send.Length); } } catch (System.IO.IOException ioe) { Console.WriteLine(ioe.Message); } byte[] bytesToRead = new byte[client.ReceiveBufferSize]; MemoryStream ms = new MemoryStream(); StringBuilder sbuild = new StringBuilder(); do { try { int numBytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize); Console.WriteLine(numBytesRead.ToString()); ms.Write(bytesToRead, 0, numBytesRead); Console.WriteLine(ms.Length); } catch (Exception e) { Console.WriteLine("No Data Found"); } }while(stream.DataAvailable); ms.Seek(0, 0); StreamReader sr = new StreamReader(ms,Encoding.ASCII); string Result = sr.ReadToEnd(); sr.Close(); ms.Close(); stream.Close(); client.Close(); Console.WriteLine(Result); } } In the above case How can i use the same TCPClient object for more than one request provided the host is same.(in case of requests like http://www.google.co.in/ http://www.google.co.in/intl/en\_com/images/logo\_plain.png http://www.google.co.in/images/nav\_logo3.png ) Please H

    S 1 Reply Last reply
    0
    • S SakthiSurya

      Hi, I am using TCPClient for requesting web pages. ex: string Ahost = "http://www.Google.co.in"; Uri lastURI=new Uri(Ahost); TcpClient client = new TcpClient(); string host = lastURI.Host; string temp = lastURI.PathAndQuery; client.Connect(host, lastURI.Port); NetworkStream stream = client.GetStream(); string str = @"GET " + temp + " HTTP/1.1" + "\r\n" + @"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" + " \r\n" + @"Accept-Language: en-us" + "\r\n" + @"Accept-Encoding: gzip, deflate" + "\r\n" + @"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" + "\r\n" + @"Host: " +host+ "\r\n" + @"Connection: Keep-Alive" + "\r\n\r\n"; byte[] send = Encoding.ASCII.GetBytes(str); try { if (stream.CanWrite) { stream.Write(send, 0, send.Length); } } catch (System.IO.IOException ioe) { Console.WriteLine(ioe.Message); } byte[] bytesToRead = new byte[client.ReceiveBufferSize]; MemoryStream ms = new MemoryStream(); StringBuilder sbuild = new StringBuilder(); do { try { int numBytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize); Console.WriteLine(numBytesRead.ToString()); ms.Write(bytesToRead, 0, numBytesRead); Console.WriteLine(ms.Length); } catch (Exception e) { Console.WriteLine("No Data Found"); } }while(stream.DataAvailable); ms.Seek(0, 0); StreamReader sr = new StreamReader(ms,Encoding.ASCII); string Result = sr.ReadToEnd(); sr.Close(); ms.Close(); stream.Close(); client.Close(); Console.WriteLine(Result); } } In the above case How can i use the same TCPClient object for more than one request provided the host is same.(in case of requests like http://www.google.co.in/ http://www.google.co.in/intl/en\_com/images/logo\_plain.png http://www.google.co.in/images/nav\_logo3.png ) Please H

      S Offline
      S Offline
      subrata jana
      wrote on last edited by
      #2

      string[] str1 = new string[3] { "http://www.google.co.in/", "http://www.google.co.in/intl/en\_com/images/logo\_plain.png", "http://www.google.co.in/images/nav\_logo3.png" }; string Ahost = "http://www.Google.co.in"; Uri lastURI=new Uri(Ahost); TcpClient client = new TcpClient(); string host = lastURI.Host; client.Connect(host, lastURI.Port); NetworkStream stream = client.GetStream(); for (int i = 0; i < str1.Length; i++) { string temp = new Uri(str1[i]).PathAndQuery; string str = @"GET " + temp + " HTTP/1.1" + "\r\n" + @"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" + " \r\n" + @"Accept-Language: en-us" + "\r\n" + @"Accept-Encoding: gzip, deflate" + "\r\n" + @"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" + "\r\n" + @"Host: " + host + "\r\n" + @"Connection: Keep-Alive" + "\r\n\r\n"; byte[] send = Encoding.ASCII.GetBytes(str); try { if (stream.CanWrite) { stream.Write(send, 0, send.Length); } } catch (System.IO.IOException ioe) { Console.WriteLine(ioe.Message); } byte[] bytesToRead = new byte[client.ReceiveBufferSize]; MemoryStream ms = new MemoryStream(); StringBuilder sbuild = new StringBuilder(); do { try { int numBytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize); Console.WriteLine(numBytesRead.ToString()); ms.Write(bytesToRead, 0, numBytesRead); Console.WriteLine(ms.Length); } catch (Exception e1) { Console.WriteLine("No Data Found"); } } while (stream.DataAvailable); ms.Seek(0, 0); StreamReader sr = new StreamReader(ms, Encoding.ASCII); string Result = sr.ReadToEnd(); sr.Close(); ms.Close(); //stream.Close(); stream.Flush(); Console.WriteLine(Result); } client.Close();

      S 1 Reply Last reply
      0
      • S subrata jana

        string[] str1 = new string[3] { "http://www.google.co.in/", "http://www.google.co.in/intl/en\_com/images/logo\_plain.png", "http://www.google.co.in/images/nav\_logo3.png" }; string Ahost = "http://www.Google.co.in"; Uri lastURI=new Uri(Ahost); TcpClient client = new TcpClient(); string host = lastURI.Host; client.Connect(host, lastURI.Port); NetworkStream stream = client.GetStream(); for (int i = 0; i < str1.Length; i++) { string temp = new Uri(str1[i]).PathAndQuery; string str = @"GET " + temp + " HTTP/1.1" + "\r\n" + @"Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*" + " \r\n" + @"Accept-Language: en-us" + "\r\n" + @"Accept-Encoding: gzip, deflate" + "\r\n" + @"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" + "\r\n" + @"Host: " + host + "\r\n" + @"Connection: Keep-Alive" + "\r\n\r\n"; byte[] send = Encoding.ASCII.GetBytes(str); try { if (stream.CanWrite) { stream.Write(send, 0, send.Length); } } catch (System.IO.IOException ioe) { Console.WriteLine(ioe.Message); } byte[] bytesToRead = new byte[client.ReceiveBufferSize]; MemoryStream ms = new MemoryStream(); StringBuilder sbuild = new StringBuilder(); do { try { int numBytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize); Console.WriteLine(numBytesRead.ToString()); ms.Write(bytesToRead, 0, numBytesRead); Console.WriteLine(ms.Length); } catch (Exception e1) { Console.WriteLine("No Data Found"); } } while (stream.DataAvailable); ms.Seek(0, 0); StreamReader sr = new StreamReader(ms, Encoding.ASCII); string Result = sr.ReadToEnd(); sr.Close(); ms.Close(); //stream.Close(); stream.Flush(); Console.WriteLine(Result); } client.Close();

        S Offline
        S Offline
        SakthiSurya
        wrote on last edited by
        #3

        Hi Subrata, Actually in my case i may not know the URLs in advance. If the host of requested url is same as the host of Previous Url I should be able to use the same connection. I tried the same without closing the underlying connection. While reading for the next request,i am getting an exception as Connection closed. Is it possible set the connection timeout. Sakthi.

        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