Now, you maybe asking yourself, what is a socket? Well, here is a basic definition: Put more simply, Okay, so now that that's out of the way, I can explain what the library actually does. This library makes it simpler to create server/client sockets with fewer lines of code. It is event-driven, so when a message has been received by either the server or client, it fires an event, which then can be processed by your code. The same goes for connecting, disconnecting, etc... Normally, a basic client socket would be something like: Code:java import java.io.*;import java.net.*; public class Requester{ Socket requestSocket; ObjectOutputStream out; ObjectInputStream in; String message; Requester() { } void run() { try { //1. creating a socket to connect to the server requestSocket = new Socket("localhost", 2004); System.out.println("Connected to localhost in port 2004"); //2. get Input and Output streams out = new ObjectOutputStream(requestSocket.getOutputStream()); out.flush(); in = new ObjectInputStream(requestSocket.getInputStream()); //3: Communicating with the server do { try { message = (String) in.readObject(); System.out.println("server>" + message); sendMessage("Hi my server"); message = "bye"; sendMessage(message); } catch (ClassNotFoundException classNot) { System.err.println("data received in unknown format"); } } while (!message.equals("bye")); } catch (UnknownHostException unknownHost) { System.err.println("You are trying to connect to an unknown host!"); } catch (IOException ioException) { ioException.printStackTrace(); } finally { //4: Closing connection try { in.close(); out.close(); requestSocket.close(); } catch (IOException ioException) { ioException.printStackTrace(); } } } void sendMessage(String msg) { try { out.writeObject(msg); out.flush(); System.out.println("client>" + msg); } catch (IOException ioException) { ioException.printStackTrace(); } } public static void main(String args[]) { Requester client = new Requester(); client.run(); }} But using this library, it can be simplified into: Code:java public class SocketExample extends JavaPlugin{ private final Server server = new Server(9876); private final Client client = new Client("127.0.0.1", 9876); private Logger logger = Logger.getLogger("Minecraft"); public void onDisable() { client.Disconnect(); server.ShutdownAll(); server.StopServer(); } public void onEnable() { client.getHandler().getConnected().addSocketConnectedEventListener(new SocketConnectedEventListener() { @Override public void socketConnected(SocketConnectedEvent evt) { logger.info("Client - Connected to server!"); logger.info("Client - Sending message to server."); client.SendMessage("Hello World!"); } }); client.getHandler().getMessage().addMessageReceivedEventListener(new MessageReceivedEventListener() { @Override public void messageReceived(MessageReceivedEvent evt) { logger.info("Client - I got the following message: " + evt.getMessage()); client.Disconnect(); } }); }} Source Code Example Source Code Download JAR Some extra info on sockets: The TCP/IP protocol is stream-oriented; it was not made for sending/receiving messages. Basically, as I understand it, when sending a "message" through a socket, the whole thing is not sent as "one message." The data is streamed, so when the client starts reading, it doesn't know where the message you sent starts or ends. This can result in broken up messages. Example: Server sends: Hello World Client receives: Hel Client receives: lo World There are several methods on receiving full messages, without it being broken up. One way would be to use a delimiter, which splits the message by a certain character(s). Another method, which I used, was to send the message with a header, which tells the server/client what the length of the message will be. So, when it receives a message, it first reads the first four bytes, which contains the header, then stores the length received into an integer. After that, it reads again and again until it receives the full message using the length that was received previously. Note: I haven't tested it very thoroughly, so if you find any bugs or want something added, feel free to post here. P.S: This library only supports sending/receiving strings at the moment, but that can be changed if needed.
Well, sending messages between an application and your plugin, or communicate with other plugins that you made.
hello, how can I add the MessageRecievedEvent Listener to a java application? Not to a plugin, because I want my application to show The info when it is recieved from ther server. Thanks
in.close(); out.close(); requestSocket.close(); They may be null on the time that code comes there, you know?
I'll get a "ClassNotFoundExeception". The library ist included into NetBeans (Properties - Libraries - Add Jar/Folder). Any suggestions to fix that? Code: org.bukkit.plugin.InvalidPluginException: java.lang.NoClassDefFoundError: com/mitch528/sockets/events/ServerSocketAcceptedEventListener at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:155) at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:305) at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:230) at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222) at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:198) at net.minecraft.server.ServerConfigurationManagerAbstract.<init>(ServerConfigurationManagerAbstract.java:50) at net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11) at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:105) at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:378) at net.minecraft.server.ThreadServerApplication.run(SourceFile:539) Caused by: java.lang.NoClassDefFoundError: com/mitch528/sockets/events/ServerSocketAcceptedEventListener at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:144) ... 9 more Caused by: java.lang.ClassNotFoundException: com.mitch528.sockets.events.ServerSocketAcceptedEventListener at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:44) at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:29) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 12 more
I'm receiving a StackOverflow error after that the plugin has been running for a little bit, about 5 minutes. Do keep in mind that this is a Bungee stack trace because I made Bungee my server instance. Code: 11:36:16 [SEVERE] Exception in thread "Thread-5" 11:36:16 [SEVERE] java.lang.StackOverflowError 11:36:16 [SEVERE] at java.util.regex.Pattern.expr(Pattern.java:1996) 11:36:16 [SEVERE] at java.util.regex.Pattern.compile(Pattern.java:1696) 11:36:16 [SEVERE] at java.util.regex.Pattern.<init>(Pattern.java:1351) 11:36:16 [SEVERE] at java.util.regex.Pattern.compile(Pattern.java:1028) 11:36:16 [SEVERE] at java.lang.String.split(String.java:2367) 11:36:16 [SEVERE] at java.lang.String.split(String.java:2409) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.SocketMessage.<init>(SocketMessage.java:13) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Main$1$1.messageReceived(Main.java:68) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.events.MessageReceived.executeEvent(MessageReceived.java:28) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:172) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:158) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:179) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:158) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:179) ECT ECT ECT ECT ECT 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:179) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.startReading(SocketHandler.java:158) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.HandleConnection(SocketHandler.java:92) 11:36:16 [SEVERE] at me.Markcreator.FlareBungeeSocket.Sockets.SocketHandler.run(SocketHandler.java:295)
Markcreator there is an error in your SocketMessafe constructor. You try to split something which caused the error, not this libary.
Code:java String stream = null; String dataType = null; String serverFrom = null; String serverTo = null; String info = null; public SocketMessage(String message) { this.stream = message; String[] data = message.split(";/"); this.dataType = data[0]; this.serverFrom = data[1]; this.serverTo = data[2]; this.info = data[3]; } public SocketMessage(String datatype, String serverfrom, String serverto, String info) { this.dataType = datatype; this.serverFrom = serverfrom; this.serverTo = serverto; this.info = info; this.stream = datatype + ";/" + serverfrom + ";/" + serverto + ";/" + info; } public String getStream() { return this.stream; } public String getDataType() { return this.dataType; } public String getServerFrom() { return this.serverFrom; } public String getServerTo() { return this.serverTo; } public String getInfo() { return this.info; }
Markcreator I have not much experience with regex, but the problem is the split method in line 9. Can't realy help you with that (Maybe try to quote it?)
Force_Update1 MCManager was last seen: Sep 27, 2012 Not shure if he will reply... And not working isn't one of the kind of bug reports that are helpful.