Communication with External App

Discussion in 'Plugin Development' started by Ewe Loon, Jan 8, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Ewe Loon

    I am wanting to write a plugin that communicates with an external application (probably on another computer)
    I would prefer it to use UDP , but will consider suggestions

    with udp all the examples i can find do not show how to know when a packet has been received
    they all wait till one is received, this is unexceptionable for a plugin
     
  2. In order to be able to "know when a packet has been received", you first have to even be able to receive it - that is essentially waiting until you get one sended. That's just how networking works.
    If you don't tell your computer "yo, I'm waiting for UDP packets on port xyz, pls hand it to me when something comes in, kay?

    What you need for your plugin is a separate thread (using the scheduler and an async delayed task preferably) that takes the waiting part, and then notify your main thread when you get a packet (or handle it in the same separate thread, depending on what you are doing).

    And I'd really recommend going with TCP first, it's way easier to understand and apply for a networking beginner.
     
  3. Offline

    Technius

    You have to create a thread, which will listen to the packets. Then from there, you create threads that listen to each packet.
     
  4. Offline

    soliddanii

    I´m interested in the same thing. any tutorials :( ?
     
  5. Offline

    soliddanii

  6. Offline

    MCManager

    I would recommend using TCP instead of UDP for what you are trying to accomplish. Take a look at my TCP sockets library here.
     
  7. If you're really looking for a library instead of making it yourself, there are two tcp-socket libraries here in the forum at the resources section.
    TCP-Library (see link post above)
    Transmit

    Choose which one you like best.
     
  8. Offline

    soliddanii

    I've read your post and its soooo awesome. I downladed the jar, but i dont understand where i have to put the metoth in my plugin so it is listening for clients continously (perhaps onEnabled?) Thanks for the help :D

    ok, i figured out. now i've my plugin working and listening for client data. I created a client thaht sends Strings, and I configured the server to show that strings in the log info

    Code:
    main.log.log(Level.INFO, "Yaay, we got some data! Here it is: {0}", event.getRawData());
    but when i try the client the log throws this error :S:

    Code:
    [INFO] Oh, someone found it's way!
    2012-03-13 17:23:32 [SEVERE] Exception in thread "Thread-12"
    2012-03-13 17:23:32 [SEVERE] com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:180)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.Gson.fromJson(Gson.java:755)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.Gson.fromJson(Gson.java:721)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.Gson.fromJson(Gson.java:670)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.Gson.fromJson(Gson.java:642)
    2012-03-13 17:23:32 [SEVERE]    at de.kumpelblase2.transmit.TransmitServer.handle(TransmitServer.java:188)
    2012-03-13 17:23:32 [SEVERE]    at de.kumpelblase2.transmit.ClientConnectionThread.run(ClientConnectionThread.java:128)
    2012-03-13 17:23:32 [SEVERE]    at java.lang.Thread.run(Unknown Source)
    2012-03-13 17:23:32 [SEVERE] Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 5
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.stream.JsonReader.expect(JsonReader.java:339)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:322)
    2012-03-13 17:23:32 [SEVERE]    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:168)
    2012-03-13 17:23:32 [SEVERE]    ... 7 more
    
    I supose it has to do with the type object and string ( i think object was expected, or i'm wrong?)
    Thanks for your help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  9. Offline

    nisovin

    It looks like you're trying to parse the string as json, but it isn't actually json.
     
  10. Offline

    soliddanii

    I finally solved it, thanks
     
Thread Status:
Not open for further replies.

Share This Page