Connecting to an IRC server

?

-

Hi,

I am trying to code an IRC client and followed the example found at
http://hacks.oreilly.com/pub/h/1966

The example worked. However, when I tried connecting to several other
servers closer to home, namely "irc.pacific.net.sg", there is a long
delay and after that the connection is closed. Sometimes, I received an
error "...register first".

Anybody knows why and how to solve it?




import java.io.*;
import java.net.*;

public class HackBot {

public static void main(String[] args) throws Exception {

// The server to connect to and our details.
String server = "irc.freenode.net";
String nick = "simple_bot";
String login = "simple_bot";

// The channel which the bot will join.
String channel = "#irchacks";

// Connect directly to the IRC server.
Socket socket = new Socket(server, 6667);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream( )));
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream( )));

// Log on to the server.
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + " 8 * : Java IRC Hacks Bot\r\n");
writer.flush( );

// Read lines from the server until it tells us we have connected.
String line = null;
while ((line = reader.readLine( )) != null) {
if (line.indexOf("004") >= 0) {
// We are now logged in.
break;
}
else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use.");
return;
}
}

// Join the channel.
writer.write("JOIN " + channel + "\r\n");
writer.flush( );

// Keep reading lines from the server.
while ((line = reader.readLine( )) != null) {
if (line.toLowerCase( ).startsWith("PING ")) {
// We must respond to PINGs to avoid being disconnected.
writer.write("PONG " + line.substring(5) + "\r\n");
writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
writer.flush( );
}
else {
// Print the raw line received by the bot.
System.out.println(line);
}
}
}

}
 
A

Ann

- said:
Hi,

I am trying to code an IRC client and followed the example found at
http://hacks.oreilly.com/pub/h/1966

The example worked. However, when I tried connecting to several other
servers closer to home, namely "irc.pacific.net.sg", there is a long
delay and after that the connection is closed. Sometimes, I received an
error "...register first".

Maybe you need to register with the server you are trying to access.

Anybody knows why and how to solve it?




import java.io.*;
import java.net.*;

public class HackBot {

public static void main(String[] args) throws Exception {

// The server to connect to and our details.
String server = "irc.freenode.net";
String nick = "simple_bot";
String login = "simple_bot";

// The channel which the bot will join.
String channel = "#irchacks";

// Connect directly to the IRC server.
Socket socket = new Socket(server, 6667);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream( )));
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream( )));

// Log on to the server.
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + " 8 * : Java IRC Hacks Bot\r\n");
writer.flush( );

// Read lines from the server until it tells us we have connected.
String line = null;
while ((line = reader.readLine( )) != null) {
if (line.indexOf("004") >= 0) {
// We are now logged in.
break;
}
else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use.");
return;
}
}

// Join the channel.
writer.write("JOIN " + channel + "\r\n");
writer.flush( );

// Keep reading lines from the server.
while ((line = reader.readLine( )) != null) {
if (line.toLowerCase( ).startsWith("PING ")) {
// We must respond to PINGs to avoid being disconnected.
writer.write("PONG " + line.substring(5) + "\r\n");
writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");
writer.flush( );
}
else {
// Print the raw line received by the bot.
System.out.println(line);
}
}
}

}
 
?

-

Ann said:
Maybe you need to register with the server you are trying to access.

Hi Ann,

Thanks for replying. Yes, the solution is to register.. but how?
The IRC RFC stated that the proper way to 'register' is to send the
following commands:

- PASS
- NICK
- USER

but the example below already did that...


Anybody knows why and how to solve it?




import java.io.*;
import java.net.*;

public class HackBot {

public static void main(String[] args) throws Exception {

// The server to connect to and our details.
String server = "irc.freenode.net";
String nick = "simple_bot";
String login = "simple_bot";

// The channel which the bot will join.
String channel = "#irchacks";

// Connect directly to the IRC server.
Socket socket = new Socket(server, 6667);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream( )));
BufferedReader reader = new BufferedReader(
new InputStreamReader(socket.getInputStream( )));

// Log on to the server.
writer.write("NICK " + nick + "\r\n");
writer.write("USER " + login + " 8 * : Java IRC Hacks Bot\r\n");
writer.flush( );

// Read lines from the server until it tells us we have
connected.

String line = null;
while ((line = reader.readLine( )) != null) {
if (line.indexOf("004") >= 0) {
// We are now logged in.
break;
}
else if (line.indexOf("433") >= 0) {
System.out.println("Nickname is already in use.");
return;
}
}

// Join the channel.
writer.write("JOIN " + channel + "\r\n");
writer.flush( );

// Keep reading lines from the server.
while ((line = reader.readLine( )) != null) {
if (line.toLowerCase( ).startsWith("PING ")) {
// We must respond to PINGs to avoid being disconnected.
writer.write("PONG " + line.substring(5) + "\r\n");
writer.write("PRIVMSG " + channel + " :I got
pinged!\r\n");

writer.flush( );
}
else {
// Print the raw line received by the bot.
System.out.println(line);
}
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top