Java Sockets?

Discussion in 'Spigot Plugin Development' started by jay275475, Aug 3, 2015.

  1. Hey guys I'm trying to make a socket server in my plugin so I can send commands from vps as a test however when I start plugin it stops loading everything else :/
    Code (Text):
    public void startListener() throws IOException{
            s = new ServerSocket(4280);
            Socket sock = s.accept();
            BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            String str = br.readLine();
            Bukkit.broadcastMessage(str);
        }
     
    • Optimistic Optimistic x 1
  2. Socket.accept() will wait until it receives and incoming connection. You need to make it multithreaded
     
    • Agree Agree x 1
    • Useful Useful x 1
  3. Thanks, how would I go to making it multi-threaded? I've never made sockets like this before.
     
    • Agree Agree x 2
  4. A class that would listen for incoming connections on the port would look something like this:

    Code (Text):
    public class SocketServer extends Thread {
         ServerSocket socket;

        public SocketServer(int port){
            socket = new ServerSocket(port)
        }

        @Override
        public void run(){
            while(true){
                Socket sock = s.accept();
                BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                String str = br.readLine();
                Bukkit.broadcastMessage(str);
            }
        }
    }
    P.S. I'm new to spigot forums. Anyone know how to format code?
     
    #5 kmecpp, Aug 3, 2015
    Last edited: Aug 3, 2015
  5. Thanks!
     
    • Like Like x 1
  6. ;-; I still can't get it xD also to format code you press the button next to save (Floopy Disk)

    I tried making it work but I think I made it worse :/ there are no errors though :)

    Socket class
    Code (Text):
    package me.jay.chat;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.ServerSocket;
    import java.net.Socket;

    import org.bukkit.Bukkit;

    public class SocketHandler extends Thread {
    static ServerSocket socket;

    public static void SocketServer(int port){
    try {
        socket = new ServerSocket(port);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    while(true){
    Socket sock = null;
    try {
        sock = socket.accept();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String str = null;
    try {
        str = br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Bukkit.broadcastMessage(str);
    }
    }
    }
     
    Main class
    Code (Text):
    @Override
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            SocketHandler.SocketServer(4000);
        }
    PS. I know Java but not much and I haven't used Socket Servers.....

    Also it works its just not doing anything, here is my php code.
    Code (Text):
    <?php
    $PORT = 4000;
    $HOST = "###.##.##.#";
    $sock = socket_create(AF_INET, SOCK_STREAM, 0)
         or die("error: could not create socket\n");
    $succ = socket_connect($sock, $HOST, $PORT)
        or die("error: could not connect to host\n");
    $text = "test\n";
    socket_sendto($sock, $text, strlen($text), 0 ,$HOST, $PORT);
    socket_close($sock);
    ?>
     
  7. Tux

    Tux

    That's a static method. The code must be placed in the Thread's run method and the thread must be started.
     
    • Agree Agree x 2
  8. Ugh I feel like such a noob but then if I start it as a thread then how do I set the port variable in kmecpp's example?
     
  9. SocketServer server = new SocketServer(4000);
    server.start();

    ?
     
  10. Tux

    Tux

    • Agree Agree x 2
  11. You shouldn't be working with networking much less using Bukkit without understanding the basics of Java first.
     
    • Agree Agree x 2
  12. x.x I was trying to start it as a class...
     
  13. Skionz I understand using Bukkit, whenever I use Java itself I need help because I'm not super used to it but I can tie a lot of Java things with bukkit easily...
     
  14. Create a new BukkitRunnable and run the task asynchronously.
     
  15. The other one already works thanks for trying to help out though :)