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 ServerSocket ss;
private Socket s;
private ObjectOutputStream oos;
private boolean isRunning=true;
private ObjectInputStream ois;
private volatile Command cmd=null;
XServer(){
try{
ss = new ServerSocket(9999);
s = ss.accept();
oos=new ObjectOutputStream(s.getOutputStream());
ois=new ObjectInputStream(s.getInputStream());
}catch(IOException ioex){
ioex.printStackTrace();
}
this.sender();
this.receiver();
this.dealReceive();
this.autoClose();
}
void sender(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XServer.this.isRunning){
int type=0;
try{
String msg=JOptionPane.showInputDialog("Mensaje para el Cliente");
Command c=new Command();
c.setMsg(msg);
type=(msg.equalsIgnoreCase("Close"))? 0:1;
c.setType(type);
oos.writeObject(c);
}catch(IOException ex) {
ex.printStackTrace();
}
if(type==0){
break;
}
}
}
});
t.start();
}
void receiver(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XServer.this.isRunning){
try{
Thread.sleep(1000);
Object aux=ois.readObject();
if(aux!=null && aux instanceof Command){
XServer.this.cmd = (Command) aux;
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(IOException ioex){
ioex.printStackTrace();
}catch(ClassNotFoundException classex){
classex.printStackTrace();
}
}
}
});
t.start();
}
void dealReceive(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XServer.this.isRunning){
try{
Thread.sleep(1000);
Command c=XServer.this.cmd;
if(c.getType().equals("Message")){
System.out.println(c.getMsg());
}else if(c.getType().equals("Action") && c.getMsg().equals("Close")){
XServer.this.isRunning=false;
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(NullPointerException nullex){
}finally{
XServer.this.cmd=null;
}
}
}
});
t.start();
}
void autoClose(){
Thread t=new Thread(new Runnable(){
public void run(){
while(true){
try{
Thread.sleep(200);
if(!XServer.this.isRunning){
XServer.this.ois.close();
XServer.this.oos.close();
XServer.this.s.close();
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(IOException ioex){
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 Socket s;
private ObjectOutputStream oos;
private boolean isRunning=true;
private ObjectInputStream ois;
private volatile Command cmd=null;
XClient(){
try{
s=new Socket("10.5.4.124", 9999);
oos=new ObjectOutputStream(s.getOutputStream());
ois=new ObjectInputStream(s.getInputStream());
}catch(IOException ioex){
ioex.printStackTrace();
}
this.sender();
this.receiver();
this.dealReceive();
this.autoClose();
}
void receiver(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XClient.this.isRunning){
try{
Thread.sleep(1000);
Object aux=ois.readObject();
if(aux!=null && aux instanceof Command){
XClient.this.cmd = (Command) aux;
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(IOException ioex){
ioex.printStackTrace();
}catch(ClassNotFoundException classex){
classex.printStackTrace();
}
}
}
});
t.start();
}
void dealReceive(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XClient.this.isRunning){
try{
Thread.sleep(1000);
Command c=XClient.this.cmd;
if(c.getType().equals("Message")){
System.out.println(c.getMsg());
}else if(c.getType().equals("Action") && c.getMsg().equals("Close")){
XClient.this.isRunning=false;
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(NullPointerException nullex){
}finally{
XClient.this.cmd=null;
}
}
}
});
t.start();
}
void sender(){
Thread t=new Thread(new Runnable(){
public void run(){
while(XClient.this.isRunning){
int type=0;
try{
String msg=JOptionPane.showInputDialog("Mensaje para el Server");
Command c=new Command();
c.setMsg(msg);
type=(msg.equalsIgnoreCase("Close"))? 0:1;
c.setType(type);
oos.writeObject(c);
}catch(IOException ex) {
ex.printStackTrace();
}
if(type==0){
break;
}
}
}
});
t.start();
}
void autoClose(){
Thread t=new Thread(new Runnable(){
public void run(){
while(true){
try{
Thread.sleep(200);
if(!XClient.this.isRunning){
XClient.this.ois.close();
XClient.this.oos.close();
XClient.this.s.close();
}
}catch(InterruptedException intex){
intex.printStackTrace();
}catch(IOException ioex){
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;
public class Command implements Serializable{
private String[] types={"Action", "Message", "Dialog", "Input", "Warning", "Error"};
private String[] actions={"Close"};
private String type="";
private String msg="";
Command(){
this.type= types[1];
}
Command(String msg){
this.type=this.types[0];
this.msg=msg;
}
Command(String msg, int type){
this.type=this.types[type];
this.msg=msg;
}
public String getMsg() {
return msg;
}
void setDialog(String msg){
this.msg=msg;
this.type=types[2];
}
void setInput(String msg){
this.msg=msg;
this.type=types[3];
}
void setWarning(String msg){
this.msg=msg;
this.type=types[4];
}
void setError(String msg){
this.msg=msg;
this.type=types[5];
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setType(int i){
this.type=this.types[i];
}
public String getType(){
return this.type;
}
}
Bueno, de antemano agradezco muchísimo vuestras respuestas, saludos










Autor



En línea






)
cualquier error alguien me corrige, saludos.

