SocketServer - Server Freezing

Discussion in 'Plugin Development' started by BlueFreakLP, Mar 20, 2014.

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

    BlueFreakLP

    Hello,
    i have made a little Socket Server.
    I have getting it worked in Bukkit. But after the server Display my message, that the Server waiting for Clients, my Server is freezing.
    Code:java
    1. public void run() {
    2. try {
    3. int serverPort = 4020;
    4. ServerSocket serverSocket = new ServerSocket(serverPort);
    5. // serverSocket.setSoTimeout(10000);
    6. while(true) {
    7. System.out.println("Warte auf Clienten auf Port " + serverSocket.getLocalPort() + "...");
    8.  
    9. Socket server = serverSocket.accept();
    10. System.out.println("Verbunden mit " + server.getRemoteSocketAddress());
    11.  
    12. PrintWriter toClient =
    13. new PrintWriter(server.getOutputStream(),true);
    14. BufferedReader fromClient =
    15. new InputStreamReader(server.getInputStream()));
    16. String line = fromClient.readLine();
    17. System.out.println("Nachricht erhalten: " + line);
    18. toClient.println("Verbindung beendet " + server.getLocalSocketAddress() + "\nGoodbye!");
    19. }
    20. }
    21. ex.printStackTrace();
    22. }
    23. catch(IOException e){
    24. e.printStackTrace();
    25. }
    26. }
    27.  
    28. public static void main(String[] args) {
    29. ServerSideSocket srv = new ServerSideSocket();
    30. srv.run();
    31. }

    Screenshot:
    http://i.imgur.com/oqG9DcD.png
     
  2. Offline

    StealerSlain

    Maybe because while(true) is endless loop?
     
  3. Offline

    RawCode

    there are TONS of samples about simple server-client applications on stackoverflow.
     
  4. Offline

    GameplayJDK

    BlueFreakLP
    Try to create a new Thread() for that. Then it shouldn't influence the server..
     
  5. Offline

    BlueFreakLP

    I need this While Loop, or the Server will not listen at more than One connection.

    GameplayJDK
    can you give me an Example. I have tried it, but Eclipse marked the most Code of the SocketServer as an Error.
     
  6. Offline

    GameplayJDK

    BlueFreakLP
    Instead of calling srv.run(); you will do something like new Thread(new ServerSideSocket()).start();
    This might help.
     
  7. Offline

    BlueFreakLP

    Sorry,
    i´m too stupid for this :D
    Thats my Class:
    Code:java
    1.  
    2. import java.net.*;
    3. import java.io.*;
    4.  
    5. public class SocketServerClass{
    6. public void startServer(){
    7.  
    8.  
    9. this.run();
    10. }
    11. public void run() {
    12.  
    13. try {
    14. int serverPort = 4020;
    15. ServerSocket serverSocket = new ServerSocket(serverPort);
    16. // serverSocket.setSoTimeout(10000);
    17. while(true) {
    18. System.out.println("Warte auf Clienten auf Port " + serverSocket.getLocalPort() + "...");
    19.  
    20. Socket server = serverSocket.accept();
    21. System.out.println("Verbunden mit " + server.getRemoteSocketAddress());
    22.  
    23. PrintWriter toClient =
    24. new PrintWriter(server.getOutputStream(),true);
    25. BufferedReader fromClient =
    26. new InputStreamReader(server.getInputStream()));
    27. String line = fromClient.readLine();
    28. System.out.println("Nachricht erhalten: " + line);
    29. toClient.println("Verbindung beendet " + server.getLocalSocketAddress() + "\nGoodbye!");
    30. }
    31. }
    32. ex.printStackTrace();
    33. }
    34. catch(IOException e){
    35. e.printStackTrace();
    36. }
    37. }
    38.  
    39. }

    GameplayJDK If i put "this.run()" into new thread() eclipse give me again an Error.
     
  8. Offline

    GameplayJDK

    BlueFreakLP
    replace this.run(); with
    Code:java
    1. Thread t = new Thread(new Runnable() {
    2. @Override
    3. public void run() {
    4.  
    5. }
    6. });

    and paste the code of the run() method into this @Override run()
    Then just do t.start()
     
  9. Offline

    BlueFreakLP

  10. Offline

    GameplayJDK

Thread Status:
Not open for further replies.

Share This Page