Thread: Sockets ?

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    11

    Sockets ?


    I've read online both in ebooks and tutorials on how sockets work. All of which give a different example on how to get this set up. I've searched this forum as well, but can anyone provide an example of code that just sets up a socket and the client sends text and then ends the connects with the server socket.


    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Quick example (no error checking):

    Code:
    import socket
    
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysock.connect((127.0.0.1, 50007))
    mysock.send("Hello, World!")
    mysock.close()
    Look here for more info: http://docs.python.org/lib/module-socket.html
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    11
    Can you maybe provide a server example as well ?

    Thanks for the sample code as well, it's much appreciated.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Code:
    import
    
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('', 50007))
    server.listen(1024)
    
    (client, addr) = server.accept()
    print "Connection: ", addr[0]
    
    input = client.recv(1024)
    print "Received: ", input
    
    client.close()
    server.close()
    It shouldn't be too hard to expand on these to make them interactive if you so desire.

    EDIT: There are some good protocol dependent and indepentent examples here: http://docs.python.org/lib/socket-example.html

IMN logo majestic logo threadwatch logo seochat tools logo