Reputation: 303
I'm trying to code chat-like application with multiple clients and single server. Idea: client sends String to server, server works with it and returns to client object (only client which sends string receives answer!).
Problem: single client works well. Running second client creates troubles: client 2 receives answer for client 1 and after first answer both clients can't send anything.
In other words, I need help, please.
Here is the code: Server:
public class serverWindow extends JFrame {
private JPanel contentPane;
private static JTextField tfAddition;
static JTextPane tp;
// connection stuff
static ServerSocket ss = null;
static Socket soc = null;
static DataInputStream din;
static ObjectOutputStream obout;
static int port = 1255;
public static void main(String[] args) throws IOException
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
serverWindow frame = new serverWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
try
{
ss = new ServerSocket(port);
while(true)
{
try
{
soc = ss.accept();
echoThread X = new echoThread(soc, tp);
X.start();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
ss.close();
}
}
/**
* Create the frame.
*/
public serverWindow() {
setTitle("Server");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 414, 183);
contentPane.add(scrollPane);
tp = new JTextPane();
tp.setEditable(false);
scrollPane.setViewportView(tp);
}
}
echoThread:
public class echoThread extends Thread
{
protected Socket soc;
public JTextPane tp;
static DataInputStream din = null;
static ObjectOutputStream obout = null;
public echoThread(Socket soc, JTextPane tp)
{
this.soc = soc;
this.tp = tp;
}
public void run()
{
try
{
din = new DataInputStream(this.soc.getInputStream());
obout = new ObjectOutputStream(soc.getOutputStream());
while(true)
{
String message = din.readUTF();
tp.setText(tp.getText().trim() + "\n" + message);
Response res = new Response(message, message.length());
obout.writeObject(res);
obout.flush();
}
}
catch(Exception ex)
{
ex.printStackTrace();
return;
}
finally
{
try
{
soc.close();
din.close();
obout.close();
}
catch(Exception ex)
{
ex.printStackTrace();
return;
}
}
}
}
client:
public class clientW extends JFrame {
private JPanel contentPane;
private JTextField textField;
private static JTextPane textPane;
// sockets
static Socket soc = null;
static ObjectInputStream obin = null;
static DataOutputStream dout = null;
static int port = 1255;
static String host = "localhost";
public static void main(String[] args) throws IOException
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
clientW frame = new clientW();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
try
{
try
{
soc = new Socket(host, port);
dout = new DataOutputStream(soc.getOutputStream());
obin = new ObjectInputStream(soc.getInputStream());
while(true)
{
Response res = (Response) obin.readObject();
textPane.setText(textPane.getText().trim() + "\n" + res.combineText());
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
soc.close();
obin.close();
dout.close();
}
}
public clientW() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("send");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
try
{
dout.writeUTF(textField.getText().trim());
dout.flush();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
btnNewButton.setBounds(335, 227, 89, 23);
contentPane.add(btnNewButton);
textField = new JTextField();
textField.setBounds(10, 227, 315, 21);
contentPane.add(textField);
textField.setColumns(10);
textPane = new JTextPane();
textPane.setEditable(false);
textPane.setBounds(10, 11, 414, 205);
contentPane.add(textPane);
}
}
Upvotes: 1
Views: 92
Reputation: 2367
You're defining the streams in echoThread
to be static, meaning that all instances of that class will share those objects. Because of that when the second echoThread
instance starts it overwrites the streams of the first instance so the streams for one socket are lost. If you change the definition to:
private DataInputStream din = null;
private ObjectOutputStream obout = null;
it appears to work as you described that it should.
Upvotes: 1
Reputation: 29332
din and obout in your echoThread are declared as static variables, so they are shared among all the clients. This might be corrected by declaring din and obout without the keyword static. Does this fix it?
Upvotes: 1