Re: pass-by-reference(of StringBuffer) to a constructor of another
class
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);
cn.start();
if(cn.mm.equals("")==false)
ta.append("\nClient : "+cn.mm);
JOptionPane.showMessageDialog(null,""+cn.mm);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Error : "+e);
}
/*try
{
Thread.sleep(500);
}
catch (Exception e)
{
}*/
}
}
public static void main(String args[])
{
server1 a=new server1();
Thread th=new Thread(a);
th.start();
}
}
class connection extends Thread
{
protected Socket cs;
protected ObjectInputStream client;
protected ObjectOutputStream cli;
protected PrintStream ps;
public 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)
{
}
}
mm="abc";
//this.start();
//toser.mesg=mm;
/*try
{
toser.mesg=new String(""+mm);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"Error : "+e);
}
System.out.println("abc value : "+abc);
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);
JOptionPane.showMessageDialog(null,"value : "+mm);
}
}
catch (Exception e3)
{
JOptionPane.showMessageDialog(null,"Error : "+e3);
}
finally
{
try
{
cs.close();
}
catch (Exception e4)
{
}
}
}
}
this is new program...
how can pause the execution the of thread(that represents the server1)
until the run() of class connection is executed...