Socket Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ali Hamad

    Socket Question

    Hello All :

    A socket question from a networking newbie. I need to create
    a server that:

    1) receive a message from client.
    2) check that message and response to it.
    3) the client get the server message and send another message.
    4) finally, the server receive the message and close the connection.

    I have successfully done this. However, I couldn't use the same socket
    to send the second message
    to the server. I have googled but all the examples are only for sending
    one message and receiving the response.

    in my client code, I have :

    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("1st message")
    response = s.recv(1024)
    validate(respon se)
    s.send("2nd message")
    response2 = s.recv(1024)
    s.close()

    However, I got the first response just fine from the server but the
    second message didn't get to the server.

    So, the solution I came up with is to send the 1st message, close the
    socket, create new socket,
    and send the 2nd message.

    I came up with something like :

    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("1st message")
    response = s.recv(1024)
    s.close()
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("2nd message")
    response = s.recv(1024)
    s.close()

    and it works !

    My Question :

    is it possible to send/receive from the same socket more than one message ?

    Thank you for your assistance in advance,
  • safecom@gmail.com

    #2
    Re: Socket Question

    Maybe you need to close the socket somewhere else, rather than to
    close it when you receive the your response.
    On 9ÔÂ30ÈÕ, ÉÏÎç7ʱ01·Ö, Ali Hamad <ali.hama...@gm ail.comwrote:
    Hello All :
    >
    A socket question from a networking newbie. I need to create
    a server that:
    >
    1) receive a message from client.
    2) check that message and response to it.
    3) the client get the server message and send another message.
    4) finally, the server receive the message and close the connection.
    >
    I have successfully done this. However, I couldn't use the same socket
    to send the second message
    to the server. I have googled but all the examples are only for sending
    one message and receiving the response.
    >
    in my client code, I have :
    >
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("1st message")
    response = s.recv(1024)
    validate(respon se)
    s.send("2nd message")
    response2 = s.recv(1024)
    s.close()
    >
    However, I got the first response just fine from the server but the
    second message didn't get to the server.
    >
    So, the solution I came up with is to send the 1st message, close the
    socket, create new socket,
    and send the 2nd message.
    >
    I came up with something like :
    >
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("1st message")
    response = s.recv(1024)
    s.close()
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    s.connect(('127 .0.0.1', 1888))
    s.send("2nd message")
    response = s.recv(1024)
    s.close()
    >
    and it works !
    >
    My Question :
    >
    is it possible to send/receive from the same socket more than one message?
    >
    Thank you for your assistance in advance,

    Comment

    • Mark Tolonen

      #3
      Re: Socket Question


      "Ali Hamad" <ali.hamad34@gm ail.comwrote in message
      news:mailman.18 58.1222894062.3 487.python-list@python.org ...
      Hello All :
      >
      A socket question from a networking newbie. I need to create
      a server that:
      >
      1) receive a message from client.
      2) check that message and response to it.
      3) the client get the server message and send another message.
      4) finally, the server receive the message and close the connection.
      >
      I have successfully done this. However, I couldn't use the same socket
      to send the second message
      to the server. I have googled but all the examples are only for sending
      one message and receiving the response.
      >
      in my client code, I have :
      >
      s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      s.connect(('127 .0.0.1', 1888))
      s.send("1st message")
      response = s.recv(1024)
      validate(respon se)
      s.send("2nd message")
      response2 = s.recv(1024)
      s.close()
      >
      However, I got the first response just fine from the server but the
      second message didn't get to the server.
      >
      So, the solution I came up with is to send the 1st message, close the
      socket, create new socket,
      and send the 2nd message.
      >
      I came up with something like :
      >
      s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      s.connect(('127 .0.0.1', 1888))
      s.send("1st message")
      response = s.recv(1024)
      s.close()
      s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      s.connect(('127 .0.0.1', 1888))
      s.send("2nd message")
      response = s.recv(1024)
      s.close()
      >
      and it works !
      >
      My Question :
      >
      is it possible to send/receive from the same socket more than one message
      ?
      >
      Thank you for your assistance in advance,
      Yes, you can send more than one message on a socket. The problem is likely
      in the server code. What does it look like? Also TCP is a streaming
      protocol. It has no concept of a start and end of message unless you
      implement something you can recognize as a complete message in the protocol.

      -Mark

      Comment

      Working...