Thema: [Java] Library (KJLib)
-
22.04.2011, 20:54 #1
- Registriert seit
- 06.11.2011
- Beiträge
- 418
Thanked 686 Times in 246 Posts[Java] Library (KJLib)
Hier ist nochmal die neueste Version der KJLib (v9.0adb) inkl. Source, hatte ich schonmal released aber aus Gründen an die ich mich nicht mehr erinnere hatte ich den Thread dann von Johnny löschen lassen. Ist eigentlich das Gleiche wie die KNLib (v1), nur in Java. Ich glaube in der KNLib hatte ich noch den ein oder anderen Fix mal eingebaut, den ich hier nicht mehr eingebaut habe, müsstet ihr euch dann rüberkopieren wenn es euch wirklich interessiert.
RemoteEndpoint.java
Code:/** * * @author Flav */ public enum RemoteEndpoint { DE("knuddels.net", 2710), AT("knuddels.net", 2711), CH("knuddels.net", 2712), COM("knuddels.com", 2713), TEST("knuddels.net", 2720), MFC("knuddels.net", 4242); private String host; private int port; private RemoteEndpoint(String host, int port) { this.host = host; this.port = port; } public String getHost() { return host; } public int getPort() { return port; } }
Code:import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.Socket; import java.net.UnknownHostException; /** * * @author Flav */ public class KJClient extends Thread { private static pS groupChat; private Socket socket; private OutputStream out; private byte[] decodeKey; private String passwordKey; private String nickname, password, channel; private String butler; static { groupChat = new pS("", 0); groupChat.init(); } private void init() throws IOException { out = socket.getOutputStream(); decodeKey = null; passwordKey = null; nickname = null; start(); } public void connect(RemoteEndpoint endpoint) throws UnknownHostException, IOException { if (!isConnected()) { socket = new Socket(endpoint.getHost(), endpoint.getPort()); init(); } } public void connect(RemoteEndpoint endpoint, String proxyHost, int proxyPort) throws UnknownHostException, IOException { if (!isConnected()) { socket = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyHost, proxyPort))); socket.connect(new InetSocketAddress(endpoint.getHost(), endpoint.getPort())); init(); } } public void disconnect() { if (socket != null) { if (!socket.isClosed()) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } socket = null; } } public boolean isConnected() { return socket != null && socket.isConnected(); } public void send(String token) { if (isConnected()) { if (token.startsWith("n\000")) { if (passwordKey == null) { String[] tokens = token.split("\000"); nickname = tokens[2]; password = tokens[3]; channel = tokens[1]; return; } token += "\000" + passwordKey; } byte[] buffer; synchronized (groupChat) { buffer = groupChat.compress(token); } try { out.write(encode(buffer)); } catch (IOException e) { e.printStackTrace(); } } } public void login(String nickname, String password, String channel) { send("n\000" + channel + "\000" + nickname + "\000" + password + "\000F"); } public void sendMessage(String message, String channel) { send("e\000" + channel + "\000" + message); } public String getButler() { return butler; } public void onReceive(String token) { } public void run() { try { out.write(0x00); send("t\000"); InputStream in = socket.getInputStream(); while (true) { byte[] buffer = decode(in); String token; synchronized (groupChat) { token = groupChat.decompress(buffer); } if (token.startsWith("(\000")) { String[] tokens = token.split("\000"); String key = tokens[3].trim(); decodeKey = key.getBytes(); passwordKey = tokens[1] + key.substring(1); if (nickname != null) { login(nickname, password, channel); nickname = null; } } else if (token.startsWith(",")) { if (token.length() == 1) { send("h\000-"); } else { send("h\000" + token.substring(2)); } } else if (token.startsWith("5\000")) { butler = token.substring(2); } onReceive(token); } } catch (IOException e) { } finally { disconnect(); } } private byte[] encode(byte[] message) { int length = message.length - 1; byte[] len; if (length < 128) { len = new byte[] { (byte) length }; } else { int count = 0; while (32 << (count + 1 << 3) <= length) { count++; } count++; len = new byte[count + 1]; len[0] = (byte) (count << 5 | 0x80 | length & 0x1F); for (int i = 1; i < len.length; i++) { len[i] = (byte) (length >>> 8 * (i - 1) + 5); } } byte[] buffer = new byte[len.length + message.length]; System.arraycopy(len, 0, buffer, 0, len.length); System.arraycopy(message, 0, buffer, len.length, message.length); return buffer; } private byte[] decode(InputStream in) throws IOException { byte first = (byte) in.read(); if (first == -1) { throw new IOException("End of stream"); } int length; if (first >= 0) { length = first + 1; } else { length = (first & 0x1F) + 1; int count = (first & 0x60) >>> 5; for (int i = 0; i < count; i++) { length += in.read() << (i << 3) + 5; } } byte[] buffer = new byte[length]; for (int i = 0; i < length; i++) { buffer[i] = (byte) ((byte) in.read() ^ (decodeKey != null && i < decodeKey.length ? decodeKey[i] : 0)); } return buffer; } }
Ähnliche Themen
-
Win7 R6034 - An application has made an attempt to load the C runtime library incorrectly
Von blackmac4444 im Forum WindowsAntworten: 4Letzter Beitrag: 21.02.2014, 19:07 -
Win7 Java in Safari ausführen (Fehlermeldung Java nicht installiert)
Von Silverstar im Forum WindowsAntworten: 8Letzter Beitrag: 02.12.2013, 23:41 -
[Release] Huffman (Java Library)
Von Flav im Forum ProtokollAntworten: 2Letzter Beitrag: 05.07.2013, 15:17 -
Funktionierende .NET Knuddels Library?
Von exception im Forum RessourcenAntworten: 4Letzter Beitrag: 14.02.2013, 08:33 -
Java EE 32 Bit?
Von Pwned im Forum JavaAntworten: 2Letzter Beitrag: 04.08.2012, 23:08
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.