5
import socket, sys

if len(sys.argv) !=3 :
print "Usage: ./supabot.py <host> <port>"
sys.exit(1)

irc = sys.argv[1]
port = int(sys.argv[2])
sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((irc, port))
sck.send('NICK supaBOT\r\n')
sck.send('USER supaBOT supaBOT supaBOT :supaBOT Script\r\n')
sck.send('JOIN #darkunderground' + '\r\n')
data = ''
while True:
      data = sck.recv(1024)
      if data.find('PING') != -1:
         sck.send('PONG ' + data.split() [1] + '\r\n')
         print data
      elif data.find('!info') != -1:
          sck.send('PRIVMSG #darkunderground supaBOT v1.0 by sourD' + '\r\n')


print sck.recv(1024)

when I run this code I get this error..

socket.error: [Errno 10054] An existing connection was forcibly closed by the remote host

it says that the error is in line 16, in data = sck.recv(1024)

3 Answers 3

4

You need to check the IRC protocol a little it more; your IRC session is not considered conncted (by the server) until certain actions have been completed which the server will inform your client about using IRC protocol codes. And if the server or network is busy when you are connecting it will take longer for these actions to complete.

In this case attempting to join a channel before the server has given you the MOTD (message of the day) would cause a disconnection by the server. The end of MOTD protocol code is 376 and indicates that the IRC connection sequence is over, and you can proceed with your IRC session eg: enter commands (like join).

I would suggest entering a RECV loop and monitoring data received from the server for the IRC code 376 before you attempt to join a channel, in Perl this would look somthing like this:

 sub chan_join{
  while(my $input = <SOCK>){
    if($input =~ /376/){
      my $talk = "JOIN $channel";
      &send_data($talk);
      &monitor;
    }
    else { print "$input";  }
  }
}

Pretty poor but you get the idea right? (please note its only necessary to check for 376 once, once seen you are connected and you only need to maintain the connection by responding to the server 'PING's)

1

That probably means that you're not supplying the expected handshake or protocol exchange for the server, and it is closing the connection.

What happens if you telnet to the same machine and port and type in the same text?

2
  • its weird cause it used to work before the same code, but i reconnected to the server and i started to get that error i dont even know why
    – SourD
    Jun 17, 2010 at 10:52
  • C0d3r: Maybe they server decided it didn't like your bot, so now it has blacklisted. Maybe they added some athentication credential that's required after NICK but before other commands? Maybe someone's already got the NICK, and since you try to join a channel without a NICK, it disconnects you. Really, try the telnet technique and type the same commands - the result should be obvious. Jun 20, 2010 at 13:18
1

The remote host is issuing a TCP reset (RST), after accepting the connection. This can happen for a lot of reasons, including:

  • Firewall rules
  • Remote application error
  • Remote application simply closes the connection
  • etc.

As John Weldon said, try telnetting to the same machine and port and entering the commands manually.

Also, a good wire sniffer (Ethereal, WireShark, etc.) is highly useful for diagnosing this kind of problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.