I set the server as well as all the events associated with it and the form with listeners, and the main class.
The class starts form is activated, but the server does not eject a text message to a text zone, and this suggests that the method to showMessage()
is probably wrong.
This is the code:
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.SwingUtilities;
public class MiniChat {
private TextField tf2;
private TextArea ta;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
//
public MiniChat(){
Frame f = new Frame("MiniChat");
TextField tf = new TextField();
TextField tf1 = new TextField("127.0.0.1");
tf1.setEditable(false);
final TextField tf2 = new TextField();
final TextArea ta = new TextArea();
Button b = new Button("Poslaji!");
b.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
ta.setText(tf2.getText());
tf2.setText("");
}
});
Label l = new Label("IP adresa korisnika: ");
Label l1 = new Label("Lokalna IP adresa: ");
Label l2 = new Label("Polje za prikaz poruka: ");
Label l3 = new Label("Ukucajte poruku: ");
f.add(l);
f.add(tf);
f.add(l1);
f.add(tf1);
f.add(l2);
f.add(ta);
f.add(l3);
f.add(tf2);
f.add(b);
f.setVisible(true);
f.setSize(400, 400);
f.setLayout(new FlowLayout());
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
}
public void startRunning(){
try{
server = new ServerSocket(6789, 100);
while(true){
try{
waitForConnection();
setupStreams();
whileChatting();
}catch(EOFException eofe){
showMessage("\nServer Ended Connection");
} finally {
closeCrap();
}
}
}catch(IOException ie){
ie.printStackTrace();
}
}
private void waitForConnection() throws IOException{
showMessage("Waiting for Someone to Connect\n");
connection = server.accept();
showMessage("Now Connected "+ connection.getInetAddress().getHostName());
}
private void setupStreams() throws IOException{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
showMessage("\n Streams are now setup");
}
private void whileChatting() throws IOException{
String message = "You Are Connected";
sendMessage(message);
ableToType(true);
do{
try{
message = (String) input.readObject();
showMessage("\n" + message);
}catch(ClassNotFoundException cnfe){
showMessage("\nI dont know what user send");
}
}while(!message.equals("CLIENT - END"));
}
private void closeCrap(){
showMessage("\n Closing Connection...\n");
ableToType(false);
try{
output.close();
input.close();
connection.close();
}catch(IOException ie){
ie.printStackTrace();
}
}
private void sendMessage(String message){
try{
output.writeObject("SERVER - " +message);
output.flush();
showMessage("SERVER - " +message);
}catch(IOException ie){
ta.append("I cant send message");
}
}
private void showMessage(final String text){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
ta.append(text);
}
}
);
}
private void ableToType(final boolean tof){
SwingUtilities.invokeLater(
new Runnable(){
public void run(){
ta.setEditable(tof);
}
}
);
}
}
And this is the main class:
import java.awt.Frame;
import javax.swing.*;
public class Cs {
public static void main(String[] args){
MiniChat mc = new MiniChat();
mc.startRunning();
}
}