Clase servidor:
Código
import java.net.ServerSocket; import java.net.Socket; import java.io.ObjectOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import javax.swing.JOptionPane; public class XServer { private boolean isRunning=true; private volatile Command cmd=null; XServer(){ try{ s = ss.accept(); ioex.printStackTrace(); } this.sender(); this.receiver(); this.dealReceive(); this.autoClose(); } void sender(){ public void run(){ while(XServer.this.isRunning){ int type=0; try{ Command c=new Command(); c.setMsg(msg); type=(msg.equalsIgnoreCase("Close"))? 0:1; c.setType(type); oos.writeObject(c); ex.printStackTrace(); } if(type==0){ break; } } } }); t.start(); } void receiver(){ public void run(){ while(XServer.this.isRunning){ try{ if(aux!=null && aux instanceof Command){ XServer.this.cmd = (Command) aux; } intex.printStackTrace(); ioex.printStackTrace(); classex.printStackTrace(); } } } }); t.start(); } void dealReceive(){ public void run(){ while(XServer.this.isRunning){ try{ Command c=XServer.this.cmd; if(c.getType().equals("Message")){ }else if(c.getType().equals("Action") && c.getMsg().equals("Close")){ XServer.this.isRunning=false; } intex.printStackTrace(); }finally{ XServer.this.cmd=null; } } } }); t.start(); } void autoClose(){ public void run(){ while(true){ try{ if(!XServer.this.isRunning){ XServer.this.ois.close(); XServer.this.oos.close(); XServer.this.s.close(); } intex.printStackTrace(); ioex.printStackTrace(); } } } }); t.start(); } }
Clase cliente:
Código
import java.net.Socket; import java.io.ObjectOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import javax.swing.JOptionPane; public class XClient { private boolean isRunning=true; private volatile Command cmd=null; XClient(){ try{ ioex.printStackTrace(); } this.sender(); this.receiver(); this.dealReceive(); this.autoClose(); } void receiver(){ public void run(){ while(XClient.this.isRunning){ try{ if(aux!=null && aux instanceof Command){ XClient.this.cmd = (Command) aux; } intex.printStackTrace(); ioex.printStackTrace(); classex.printStackTrace(); } } } }); t.start(); } void dealReceive(){ public void run(){ while(XClient.this.isRunning){ try{ Command c=XClient.this.cmd; if(c.getType().equals("Message")){ }else if(c.getType().equals("Action") && c.getMsg().equals("Close")){ XClient.this.isRunning=false; } intex.printStackTrace(); }finally{ XClient.this.cmd=null; } } } }); t.start(); } void sender(){ public void run(){ while(XClient.this.isRunning){ int type=0; try{ Command c=new Command(); c.setMsg(msg); type=(msg.equalsIgnoreCase("Close"))? 0:1; c.setType(type); oos.writeObject(c); ex.printStackTrace(); } if(type==0){ break; } } } }); t.start(); } void autoClose(){ public void run(){ while(true){ try{ if(!XClient.this.isRunning){ XClient.this.ois.close(); XClient.this.oos.close(); XClient.this.s.close(); } intex.printStackTrace(); ioex.printStackTrace(); } } } }); t.start(); } }
Se me olvidaba, para lo que quiero hacer despues de aprender a manipular sockets en internet creé una clase comando, que en el futuro me servirá para mandar solo strings con referencias a lo que se debe hacer y no mandar objetos pesados a traves de la conexion, esta es la clase:
Código
import java.io.Serializable; Command(){ this.type= types[1]; } this.type=this.types[0]; this.msg=msg; } this.type=this.types[type]; this.msg=msg; } return msg; } this.msg=msg; this.type=types[2]; } this.msg=msg; this.type=types[3]; } this.msg=msg; this.type=types[4]; } this.msg=msg; this.type=types[5]; } this.msg = msg; } public void setType(int i){ this.type=this.types[i]; } return this.type; } }
Bueno, de antemano agradezco muchísimo vuestras respuestas, saludos