简体   繁体   中英

Java TCP Listener loop

I've been searching for hours on google as I know this is a really simple problem I'm facing but I can't find a SIMPLE solution to my issue.

I'm writing a really basic IRC client, just to re-learn the ropes of Java as its been a long time since I've done any coding and I have a project I want to do later.

I'm stuck at the socket listener though, I can provide code if needed, but all I really need is a SIMPLE tutorial on how to create a loop that listens to and already created socket, and does something as soon as it receives any data. This loop should continue until the socket is closed (the user disconnects from the server)

The issue with my current code is the listener seems to hang the entire program UNTIL it receives data. I've even put a while loop in a separate thread so it can hang that thread until data arrives, yet it still hangs the entire program.

I don't want to be told the answer, I want to know how to find the answer. I have even looked at creating an event driven listener, but that seemed overly complex for what I need.

I was hoping not to have to post the code because its a mess after all the edits I've done to try and get it working, but here is what I have so far:

ListenToServer.java:

class ListenToServer extends Thread{

JTextPane outputDestination = null;
Socket establishedSocket = null;

public void kickStartprep(Socket establishedSocket, JTextPane outputDestination){
    this.establishedSocket = establishedSocket;
    this.outputDestination = outputDestination;
}


@Override
public void run(){

    UpdateServerStatusWindow("Thread is running!", outputDestination);


    BufferedReader inFromServer = null;

  try{
  inFromServer = new BufferedReader(new InputStreamReader(establishedSocket.getInputStream()));

      while (inFromServer.readLine().isEmpty() == false){
        UpdateServerStatusWindow(inFromServer.readLine(), outputDestination);  
      }


  }
  catch (Exception e){
      UpdateServerStatusWindow(e.toString(), outputDestination);
  }
}


public void UpdateServerStatusWindow(String message, JTextPane destination){

    StyledDocument doc = destination.getStyledDocument();
    try
{
doc.insertString(doc.getLength(), '\n' + message, null);    
}
catch(Exception e){ 
    JOptionPane.showMessageDialog(null, "There was an error updating the server     message output window in the TCP Listner!");
    JOptionPane.showMessageDialog(null, e);

}

}
}

Then I invoke thus:

MainGUI.java

 ListenToServer serverListener = new ListenToServer();
    serverListener.kickStartprep(establishedConnection, ServerMessageOutput);
    serverListener.run();

A friend had a look at this code and found the issue was with the way I was calling the Thread. Basically I needed to call:

serverListener.start();

instead of

serverListener.run();

This has resolved the issue, and my IRC client is now connecting to the server correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM