I found this code to communicate with an IRC server (see below). However I did not find how to send a command to download or upload in xdcc.
Once connected to the IRC server and positioned in the channel. I want to send a command like.
/msg bot_name xdcc send #number_of_file
Thank you in advance for your answers, examples and help.
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.toUpperCase( ).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);
}
}
}
}
Your code shows only the minimum requirements needed to open a connection to a IRC server and keep it connected, actually the entire IRC protocol is much more complex and it is not implemented in the code.
The xdcc send is simply a normal IRC message sent privately to a specific other user (usually a bot) of the IRC server, therefore you can send it by using the command PRIVMSG:
writer.write("PRIVMSG " + botNickName + " :xdcc send #" + numberOfPack + "\r\n");
where botNickName and numberOfPack are two String variables containing the nickname of the bot (i.e. the recepient of the message) and the number (in string format) of the package in which you are interested.
Nevertheless you must consider that the DCC is an entire completely different protocol from the IRC protocol itself: it uses the CTCP message on IRC:
DCC SEND <filename> <ip> <port>
only to start a DCC session, but then there is the DCC protocol in order to manage the communication client-to-client. So if you really want to make the DCC works you should also implement it, but it would not be a quick job.
Related
I think it is really strange problem because I don't even know where to start when repairing it.
My problem is my client don't receive message send from server.
I know I provided very little amount of code but I think other code really does not matter in this problem. If you want me to provide more code comment and I will send it to you.
Server code
String s = "";
for(String a : Main.users.keySet()) {
s = s + a + " ";
}
System.out.println("Sendind message: " + "SERVER USERS " + s);
output.writeBytes("SERVER USERS " + s);
output.flush();
System.out.println("User message sent");
Main.users.keySet() returns Set of strings.
Server console
SERVER USERS test
User message sent
Client code
System.out.println("TEST 1");
while(true){
try {
reader = new BufferedReader(new InputStreamReader(input));
line = reader.readLine();
System.out.println("TEST 2");
Client console
I/System.out: TEST 1
I have a program that includes HTTP connection to a PLM application that runs on SQL Server. The program is scheduled to run daily. It collects data from few sources, then issues a query to the PLM to store the data, and finally reads the PLM's reply to verify if the data was properly stored.
The application ran OK, until we upgraded both the DB (into SQL Server 2012) and the PLM.
Since then the upgrade, when the program establishes the connection it receives OK status; however, the data setting query does not affect the data base, and there is no answer received. There are no error messages - just malfunction.
My major question is - how to debug it. I know whet I send and what I receive. How can I get more data on what happens in between?
I attach the code for review. What I didn't add here is the query itself, which is WML-like string. The PLM should fire an answer regardless the query it receives, even if it is an error message. However, I get only NULL.
public Boolean amlArasCommunication (String data , int targetDbType, String passWord)
{
final String url = "http://plm-srv/InnovatorServer/Server/InnovatorServer2012.aspx";
final String schemeUrl = "'http://schemas.xmlsoap.org/soap/envelope/'";
String answer =””;
String dataBase = data base name;
Writer wout;
HttpURLConnection amlConnection = null;
try
{
// instantiate the HttpURLConnection with the URL object - A new connection is
// opened every time by calling the openConnection method of the protocol
// handler for this URL. This is the point where the connection is opened.
amlConnection = (HttpURLConnection) new URL(url).openConnection();
amlConnection.setDoOutput(true);
amlConnection.setDoInput(true);
amlConnection.setRequestMethod("POST");
amlConnection.setRequestProperty("SOAPAction", "ApplyAML");
amlConnection.setConnectTimeout(10000);
amlConnection.setReadTimeout(10000);
amlConnection.setRequestProperty("AUTHUSER", "Admin");
amlConnection.setRequestProperty("AUTHPASSWORD", calcMD5(passWord));
amlConnection.setRequestProperty("DATABASE", dataBase);
String query = "<?xml version='1.0'?>\r\n" +
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=" + schemeUrl + ">\r\n" +
" <SOAP-ENV:Body>\r\n" +
data +
" </SOAP-ENV:Body>\r\n" +
"</SOAP-ENV:Envelope>\r\n";
// instantiate OutputStreamWriter using the output stream, returned from getOutputStream, that writes
// to this connection. If an I/O error occurs while creating the output stream, IOException will be fired.
wout = new OutputStreamWriter(amlConnection.getOutputStream());
wout.write (query);
wout.close();
// At this point, we've sent all the data. The outputStream was closed, while the connection is still open
int result;
if ((result = amlConnection.getResponseCode()) == HttpURLConnection.HTTP_OK)
{
// Get the communication results from the PLM
InputStream ac = amlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader (ac);
BufferedReader in = new BufferedReader(isr);
String readResult = in.readLine ();
int count = 0;
while (readResult != null)
{
answer += readResult + "\n";
readResult = in.readLine ();
count++;
}
in.close();
if (answer.contains("fault"))
System.out.println ("Error message: " + answer + "\nQuery: " + query);
else
log.message ("Lines count=" + count + "; com status=" + result + "; reply: " + answer, false);
}
else
// Error code is returned, or no status code is returned, do stuff in the else block
System.out.println("Connection failed with the following code: " + result);
}
catch (IOException e) { ; }
if (amlConnection != null)
amlConnection.disconnect ();
return true;
}
I think you will have to look into the log files of the PLM application to find out why you do not get an HTTP response. There might be a number of possible reasons why the application is not working anymore after the upgrade.
I guess that it will be difficult to debug the problem based on the client code only. As the server seems to accept your HTTP, I would expect that this event and errors would be written to a log file somewhere. You might also want to try some graphical tool like SOAP UI to test the SOAP service.
I asked a similar question in another thread but I think I'm just having trouble getting the syntax right at this point. I basically want to open a socket in Java, send a HTTP request message to get the header fields of a specific web page. My program looks like this so far:
String server = "www.w3.org";
int port = 80;
String uri = "/Protocols/rfc2616/rfc2616-sec5.html#sec5.1"
Socket socket = new Socket(server, port);
PrintStream output = new PrintStream(socket.getOutputStream());
BufferedReader socketInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.println("HEAD " + uri + " HTTP/1.1");
//String response = "";
String line = "";
while((line = socketInput.readLine()) != null){
System.out.println(line);
}
socketInput.close();
socket.close();
It doesn't really work. Or it doesn't work for all websites. If someone could just tell me the immediate problems with what I'm doing, that would be great. Thank you!
Change
output.println("HEAD " + uri + " HTTP/1.1");
to
output.println("HEAD " + uri + " HTTP/1.1");
output.println("Host: " + server);
output.println();
You have to send the Host header because usually there are more than one virtual host on one IP address. If you use HTTP/1.0 it works without the Host header.
I would use some higher-level component, like HttpURLConnection (see here) or apache http components.
I'm developing an application, where users have the option to have sent an e-mail to a specified e-mail every x minutes.
I don't want to rely on JavaMail (i.e. rely on whether my users have added the JavaMail jar to their classpath).
I realize that I could go on and create a server for doing this and connect to it with the necessary details, but this is last option.
How would I go on sending an e-mail in this case?
Are there any online services etc (paid or free) that provides a solution for this? For example connecting to them and specifying recipient e-mail and message, they would handle the e-mail sending.
Are there any smart and/or reasonably easy ways of sending e-mails using the Java Core packages?
Thanks :)
Mike.
You can -- by opening a socket to the smtp server and then writing to that socket.
Socket socket=new Socket("your.smtp.server",25);
br= new BufferedReader(newInputStreamReader(socket.getInputStream()));
os = socket.getOutputStream();
smtp("HELLO " + toEmailAddress);
smtp("MAIL FROM: "+ fromEmailAddress);
smtp("DATA");
smtp(yourContent");
and your smtp method would just read from the bufferedreader and write to socket
public void smtp(String command) {
br.readLine();
os.write(command.getBytes());
}
Here is some old code I had lying around that might get you started:
import java.io.*;
import java.net.*;
class EMail2
{
public static void main(String args[])
{
if ( args.length != 5 )
{
System.out.print("usage: java EMail2 <smtp-host> <fromName> <toAddress>");
System.out.println(" <subject> <body>");
System.exit(-1);
}
try
{
send(args[0], args[1], args[2], args[3], args[4]);
}
catch(Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
public static void send(String host, String from, String to, String subject, String message)
{
try
{
System.setProperty("mail.host", host);
// System.setProperty("mail.smtp.starttls.enable","true"); // not sure it this works or not
// open connection using java.net internal "mailto" protocol handler
URL url = new URL("mailto:" + to);
URLConnection conn = url.openConnection();
conn.connect();
// get writer into the stream
PrintWriter out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream() ) );
// write out mail headers
// From header in the form From: "alias" <email>
out.println("From: \"" + from + "\" <" + from + ">");
out.println("To: " + to);
out.println("Subject: " + subject);
out.println(); // blank line to end the list of headers
// write out the message
out.println(message);
// close the stream to terminate the message
out.close();
}
catch(Exception err)
{
System.err.println(err);
}
}
}
i'm trying to convert a simple irc client written on java to blackberry, it uses sockets, here it is:
package seinao;
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws Exception {
// The server to connect to and our details.
String server = "127.0.0.1";
String nick = "nickname";
String login = "nickname";
// The channel which the bot will join.
String channel = "#oi";
// 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.write("Hello World!");
writer.write("PRIVMSG " + channel + "Hello!\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 if(line.toLowerCase( ).contains("funciona")){
writer.write("PRIVMSG " + channel + " Olaz!\r\n");
writer.flush();
System.out.println("mermao ta foda");
}
else {
// Print the raw line received by the client.
System.out.println(line);
}
}
}
}
but i noticed there is no java.net.* on blackberry eclipse plugin soo, what should I do ? can someone help me ? what should I use for sockets ? I'm new to java and blackberry programming but i'm learning it quite fast, thanks alot
If I remember correctly, it's SocketConnection that you use. Search through the Blackberry API for what you'll need, it'll be way more helpful.