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. .NET (Core and Framework)
  4. Socket Recieve problem

Socket Recieve problem

Scheduled Pinned Locked Moved .NET (Core and Framework)
sysadminhelp
5 Posts 4 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.
  • G Offline
    G Offline
    ganesh_IT
    wrote on last edited by
    #1

    Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.

    using (NetworkStrem ns = new NetworkStream(client))
    {
    using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
    {
    while (sent < file.Length) // sent = 0 initially
    {
    data = new byte[20];

                        br.Read(data, 0, data.Length); //read 20 byte from file 
                        tempData = data;
                        ns.Write(data, 0, data.Length); // sent to client
                        ns.Flush();
                        ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client 
                        ack = Encoding.ASCII.GetString(data);
                        while (ack != "ok")
                        {
                            ns.Write(tempData, 0, tempData.Length);
                            ns.Flush();
                            ns.Read(data, 0, data.Length);
                            ack = Encoding.ASCII.GetString(data);
                        }
                        sent += 20;
                        ack = string.Empty;
                    }
    

    }

    what'wrong here i dont know if u know tell me Thanks in advance

    A E 2 Replies Last reply
    0
    • G ganesh_IT

      Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.

      using (NetworkStrem ns = new NetworkStream(client))
      {
      using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
      {
      while (sent < file.Length) // sent = 0 initially
      {
      data = new byte[20];

                          br.Read(data, 0, data.Length); //read 20 byte from file 
                          tempData = data;
                          ns.Write(data, 0, data.Length); // sent to client
                          ns.Flush();
                          ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client 
                          ack = Encoding.ASCII.GetString(data);
                          while (ack != "ok")
                          {
                              ns.Write(tempData, 0, tempData.Length);
                              ns.Flush();
                              ns.Read(data, 0, data.Length);
                              ack = Encoding.ASCII.GetString(data);
                          }
                          sent += 20;
                          ack = string.Empty;
                      }
      

      }

      what'wrong here i dont know if u know tell me Thanks in advance

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      What are the problems? Are you getting an error - if so edit your question and post the error.

      The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

      G 1 Reply Last reply
      0
      • A Abhinav S

        What are the problems? Are you getting an error - if so edit your question and post the error.

        The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it. My latest tip/trick Visit the Hindi forum here.

        G Offline
        G Offline
        ganesh_IT
        wrote on last edited by
        #3

        using (NetworkStrem ns = new NetworkStream(client))
        {
        using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
        {
        while (sent < file.Length) // sent = 0 initially
        {
        data = new byte[20];

                            br.Read(data, 0, data.Length); //read 20 byte from file 
                            tempData = data;
                            ns.Write(data, 0, data.Length); // sent to client //\*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\*
                            ns.Flush();
                            ns.Read(data, 0, data.Length); //Error occures here, this read method read the data send by me in  \*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* and also the ack string ok, thats why the while loop became infinite
                            ack = Encoding.ASCII.GetString(data);
                            while (ack != "ok")
                            {
                                ns.Write(tempData, 0, tempData.Length);
                                ns.Flush();
                                ns.Read(data, 0, data.Length);
                                ack = Encoding.ASCII.GetString(data);
                            }
                            sent += 20;
                            ack = string.Empty;
                        }
        

        }

        L 1 Reply Last reply
        0
        • G ganesh_IT

          using (NetworkStrem ns = new NetworkStream(client))
          {
          using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
          {
          while (sent < file.Length) // sent = 0 initially
          {
          data = new byte[20];

                              br.Read(data, 0, data.Length); //read 20 byte from file 
                              tempData = data;
                              ns.Write(data, 0, data.Length); // sent to client //\*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\*
                              ns.Flush();
                              ns.Read(data, 0, data.Length); //Error occures here, this read method read the data send by me in  \*\*\*\*\* Line Number - 12 \*\*\*\*\*\*\* and also the ack string ok, thats why the while loop became infinite
                              ack = Encoding.ASCII.GetString(data);
                              while (ack != "ok")
                              {
                                  ns.Write(tempData, 0, tempData.Length);
                                  ns.Flush();
                                  ns.Read(data, 0, data.Length);
                                  ack = Encoding.ASCII.GetString(data);
                              }
                              sent += 20;
                              ack = string.Empty;
                          }
          

          }

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          ganesh_IT wrote:

          br.Read(data, 0, data.Length);

          I don't understand your message, however I can tell you a Read method returns an integer, i.e. the number of bytes read. Not using that information is a mistake, as there may be less than data.Length, in which case you are processing too much data later on (as in your write). :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          1 Reply Last reply
          0
          • G ganesh_IT

            Hi guys i try to write a simpale server client appliaction. server transmit the file content to client and recieve an acknowledgment which is a "ok" string. i have some problem in my code.

            using (NetworkStrem ns = new NetworkStream(client))
            {
            using (BinaryReader br = new BinaryReader(File.OpenRead(filepath)))
            {
            while (sent < file.Length) // sent = 0 initially
            {
            data = new byte[20];

                                br.Read(data, 0, data.Length); //read 20 byte from file 
                                tempData = data;
                                ns.Write(data, 0, data.Length); // sent to client
                                ns.Flush();
                                ns.Read(data, 0, data.Length); //recieve ack, but its read the previews content sent to the client 
                                ack = Encoding.ASCII.GetString(data);
                                while (ack != "ok")
                                {
                                    ns.Write(tempData, 0, tempData.Length);
                                    ns.Flush();
                                    ns.Read(data, 0, data.Length);
                                    ack = Encoding.ASCII.GetString(data);
                                }
                                sent += 20;
                                ack = string.Empty;
                            }
            

            }

            what'wrong here i dont know if u know tell me Thanks in advance

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            If you are using TCP/IP you do not need to send acknowledgments. TCP/IP is a guaranteed, in-order protocol and .NET will throw an exception if it fails. If you want to keep the connection active set the keep alive flag. Oh, and, if you are using UDP sending an ack is self defeating.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            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