Hi, hab ein großes Problem und ich hoffe, ihr könnt mir dabei helfen.
so, zumindest mag ich nun mal selber einen Java IRC-Bot/Client schreiben und dies als Applet realisieren.
Zumndest ist mein Problem folgendes.
Stellt euch vor, ihr connected zu einem Server und diesen gibt es auch. nur dass der Port gesperrt ist, weil der Server vielleicht kein IRC hat. Nur gibt es da einerseits ein Problem mit java, meines erachtens. Oder ich bin halt zu unwissend.
Wenn ich nun ein neuen Socket mit java erstelle,
dann versucht er so lange einen neuen Sockel zu erstellen, bis er eine Verbindung auf den Port/Server hat.
Ich weiss nich, wie lange es dauert, bis er abbricht oder zumindest dauert es mir zu lange.
Über Threads denke ich geht dieses Problem zu lösen. Jedoch mag ich dies erst als aller letze Möglichkeit hinzu ziehen.
Hier nochmal mein Code, vielleicht hab ich ja da auch einen fehler drin.
Danke schon mal im voraus, wenn ihr mir da Anhaltspunkte liefert und mir da weiter helft!
CU
die4me
so, zumindest mag ich nun mal selber einen Java IRC-Bot/Client schreiben und dies als Applet realisieren.
Zumndest ist mein Problem folgendes.
Stellt euch vor, ihr connected zu einem Server und diesen gibt es auch. nur dass der Port gesperrt ist, weil der Server vielleicht kein IRC hat. Nur gibt es da einerseits ein Problem mit java, meines erachtens. Oder ich bin halt zu unwissend.
Wenn ich nun ein neuen Socket mit java erstelle,
Code:
Socket soc = new Socket(InetAddress.getByName("irc.triagony.net"), 6667);
Ich weiss nich, wie lange es dauert, bis er abbricht oder zumindest dauert es mir zu lange.
Über Threads denke ich geht dieses Problem zu lösen. Jedoch mag ich dies erst als aller letze Möglichkeit hinzu ziehen.
Hier nochmal mein Code, vielleicht hab ich ja da auch einen fehler drin.
Code:
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
/**
* Write a description of class Connection here.
*
* @author Sebastian Koschmieder <root [AT] root-online [dot] [dee] [ee]>
* @version 0.02
*/
public class Connection
{
//Verbindungsspezifische Variablen
private Socket soc = null;
private String server = null;
private int port = 0;
//Streamer, welche in und aus dem Socket lesen
private PrintWriter out = null;
private BufferedReader in = null;
/**
* this constructor set some of the global variables and onnect to server on port
*/
public Connection(String server, int port)
{
this.setServer(server);
this.setPort(port);
}
/**
* this is the deconstructor, and it is unseting the global variables
*/
protected void finalize()
{
this.close();
this.closeIn();
this.closeOut();
this.setServer(null);
this.setPort(0);
}
/**
* Set the server, you want to connect
*/
protected void setServer(String server)
{
this.server = server;
}
/**
* show the server
*/
protected String getServer()
{
return this.server;
}
/**
* you can set the port
*/
protected void setPort(int port)
{
this.port = port;
}
/**
* show the port
*/
protected int getPort()
{
return this.soc.getPort();
}
/**
* connect you to the server and show you your status
*/
protected boolean connect()
{
try
{
if(this.server == null) return false;
soc = new Socket(InetAddress.getByName(this.server), this.port);
return true;
}
catch(NullPointerException e)
{
System.out.println(e.toString());
return false;
}
catch(IOException e)
{
System.out.println(e.toString());
return false;
}
}
protected boolean socketActive()
{
return this.soc.isConnected();
}
/**
* initialize the outputstream out
*/
protected boolean output()
{
try
{
//create an new PrintWriter to write in the socket
this.out = new PrintWriter(this.soc.getOutputStream(), true);
return true;
}
catch(IOException e)
{
System.out.println(e.toString());
return false;
}
}
/**
* initialize the inputstream in
*/
protected boolean input()
{
try
{
//create a new BufferedReader to read out of the socket
this.in = new BufferedReader(new InputStreamReader(this.soc.getInputStream()));
return true;
}
catch(IOException e)
{
System.out.println(e.toString());
return false;
}
}
/**
* close the opened output stream out
*/
private void closeOut()
{
try
{
this.out.close();
}
catch(NullPointerException e) {}
}
/**
* colose the input stream in
*/
private void closeIn()
{
try
{
this.in.close();
}
catch(NullPointerException e) {}
catch(IOException e) { this.in = null; }
}
/**
* write command into the stream
*/
protected void writeCommand(String command)
{
this.out.println(command);
}
/**
* get messages from the stream
*/
protected String incomming()
{
try
{
String res = this.in.readLine();
System.out.println(res);
return res;
}
catch(IOException e)
{
System.out.println(e.toString());
return null;
}
}
/**
* close the socket soc
*/
private boolean close()
{
try
{
this.soc.close();
return true;
}
catch(IOException e)
{
System.out.println(e.toString());
return false;
}
catch(NullPointerException e) { return true; }
}
}
Danke schon mal im voraus, wenn ihr mir da Anhaltspunkte liefert und mir da weiter helft!
CU
die4me