Server and Client Socket problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • José Carlos

    Server and Client Socket problem

    Hi.

    I am starting a server socket program. I send data from client socket but
    the server don´t recived anything.

    This is my python code:

    Server:

    import socket
    def run():
    servidor = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    servidor.bind(( 'localhost', 6969))
    servidor.listen (1)
    while 1:
    (conexion, direccion) = servidor.accept ()
    datos = conexion.recv(4 069)
    print "Servidor Recibe", datos
    if datos == "quit":
    servidor.close( )
    break

    run()


    Client:

    import socket
    def run():
    cliente = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    cliente.connect (('localhost', 6969))
    except:
    print "No he podido Conectar con el Servidor"
    return
    datos = '12345678'
    cliente.send(da tos)
    #cliente.close( )

    run()


    I am a new python user. what can i do?

    Thank you.

    Regards.
    José Carlos


  • Diez B. Roggisch

    #2
    Re: Server and Client Socket problem

    Hi,

    [color=blue]
    > import socket
    > def run():
    > servidor = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    > servidor.bind(( 'localhost', 6969))
    > servidor.listen (1)
    > while 1:
    > (conexion, direccion) = servidor.accept ()
    > datos = conexion.recv(4 069)
    > print "Servidor Recibe", datos
    > if datos == "quit":
    > servidor.close( )
    > break
    >
    > run()[/color]

    This code can obviously not run, as its not properly indented. I'm sure that
    indentation got somehow lost when you create the posting, but as long as
    you don't provide it in a executable fashion, we can't help you.

    However, as someone beeing recently converted from hand-made socket
    programming to twisted, I suggest instead of working with sockets yourself,
    you better investigate into the twisted framework. Its very well
    documented.

    Regards,

    Diez B. Roggisch

    Comment

    • Josiah Carlson

      #3
      Re: Server and Client Socket problem

      > This code can obviously not run, as its not properly indented. I'm sure that[color=blue]
      > indentation got somehow lost when you create the posting, but as long as
      > you don't provide it in a executable fashion, we can't help you.[/color]

      Agreed.
      [color=blue]
      > However, as someone beeing recently converted from hand-made socket
      > programming to twisted, I suggest instead of working with sockets yourself,
      > you better investigate into the twisted framework. Its very well
      > documented.[/color]

      After having written both synchronous and asynchronous servers and
      clients using Python sockets, I've been very happy with the asyncore
      standard library. I should probably give twisted a shot, but writing
      with asyncore is so easy that I'm hard-pressed to even bother. I also
      get a sick satisfaction in implementing protocols...but maybe that is
      just me.

      - Josiah

      Comment

      • Pierre Quentel

        #4
        Re: Server and Client Socket problem

        Apart from indentation, you had forgotten the "try" statement in client
        This should work better :

        Client

        ---------------------
        import socket

        def run():
        try:
        cliente = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        cliente.connect (('localhost', 6969))
        except:
        print "No he podido Conectar con el Servidor"
        return
        datos = '12345678'
        cliente.send(da tos)
        #cliente.close( )

        run()

        ---------------------

        Server

        ---------------------
        import socket

        def run():
        servidor = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        servidor.bind(( 'localhost', 6969))
        servidor.listen (1)
        while 1:
        (conexion, direccion) = servidor.accept ()
        datos = conexion.recv(4 069)
        print "Servidor Recibe", datos
        if datos == "quit":
        servidor.close( )
        break

        run()
        ---------------------

        @+
        Pierre


        Comment

        Working...