TCP Socket - text size limit

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pschulz
    New Member
    • Mar 2008
    • 4

    TCP Socket - text size limit

    I am trying to send strings over a simple TCP Socket connection but it will not send more than about 7 kBytes.
    I am not using the TCPListener / TCPClient but the Socket class, the socket parameters being NetworkFamily.I nternetwork, Protocol.Tcp, and Stream as a socket type.

    To send data, I am using the Socket.Send() member.
    The server accepts the connection synchronously and creates a new socket and it all works perfectly and as long as I am having small strings it is not a problem. The receive buffer I tried to set to varying sizes with no change.

    Do I need to get more complicated - do I need to use streams and all kinds of things? I have seen samples which just use Send() as I am but it's not working for me. I am using the .NET Framework 2.0 in C#.

    Regards

    Peter
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Hmm I have not had trouble with length before. I've sent up to about a meg with my own stuff.
    What are you using for code exactly?

    Comment

    • pschulz
      New Member
      • Mar 2008
      • 4

      #3
      Thanks for the reply.

      Below is the relevant code snippet. It is just a test sample but works okay to connect and get text sent, except for the strange limitation. If I send text of 12 KBytes, the programs says 7300 bytes received.

      Certainly TCP will send several packets - but the .NET documentation does not say if the Send() member function does any collation of packages or what. On MFC one had to use an CFile archive object with CSocket - is that needed too?

      the client which sends to my server is some sample application which for sure can send at least 512 Kbytes in one go and has done so to other TCP servers without any trouble.


      Code:
                  try
                  {
                      int port = HostListenPort;
                      int backlog = ListenBacklog;
                      IPAddress hostIP = HostIPAddress;
      
                      IPEndPoint ep = new IPEndPoint(hostIP, port);
                      listenSocket.Bind(ep);
                      listenSocket.Listen(backlog);
      
                      MessageBox.Show("Listening has started.");
      
                      try
                      {
                             Socket cSocket= listenSocket.Accept();
      
                              MessageBox.Show("Accepted, and new socket created.);
      
      
      
      		        cSocket.ReceiveBufferSize = 32768;
      		        byte[] recBuffer = new byte[32768];            
      	                int i = cSocket.Receive(recBuffer);
      	                String strRec = Encoding.UTF8.GetString(recBuffer);
                              MessageBox.Show(Convert.ToString(iTot) + " bytes received.");
      	           
      		        MessageBox.Show(strRec);
      
      
                      }
                      catch (Exception exptAccept)
                      {
                           String msg = "Error on accepting socket: " + exptAccept.ToString();
                            MessageBox.Show(msg);
                      }
                  }
                  catch (Exception exptConn)
                  {
                      MessageBox.Show("Error on connection: " + exptConn.ToString());
                  }

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well for starters, all of the data will not come in all at once, 7k is probably about all that makes it in before you do your reads. Have you tried to do another read, after allowing time for more data to arrive?

        Also, I have not seen a backlog supplied since the days of C++, I don't think it's really needed?

        Comment

        • pschulz
          New Member
          • Mar 2008
          • 4

          #5
          Thanks - and this worked.
          Obviously the RECEIVE method does not block until all is received ... no mention of this anywhere in the documentation though.

          Concerning the Backlog entry - there is no overload which doesn't have this parameter, you'll have to supply it on the Accept() method.

          Peter

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Hmm, guess I don't do enough socket binding.
            Also, there is a ReceiveBufferSi ze that is default to 8192 bytes, if that helps.

            Comment

            Working...