Klient IRC -

0

Witam, mam problem z podłączeniem się do serwera irc(próbowałem tego na portach 6665-6668, na różnych serwerach) - przy każdym połączeniu otrzymuję wyjątek: java.net.UnknownHostException: irc.pirc.pl

kod odpowiedzialny za połączenie:

package jirc.lib;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

import javax.swing.JTextArea;

public class IrcProtocol {
	private static final int DEFAULT_PORT = 6667;
	
	private Socket socket;
	private InputStream inStream;
	private String hostname;
	private String msg;
	private JTextArea textChat;
	
	public IrcProtocol(String hostname, JTextArea textChat) {
		this.hostname = hostname;
		this.textChat = textChat;
		
		Runnable r	= new IrcConnectionThread();
		Thread t	= new Thread(r);
		t.start();		
	}
	
	public void setMsg(String s) {
		if(s.startsWith("/")) 
			s = s.replace("/", "");
		msg = s;
	}
	
	private class IrcConnectionThread implements Runnable {
		public void run() {
			try {
				socket = new Socket(hostname, DEFAULT_PORT);
				try {
					inStream		= socket.getInputStream();
					Scanner in		= new Scanner(inStream);
					PrintWriter out	= new PrintWriter(socket.getOutputStream(), true);
				
					out.write("user jIRC 127.1.0.1" + hostname + " :[email protected]\n");
					msg = null;
				
					while(in.hasNextLine()) {
						String input = in.nextLine();
						textChat.append(input + "\n");
					
						if(msg != null) {
							out.write(msg);
							msg = null;
						}
					}
				} finally {
					socket.close();
					textChat.append("Disconnect\n");
				}
			} catch(IOException e) {
				textChat.append(e + "\n");
			}
		}
	}
}

0

ping hostname z konsoli co pokazuje ?

0

problemem był pusty znak na początku stringu

0

Rozwiązałem problem z połączeniem, ale aplikacja nadal nie zachowuje się tak jakbym oczekiwał.
W wyniku połączenia otrzymuję:

:insomnia.pirc.pl NOTICE AUTH :*** Looking up your hostname...
:insomnia.pirc.pl NOTICE AUTH :*** Found your hostname (cached)
:insomnia.pirc.pl NOTICE AUTH :*** Checking ident...
:insomnia.pirc.pl NOTICE AUTH :*** No ident response; username prefixed with ~
ERROR :Closing Link: [81.190.191.197] (Ping timeout)

Błąd konsoli:

Exception in thread "Thread-2" java.lang.NullPointerException
	at jirc.lib.IrcProtocol$IrcConnectionThread.run(IrcProtocol.java:53)
	at java.lang.Thread.run(Unknown Source)

Źródło:

package jirc.lib;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JTextArea;

public class IrcProtocol {
	private static final int DEFAULT_PORT = 6667;
	
	private Socket socket;
	private String hostname;
	private String msg;
	private JTextArea textChat;
	
	public IrcProtocol(String hostname, JTextArea textChat) {
		this.hostname = hostname.trim();
		this.textChat = textChat;
		
		Runnable r	= new IrcConnectionThread();
		Thread t	= new Thread(r);
		t.start();		
	}
	
	public void setMsg(String s) {
		if(s.startsWith("/")) 
			s = s.replace("/", "");
		msg = s;
	}
	
	private class IrcConnectionThread implements Runnable {
		public void run() {
			try {
				socket = new Socket(hostname, DEFAULT_PORT);
				try {
					BufferedReader in	= new BufferedReader(new InputStreamReader(socket.getInputStream()));
					PrintWriter out		= new PrintWriter(socket.getOutputStream(), true);
				
					out.write("USER jIRC 127.0.0.1" + hostname + " :[email protected] \n");
					msg = null;
				
					while(true) {
						String input = in.readLine();
						
						if(input != null)
							textChat.append(input + "\n");
						
						//PING & PONG
						if(input.startsWith("PING")) {
							String s = input.replace("PING", "PONG");
							out.write(s + " \n");
							textChat.append(s + "\n");
						}
						
						if(msg != null) {
							out.write(msg);
							msg = null;
						}
					}
				} finally {
					socket.close();
					textChat.append("Disconnect\n");
				}
			} catch(UnknownHostException e) {
				textChat.append(e + "\n");
			} catch(IOException e) {
				textChat.append(e + "\n");
			}
		}
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1