reusing sockets

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Omega
    New Member
    • Oct 2007
    • 12

    reusing sockets

    Hello,

    I've a quite simple question about socket programming (but I can't find the answer in widely available sources). Is it possible to 'reuse' a socket within 1 connection? In other words, is it possible to place 'send()' and 'recv()' in a loop in the code below? In that case time will be saved because the socket doesn't have to be opened and closed for every new buffer. The socket desciptor stays the same, the only thing that has to be changed is the message buffer.

    socket();
    connect();
    send(); //instead, while(1) {buffer = "new buffer"; send(buffer); recv();}
    recv(); // "
    close();

    Thanks!
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by Omega
    Hello,

    I've a quite simple question about socket programming (but I can't find the answer in widely available sources). Is it possible to 'reuse' a socket within 1 connection? In other words, is it possible to place 'send()' and 'recv()' in a loop in the code below? In that case time will be saved because the socket doesn't have to be opened and closed for every new buffer. The socket desciptor stays the same, the only thing that has to be changed is the message buffer.

    socket();
    connect();
    send(); //instead, while(1) {buffer = "new buffer"; send(buffer); recv();}
    recv(); // "
    close();

    Thanks!
    I recommend you to read a good Socket programming tutorial to understand this.
    While sending message we dont create a socket newly every time , we change only the message buffer

    Raghuram

    Comment

    • Omega
      New Member
      • Oct 2007
      • 12

      #3
      Okay, but then you send a larger message in parts and after that you receive a message in parts. But what I want is to send a small message, receive an answer and then send a new message, receive an answer etc. I tried this but it doesn't work for me. I seems that the server closes that particular socket connection after 1 reply.

      Comment

      • Arulmurugan
        New Member
        • Jan 2008
        • 90

        #4
        Originally posted by Omega
        Okay, but then you send a larger message in parts and after that you receive a message in parts. But what I want is to send a small message, receive an answer and then send a new message, receive an answer etc. I tried this but it doesn't work for me. I seems that the server closes that particular socket connection after 1 reply.
        No , Never server closes the socket , until unless you call "close()". I think there is something else gone wrong.I could help you if send me the source code.
        -Arul

        Comment

        • Omega
          New Member
          • Oct 2007
          • 12

          #5
          Originally posted by Arulmurugan
          No , Never server closes the socket , until unless you call "close()". I think there is something else gone wrong.I could help you if send me the source code.
          -Arul
          Thanks for your offer, but it is not necessary to fix the code. I only need to mention this issue as a recommendation in a report. Therefore I needed a confirmation that this can indeed improve the written software (it already works, but quite slow). Thanks!

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by Omega
            Thanks for your offer, but it is not necessary to fix the code. I only need to mention this issue as a recommendation in a report. Therefore I needed a confirmation that this can indeed improve the written software (it already works, but quite slow). Thanks!


            Check the server side code. what it does after sending the message to the client.
            Check whether any shutdown/close call in the server side and analyze the logic there

            Raghuram

            Comment

            Working...