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

    Join Date
    Jul 2004
    Location
    INDIA
    Posts
    31
    Rep Power
    10

    What are the reason that socket.connect() fails?


    Hello this sounds to be quite general question rather than to be specific. However i just want to know something about this friendly earlier and mysterious at present socket.connect() function..

    I am using socket.connect() function in my code in several classess. However I found that in most of them it is working fine. Today i wrote one class in which i am trying to make socket connection to my bridge.

    I am using:

    Code:
    import socket
    
    self.socket = socket.socket(socket.AF_INET,socket_SOCK_STREAM)
    try:
     self.socket.connect((self.server,self.port))
     print "sucess"
    except:
     print "error: failed to open socket"
    well self.server is initialised with 192.168.100.254 and self.port as 2067

    My problem is that it is not working. I am using similar nature of socket in different class and here too i am connecting and everything seems to be fine.

    I just wann know that what can be the reason for the socket .conect to fail?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Your code is completely incorrect. First of all, you only use "self" when dealing with class objects. You must create a global (preferrably) variable that equals socket.socket(socket.AF_INET,socket_SOCK_STREAM). For instance:
    Code:
    s = socket.socket(socket.AF_INET,socket_SOCK_STREAM)
    Then to connect:
    Code:
    s.connect(('asia.battle.net', '6112'))
    'asia.battle.net' would be replaced by the server you want to connect to, and '6112' would be replaced by the servers port.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    INDIA
    Posts
    31
    Rep Power
    10
    Hi Yegg

    thanks for reply.

    Well I am using the all socket stuff in function of my class. I do apologise for my earlier incomplete / confusing post. self.socket = None has been initialised in my class __init__ function. and hence i am using it in my function say connect(self): as self.socket.

    I hope now it does makes sense.
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Re-read my post, and make s equal self.s. Then to connect you would do:
    Code:
    self.s.connect((server, port))
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    11
    Ok this is a really dumb question, but lets say I made a socket connection, what can I do with it? I mean the usage.
    What can change the nature of a man?
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Send/receive data from other computers.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    and more high level stuff if you use different modules like htmllib or httplib or something like ftplib and smtplib(email) and ...
    Those people who think they know everything are a great annoyance to those of us who do.
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Originally Posted by monkeyman23555
    and more high level stuff if you use different modules like htmllib or httplib or something like ftplib and smtplib(email) and ...
    Which is sending/receiving data.

IMN logo majestic logo threadwatch logo seochat tools logo