0

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();  
    }
}
Marko Malbasic
  • 149
  • 1
  • 10
  • 2
    Any reason you don't want to use `JComponent`s? – Jeremy Feb 07 '15 at 13:52
  • I realize that I can not use the GUI components in the form of AWT. I'm still a beginner, learn java language, this is my first encounters with programming. how to put this method? Can you explain? :) – Marko Malbasic Feb 07 '15 at 14:01
  • You could replace `Frame` with `JFrame`, `Label` with `JLabel`, etc. – Jeremy Feb 07 '15 at 14:05
  • 1
    I know I can do it, but I'm learning AWT, the next course is a GUI and then I will be allowed to use the GUI components. – Marko Malbasic Feb 07 '15 at 14:11
  • @MarkoMalbasic What is supposed to do your program, am I suppose to run 2 instances and talk with each other ? – Jean-François Savard Feb 07 '15 at 14:48
  • Yes, I have a server and client side, it is the simple chat application – Marko Malbasic Feb 07 '15 at 14:52
  • *"I know I can do it, but I'm learning AWT,.."* That's a waste of time, do directly to Swing. See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 07 '15 at 23:39
  • "That's a waste of time, do directly to Swing", ok thx mate, but i don't do it.... – Marko Malbasic Feb 08 '15 at 13:25

0 Answers0