I faced a new problem.
I’m writing a messenger in Java+Swing and I chose that way to make a client’s conversation with server:
I have class Running(all the code is for client)
(sever is OK – I checked)
public class Running {
private Socket s;
private PrintStream ps;
private BufferedReader br;
public Running(){
try{
s = new Socket(InetAddress.getLocalHost(), 8072);
ps = new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
} catch (UnknownHostException ex) {
System.out.println("11");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("00");
ex.printStackTrace();
}
}
public String receiveLine(){
String ret = "";
try {
ret = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
public void sendLine(String s){
ps.println(s);
}
public void close(){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And in main i make one variable
run = new Running();
Then I send this variable everywhere and it seemed to work.
It provides readline or writeline throwgh sockets.
I could register a user, log in or add contact.
I can open a MessageFrame to send messages to my contact.
But when I close it, my programm stopps reacting properly to server’s messages.
It reacts only to 30-70% of the messages.
I checked – server is OK.
So the problem is in Running or MessageFrameListener
public class MessageFrameListener{
private MessageFrame mf;
private User us;
private Contact cn;
private Timer timer;
private Running run;
public MessageFrameListener(MessageFrame m_f, User u_s, Contact c_n, Running r_n){
run = r_n;
mf = m_f;
us = u_s;
cn = c_n;
m_f.addButtonListener(new SButtonListener());
m_f.addWinListener(new FrameListener());
timer = new Timer(500,new timerListener());
timer.start();
}
public class timerListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
SwingWorker<String,Void> sw = new SwingWorker<String,Void>(){
public String doInBackground() {
String ret = "";
ret = run.receiveLine();
return ret;
}
public void done() {
String[] results = null;
try {
results = get().split(" ");
} catch (InterruptedException
| ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if("m".equals(results[0])){
if("-1".equals(results[2]))
mf.addLine2("Error");
else{
mf.addLine2(results[3]);
}
}
}
};
sw.execute();
}
}
public class SButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String insert = mf.getInput();
if(!insert.equals("")){
String infoString = "m "+us.getName()+" "+cn.getName()+" "+insert;
run.sendLine(infoString);
mf.addLine(insert);
mf.refreshInput();
}
}
}
public class FrameListener implements WindowListener{
@Override
public void windowClosing(WindowEvent e) {
timer.stop();
timer = null;
mf.close();
}
New information!
I started debugging. My client gets messages from server and gets to this line
mf.addLine2(results[3]);
But then nothing happens! Swing doesn’t add new line.
The code of addLine2:
public void addLine2(String line){
//dialogArea.append(line+"\n");
try {
if(line != ""){
String formLine = us.getName()+" ("+now()+")\n";
doc.insertString(doc.getLength(), formLine+line+"\n",st2);
}
}
catch (BadLocationException e){
e.printStackTrace();
}
}
Of course line!=””;
May be it is some Swing problem with threads? And Swing can’t deal with my requests?
Vasily.
Hey what he is saying is right , that string is going to with /n but it is trying to fetch .
Here is the sample ,