-2

the problems:

  1. I think the java compiler runs the compiled classes and doesn't re-compile them every time. How do I check that?
  2. It doesn't add the JButton to the JFrame
  3. I don't know if the mouselistener works
  4. The socket does not seem to connect(the port is open and there is a host like that-my school computers)

Code:

package alpha;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Base extends JFrame implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener{
    JButton close=new JButton("close");
    JButton open=new JButton("open");
    boolean closed=false,opened=false;
    protected JTextField setPort = new JTextField("set port");
    protected JTextField setInitAdress = new JTextField("set host");
    protected JTextArea textArea = new JTextArea();
    protected DataOutputStream toServer;
    protected DataInputStream fromServer;
    Socket socket=null;
    int portNumber;
    String initAddress;
    public Base(int portNumber,String host){
        setLayout(new FlowLayout());
        //default values for port+host e.g 123.56.4. or "aa.stand.com"-can connect via my web site
        this.portNumber=portNumber;
        this.initAddress=host;


        addMouseListener(this); 
        addMouseMotionListener(this);
        addMouseWheelListener(this);
        addKeyListener(this);


        setPort.setText(this.setPort+"");
        setInitAdress.setText(this.initAddress);
        setPort.setToolTipText("this sets the port");
        setInitAdress.setToolTipText("set adress for socket");
        textArea.setEditable(false);

        JPanel a=new JPanel();
        a.setLayout(new FlowLayout());
        a.add(close);
        a.add(open);
        a.add(textArea);
        a.add(setInitAdress);
        a.add(setPort);
        add(a,BorderLayout.SOUTH);



        setVisible(true);
        setSize(600,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
    public void mouseWheelMoved(MouseWheelEvent e) {}
    public void mouseDragged(MouseEvent arg0) {}
    public void mouseMoved(MouseEvent arg0) {}
    public void mouseClicked(MouseEvent arg0) {}
    public void mouseEntered(MouseEvent arg0) {}
    public void mouseExited(MouseEvent arg0) {}
    public void mousePressed(MouseEvent arg0) {}
    public void mouseReleased(MouseEvent arg0) {}

    protected String read(){
        String str="";
        try {
            if(opened)
                str = fromServer.readUTF();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(str!=""){
            textArea.append("read packet , content: "+str);
            return str; 
        }
        return "";
    }

    protected void send(int a) {
        try {
            toServer.writeInt(a);
            toServer.flush();
            textArea.append("sent:"+a);
        } catch (IOException e) {
            e.printStackTrace();
        }   

    }

    protected void send(String a) {
        try {
            toServer.writeUTF(a);
            toServer.flush();
            textArea.append("sent:"+a);
        } catch (IOException e) {
            e.printStackTrace();
        }   
    }




}

package alpha;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import javax.sound.midi.Receiver;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Controlled extends Base implements ActionListener{
    Robot robot=null;
    ServerSocket serverSocket;
    int numpc;

    public Controlled(int port,String host){
        super(port,host);
        setTitle("client");
        try {
            robot=new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        close.addActionListener(this);
        open.addActionListener(this);
        while(true){
            String recieved=read();
            int x=0,y=0,button=InputEvent.BUTTON1_DOWN_MASK;
            if(recieved.indexOf(" ")!=-1){
            x=Integer.parseInt(recieved.substring(10,recieved.indexOf(" ")));
            y=Integer.parseInt(recieved.substring(recieved.indexOf(" ")+1),recieved.indexOf(" ",recieved.indexOf(" ")));
            button=Integer.parseInt(recieved.substring(recieved.indexOf(" ",recieved.indexOf(" "))+1, recieved.length()));
            }
            if(recieved.length()>10){
                if(recieved.substring(0, 10).equals("keypressed")){
                    robot.keyPress(Integer.parseInt(recieved.substring(10)));
                }
                else if(recieved.substring(0, 10).equals("keyrelease")){
                    robot.keyRelease(Integer.parseInt(recieved.substring(10)));
                }
                else if(recieved.substring(0, 10).equals("keytypeded")){
                    robot.keyPress(Integer.parseInt(recieved.substring(10)));
                    robot.keyRelease(Integer.parseInt(recieved.substring(10)));
                }
                else if(recieved.substring(0, 10).equals("mousepress")){
                    robot.mouseMove(x, y);
                    robot.mousePress(button); 
                }
                else if(recieved.substring(0, 10).equals("mouserelea")){
                    robot.mouseMove(x, y);
                    robot.mouseRelease(button); 
                }
                else if(recieved.substring(0, 10).equals("mousemoved")){
                    robot.mouseMove(x, y);
                }
                else if(recieved.substring(0, 10).equals("mousedragg")){
                    robot.mouseMove(x, y);
                }
                else if(recieved.substring(0, 10).equals("mouseclick")){
                    robot.mouseMove(x, y);
                    robot.mousePress(button);
                    robot.mouseRelease(button);
                }
                else if(recieved.substring(0, 10).equals("wheelmoved")){
                    robot.mouseWheel(Integer.parseInt(recieved.substring(10)));
                }
            }
        }
    }

    public void openSocket(){
        System.out.println("entered");
        try {
            serverSocket=new ServerSocket(this.portNumber);
            socket=serverSocket.accept();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent arg0) {
        Object help=arg0.getSource();
        if(help instanceof JButton){
            JButton but=(JButton)help;
            if(but.getActionCommand()=="open"||but.getActionCommand()=="close"){
                if(setInitAdress.getText()!=null)
                    this.initAddress=setInitAdress.getText();
                if(setPort.getText()!=null)
                    this.portNumber=Integer.parseInt(setInitAdress.getText());
                if(but.getActionCommand()=="open"){
                    open();
                }
                if(but.getActionCommand()=="close"){
                    close();
                }
            }
        }

    }

    protected void close() {
        try {
            toServer.close();
            fromServer.close();
            socket.close();
            textArea.append("closed connection");
            closed=true;
            opened=false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void open() {
        try {
            if(!(closed==opened==false)){
                close();
            }
            openSocket();
            toServer=new DataOutputStream(socket.getOutputStream());
            fromServer=new DataInputStream(socket.getInputStream());
            textArea.append("opened connection with host:"+this.initAddress+" and port number:"+this.portNumber);
            opened=true;
            closed=false;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

package alpha;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
public class Controller extends Base implements ActionListener{
    public Controller(int port,String host){
        super(port,host);
        close.addActionListener(this);
        open.addActionListener(this);
        setTitle("server");
        setVisible(true);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private void openSocket(){
        try {
            socket=new Socket(this.initAddress,this.portNumber);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void keyPressed(KeyEvent e){send("keypressed"+e.getKeyCode());}
    public void keyReleased(KeyEvent e) {send("keyrelease"+e.getKeyCode());}
    public void keyTyped(KeyEvent e) {send("keytypeded"+e.getKeyCode());}
    public void mousePressed(MouseEvent e) {send("mousepress"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}
    public void mouseReleased(MouseEvent e) {send("mouserelea"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}


    public void mouseDragged(MouseEvent e) {send("mousedragg"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}
    public void mouseMoved(MouseEvent e) {send("mousemoved"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}
    public void mouseClicked(MouseEvent e) {send("mouseclick"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}
    //public void mouseEntered(MouseEvent e) {send("mouseenter"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}
    //public void mouseExited(MouseEvent e) {send("mouseexit"+e.getXOnScreen()+" "+e.getYOnScreen()+" "+e.getButton());}

    //finish action for those methods in client
    public void mouseWheelMoved(MouseWheelEvent e) {send("wheelmoved"+e.getScrollAmount());}



    public void actionPerformed(ActionEvent arg0) {
        Object help=arg0.getSource();
        if(help instanceof JButton){
            JButton but=(JButton)help;
            if(but.getActionCommand()=="open"||but.getActionCommand()=="close"){
                if(setInitAdress.getText()!=null)
                    this.initAddress=setInitAdress.getText();
                if(setPort.getText()!=null)
                    this.portNumber=Integer.parseInt(setInitAdress.getText());
                if(but.getActionCommand()=="open"){
                    open();
                }
                if(but.getActionCommand()=="close"){
                    close();
                }
            }
        }

    }


    protected void close() {
        try {
            toServer.close();
            fromServer.close();
            socket.close();
            textArea.append("closed connection");
            closed=true;
            opened=false;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    protected void open() {
        try {
            if(!(closed==opened==false)){
                close();
            }
            openSocket();
            toServer=new DataOutputStream(socket.getOutputStream());
            fromServer=new DataInputStream(socket.getInputStream());
            textArea.append("opened connection with host:"+this.initAddress+" and port number:"+this.portNumber);
            opened=true;
            closed=false;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

package alpha;
public class Test {
    public static void main(String[]args){
        Controlled controlled=new Controlled(4004, "M11");
        controlled.open();
        Controller controller=new Controller(4004, "M11");
        controller.open();  
        Base b=new Base(4000,"M11");
    }
}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
  • 2
    please strip your example down to the barest minimum that demonstrates the problem - nobody wants to wade through tons of unrelated code – kleopatra Nov 21 '11 at 13:15

1 Answers1

2

Don't execute I/O code in the AWT thread, i.e. in action listeners, and don't execute it in constructors either. Use a separate dedicated thread per Socket.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • even when i call Base b=new Base(3000,"M11"); (no call to socket, i still don't get all the gui JButton drawen out) – user1057415 Nov 21 '11 at 15:37
  • @user1057415: For reference, there's a working example [here](http://stackoverflow.com/questions/3244400/socket-using-in-a-swing-applet/3245805#3245805). – trashgod Nov 21 '11 at 15:41