I was trying to send message through threading.
dataAvailable is a variable which tells whether message is available on textfield or
not. if available dataAvailable is set to true and in run() method if it is true
following code is executed. But the problem is in run() it never sees dataAvailable is
true and nothing is sent. Help
needed.
/*
This
is client
*/
import
java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
import
java.net.Socket;
import java.net.UnknownHostException;
import
javax.swing.JButton;
import javax.swing.JFrame;
import
javax.swing.JScrollPane;
import javax.swing.JTextArea;
import
javax.swing.JTextField;
public class PeerOne extends JFrame
implements Runnable
{
private static final long
serialVersionUID = 1L;
JTextField outgoing;
JTextArea
incoming;
JButton send;
Thread t,t1;
ObjectOutputStream
output; //for writing objects to a stream
ObjectInputStream input; //for
reading objects from a stream
Socket s;
volatile boolean
dataAvailable=false;
String message = null;
public
PeerOne()
{
outgoing = new JTextField();
outgoing.setEnabled(true);
outgoing.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
if(!(e.getActionCommand().isEmpty()))
{
dataAvailable =
true;
System.out.print("hi");
}
}
});
this.add(outgoing,BorderLayout.SOUTH);
incoming = new JTextArea();
incoming.setEditable(true);
this.add(new JScrollPane(incoming),BorderLayout.CENTER);
this.add(incoming);
setSize(300,150);
setVisible(true);
try
{
s = new
Socket("localhost",5500);
incoming.append("Connection
Successfull..."+"\n");
output = new
ObjectOutputStream(s.getOutputStream());
output.flush();
input =
new ObjectInputStream(s.getInputStream());
}
catch
(UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
t = new
Thread(this,"PeerOne");
System.out.print("New Thread");
//t1 = new
Thread(this,"Two");
t.start();
}
public static void main(String[] args)
{
new
PeerOne();
}
public void run()
{
while(true)
{
if(dataAvailable==true)
{
try
{
System.out.print(0);
output.writeObject(outgoing.getText());
output.flush();
dataAvailable = false;
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
try
{
try
{
message =
(String)input.readObject();
incoming.append(message);
}
catch
(ClassNotFoundException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
This
is server
*/
import
java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import
java.awt.event.ActionListener;
import
java.io.BufferedInputStream;
import java.io.IOException;
import
java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import
java.io.PrintStream;
import java.net.ServerSocket;
import
java.net.Socket;
import
java.net.UnknownHostException;
import
javax.swing.JButton;
import javax.swing.JFrame;
import
javax.swing.JScrollPane;
import javax.swing.JTextArea;
import
javax.swing.JTextField;
public class PeerTwo extends JFrame
implements Runnable
{
private static final long
serialVersionUID = 1L;
JTextField outgoing;
JTextArea
incoming;
JButton send;
Thread t;
Socket s;
ObjectOutputStream output; //for writing objects to a stream
ObjectInputStream input; //for reading objects from a stream
volatile boolean
dataAvailable=false;
String message = null;
public PeerTwo()
{
outgoing = new JTextField();
outgoing.setEnabled(true);
outgoing.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
if(!(e.getActionCommand().isEmpty()))
{
dataAvailable =
true;
}
}
});
this.add(outgoing,BorderLayout.SOUTH);
incoming = new
JTextArea();
incoming.setEditable(true);
this.add(new
JScrollPane(incoming),BorderLayout.CENTER);
this.add(incoming);
setSize(300,150);
setVisible(true);
try
{
ServerSocket ss = new
ServerSocket(5500,100);
s = ss.accept();
output = new
ObjectOutputStream(s.getOutputStream());
output.flush();
input = new ObjectInputStream(s.getInputStream());
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
t = new
Thread(this,"PeerTwo");
System.out.print("New Thread");
t.start();
}
public static void main(String[] args)
{
new PeerTwo();
}
public void run()
{
while(true)
{
if(dataAvailable==true)
{
try
{
System.out.print("bbb");
output.writeObject(outgoing.getText());
output.flush();
dataAvailable = false;
}
catch
(IOException e1)
{
e1.printStackTrace();
}
}
try {
try
{
message =
(String)input.readObject();
System.out.print(0);
incoming.append(message);
}
catch
(ClassNotFoundException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
No comments:
Post a Comment