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. TcpListener Hangs at 4096 bytes?

TcpListener Hangs at 4096 bytes?

Scheduled Pinned Locked Moved C#
helpcsharpquestion
3 Posts 3 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.
  • D Offline
    D Offline
    Daniel Bright
    wrote on last edited by
    #1

    The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do). private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); } private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } } Any help would be greatly appreciated.

    H S 2 Replies Last reply
    0
    • D Daniel Bright

      The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do). private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); } private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } } Any help would be greatly appreciated.

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      You should never read this much information in one read. Many servers don't even send information that fast and your socket times-out while waiting. Buffer your respones into byte[4096] (a good recommended size) and write them out to a stream (using the read byte count) as they're read-in. This is how practically every transport mechanism works. "Well, I wouldn't say I've been missing it, Bob." - Peter Gibbons

      1 Reply Last reply
      0
      • D Daniel Bright

        The BeginListener function will not receive any information from the socket past a certain amount (apparently 4096 bytes). Information sent that is smaller than that is received properly. If data is sent that is larger than that, it's truncated to 4096 bytes, and then the while loop exits (which it should never do). private void BeginListener() { byte[] b = new byte[10000000]; int k; Socket s; String strMessage = ""; TcpListener newList = new TcpListener(10000); newList.Start(); while (true) { strMessage = ""; s = newList.AcceptSocket(); k = s.Receive(b); for (int nCounter = 0; nCounter < k; nCounter++) { strMessage += (char)b[nCounter]; } } newList.Stop(); } private void SendCommand(String Address,String strTempFile) { StreamReader newReader = new StreamReader(strTempFile); String strSendString = newReader.ReadToEnd(); newReader.Close(); newClient = new TcpClient(); NetworkStream tcpStream = null; byte[] ba; try { newClient.Connect(Address,10000); tcpStream = newClient.GetStream(); ASCIIEncoding asem = new ASCIIEncoding(); ba = asem.GetBytes(strSendString); tcpStream.Write(ba,0,ba.Length); } catch (System.Net.Sockets.SocketException) { //Error Handling. } finally { if (tcpStream != null) {tcpStream.Close();} if (newClient != null) {newClient.Close();} } } Any help would be greatly appreciated.

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        First of all, there no such limitation in the .NET Socket implementation : the actual code beinh Receive() is in fact a direct Winsock2 recv() function call, and it passes your buffer directly, not an intermediate buffer. I can see an error in your code : in your loop, you are Accepting a new socket instance at each iteration. :confused: Usually, you accept a socket once, and then loop with Receive.

        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