Network Communication

Discussion in 'Plugin Development' started by Zachster, May 13, 2013.

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

    Zachster

    What is the best tutorial on Network Communication in Java?
     
  2. Offline

    Craftiii4

    Can you be a bit more specific? Do you mean for example;

    A minecraft-server (plugin) to an webserver? and the ability to send data between them?
     
  3. Offline

    Zachster

    I'm wanting to make a plugin will communicate with an Android remote management app. (I know they are out there but I want to write my own.)

    EDIT: Nice I see you wrote ColourFireWorks. I love that plugin!
     
  4. Offline

    Craftiii4

    Thanks ^^,

    Okay I can easily help you here as I have actually done this!

    What you need to do is open an socket (look it up) between the two locations. I'm guessing you want to send the information both ways so both need to be aware that the other exists. You will also need a good knowledge of java and XML (& android) if you want it to be appealing on the android device.

    A socket is kinda a gateway between two locations over the internet, one side must open and the other side must accept. This connection must then be kept open. On the minecraft the server the best way to achieve this while being able to send and receive data is to open the socket within its own thread, each connection (from separate android devices) must spawn their own thread and your plugin needs to be able to determine the data sent from each and which socket to send data to.

    This is not an easy process btw, but you will need to look up sockets and understand them :p.
     
  5. Offline

    Zachster

    Thanks. I'll try this. btw which should i use udp or tcp

    EDIT: Also, what is the best way to add encryption to this?
     
  6. Offline

    socram8888

    I recommed you using TCP. With it, you're sure the data is going to be received well on the other side.

    And about encryption: Minecraft use CryptoInputStream from BouncyCastle. This is already included in Bukkit, so I think this is the best bet.
     
  7. Offline

    Technius

    On your server, you want to host a ServerSocket that runs on its own thread. Then you will accept incoming connections with a Socket:
    Code:
    //Modified snippets from SkyLink
    public class ServerThread extends Thread
    {
        private boolean isRunning = false;
        ServerSocket serve;
        public ServerThread()
        {
              serve = new ServerSocket(1337);//The number is the port of the server
        }
        public void start() //Starts your listening server
        {
              isRunning = true;
              super.start();
        }
        public void run()
        {
              while(isRunning)
              {
                  Socket s = null;
                  try
                  {
                        s = serve.accept(); //Attempt to receive connections
                  }
                  catch(IOException ioe)
                  {
                        //Connection error
                  }
                  new SocketThread(s); //Your own class for incoming connections
              }
        }
        public void stopServer()
        {
              isRunning = false;
              try
              {
                  serve.close(); //Close the listening server
              }
              catch(IOException ioe)
              {
                  ioe.printStackTrace();
              }
        }
    }
    
    You will want to then create and start threads for each Socket. You can perform authorization in either your server's run method or in the thread's constructor.

    You can use MD5 hashes to encrypt passwords:
    Code:
    try
    {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        digest.update(string.getBytes(), 0, string.length());
        enc = new BigInteger(1, digest.digest()).toString(16);
    }
    catch (NoSuchAlgorithmException e)
    {
          e.printStackTrace();
    }
    
    Store passwords on the server as MD5 hashes and compare them to encrypted hashes sent by the android devices.
     
  8. Offline

    BajanAmerican

    I found this to be the best one out there:
     
  9. Offline

    Zachster

    Thank you all for your help. It might be a couple of days before I can try this as I am currently tied up with another project and have a big deadline to meet.
     
  10. Offline

    zack6849

    This video really irritated me, how can someone who couldn't even remember the ip address for localhost try to teach others networking? he also doesn't close the sockets or any of the readers he made!
     
Thread Status:
Not open for further replies.

Share This Page