Your code is missing some important peaces. For example the imports. They show us what libraries you used and give us chances to do some reserches.
1) For example the line:
myListener = new Timer(250, readLine);
It can't be java.util.Timer because this class doesn't have an constructor (long/int, ActionListener). Possibly this call only runs once instead of multiple times, but I'm unable to know and tell you that.
2) Another example: You have the line
acceptClient.start();
I can only assume that there is an instance variable
Accepter acceptClient = new Accepter();
but it can also refer to a completely different class which works differently.
3) Back to my first example:
You are doing some kind of polling. This is not good style in java because Java Thready are by design optimized for blocking threads. Use a code block like this instead (untested; inspired by your server code)
class ClientThread extends Thread{
@Override
public void run(){
try{
String tempReceiver;
while ((tempReceiver = socketIn.readLine()) != null) {
exStr(tempReceiver);
}
} catch (IOException e) {
System.out.println("Read failed");
}
}
}
In this case you don't need polling and you have immediate response. This consumes less CPU.
A completely different option would be to use Java NIO, which is more optimized for single-thread applications (or let's call it few-thread).
Some off-topic remarks:
OT 1)
Calling Swing-functions like JOptionPane.showMessageDialog()
and setText()
is dangerous from threads. Swing itself is not Thread-safe. You should use a EventQueue.invokeLater()
wrapper around it. my favourite: SwingUtils.invokeLater()
OT 2)
Having the variable tempReceiver
class-global (I assume this because I can't see any declaration) is also dangerous because you likely use it somewhere else and possibly exactly overwrite it during the readLine()
and exStr()
calls in the other thread (if Timer is the Swing-implementation, it runs an extra thread).
OT 3)
You are saying "the .jar client and server has been connected to each other". Does this mean you have two open console windows with a java -jar
command running in each window? If not, you might miss some important console output (possibly exceptions).
The windows distribution of java has 2 java runners: javaw.exe
which is called when double-clicking a .jar file in the Explorer. And java.exe
which also runs your program but also opens a console window which shows the output of System.out.println
etc. This console is very important for debugging. It shows the same output as the Debug window in NetBeans or Eclipse.