Re: pass-by-reference(of StringBuffer) to a constructor of another
class
i am very sorry to present my question that doesn't convey correct
meaning...
this file acts as server...
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Message implements Serializable
{
String mesg;
}
class server1 extends JFrame implements ActionListener,Runnable
{
JPanel p;
JTextArea ta;
JScrollPane sp;
JLabel l;
JTextField txt;
JButton b;
Socket cl;
String str;
ServerSocket sc;
Thread th;
Socket s;
//public String temp;
protected ObjectInputStream client;
protected ObjectOutputStream cli;
server1()
{
super("Client Window");
th=new Thread(this);
p=new JPanel();
//temp=new Message();
ta=new JTextArea(10,20);
sp=new JScrollPane(ta);
l=new JLabel("Enter Message ");
txt=new JTextField(15);
b=new JButton("Submit");
try
{
sc=new ServerSocket(1001);
}
catch (Exception e)
{
//JOptionPane.showMessageDialog(null,"Socket Error");
}
p.add(sp);
p.add(l);
p.add(txt);
p.add(b);
getContentPane().add(p);
setSize(300,300);
setVisible(true);
b.addActionListener(this);
th.start();
}
public void actionPerformed(ActionEvent ae)
{
try
{
str=txt.getText();
txt.setText("");
Message m1=new Message();
m1.mesg=str;
if (str.equals("")==false)
{
cli=new ObjectOutputStream(s.getOutputStream());
cli.writeObject((Message)m1);
ta.append("\nServer : "+str);
}
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,""+e);
}
}
public void run()
{
while (true)
{
try
{
s=sc.accept();
Message temp=new Message();
connection cn=new connection(s,temp);
if(temp.mesg.equals("")==false)
ta.append("\nClient : "+temp.mesg);
//JOptionPane.showMessageDialog(null,""+temp);
}
catch (Exception e)
{
}
/*try
{
Thread.sleep(500);
}
catch (Exception e)
{
}*/
}
}
public static void main(String args[])
{
new server1();
}
}
class connection extends Thread
{
protected Socket cs;
protected ObjectInputStream client;
protected ObjectOutputStream cli;
protected PrintStream ps;
String mm,abc;
public connection(Socket sr,Message toser)
{
cs=sr;
try
{
client=new ObjectInputStream(cs.getInputStream());
//cli=new ObjectOutputStream(cs.getOutputStream());
}
catch (Exception e1)
{
try
{
cs.close();
}
catch (Exception e2)
{
}
}
this.start();
//toser.mesg=mm;
//abc=new String(""+mm.toString());
// the main probem is here....
// the value of mm is not assigned to toser.mesg--> please suggest
me a solution
//
toser.mesg=new String(mm);
JOptionPane.showMessageDialog(null,""+toser.mesg);
JOptionPane.showMessageDialog(null,""+abc);
}
public void run()
{
Message msg1;
try
{
for(;;)
{
msg1=(Message)client.readObject();
if(msg1==null)
break;
mm=msg1.mesg;
//mm="chanti gadu";
System.out.println("Client Message : "+mm);
}
}
catch (Exception e3)
{
}
finally
{
try
{
cs.close();
}
catch (Exception e4)
{
}
}
}
}