IMPORTANT: Per accedir als fitxer de subversion: http://acacha.org/svn (sense password). Poc a poc s'aniran migrant els enllaços. Encara però funciona el subversion de la farga però no se sap fins quan... (usuari: prova i la paraula de pas 123456)

misatge

package com.iesebre.DAM2_201213.CarlesRisa.socket;


import java.io.Serializable;

public class missa implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -789552121542155545M;
	                                               
	private int accio = -1;
        private String text = " ";
	private boolean close = false;
	

	public boolean getclose() {
		return close;
	}

	public void setTancat(boolean close) {
		this.close = close;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public missatge(boolean close, String text, int accio) {
		// TODO Auto-generated constructor stub
		this.close = close;
		this.text = text;
		this.accio = accio;
	}


	public missa() {
		// TODO Auto-generated constructor stub
	}

	public int getAccio() {
		return accio;
	}

	public void setAccio(int accio) {
		this.accio = accio;
	}
	
}

server

package com.iesebre.DAM2_201213.CarlesRisa.socket;

//import java.io.DataInputStream;
//import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;



public class serversocket{

	private static ObjectOutputStream out;

	@SuppressWarnings("resource")
	
	public static void main(String[] args) throws ClassNotFoundException, IOException{
	  
		final int port = 8888;
		ServerSocket serverSocket = null;
		Socket socket = null;
		
		ObjectOutputStream out = null;
		
		missatge missadata = null;
		
		try{
			
			serverSocket = new ServerSocket(port);
		    System.out.println("Escoltant port " + port);
		    
		    try{
		    	
		    	socket = serverSocket.accept();
			    System.out.println("IP: " + socket.getInetAddress());
		    
			    while(true){
	   
			    	ObjectInputStream obj = new ObjectInputStream(socket.getInputStream());
			    	Object near = obj.readObject();
	    
			    	if (near instanceof missatge){
	    	
			    		missadata = (missatge) near;
	    	
			    		if (!mdata.getTancat()){
	    		
			    			if (missadata.getAccio() != -1){
	    			
			    				executa(mdata.getAccio());
			    				System.out.println("fes l'acció: " + missadata.getAccio());
			    				
			    			} else {
			    				System.out.println("cap acció");
			    			}
	    		
			    		} else {
	    		
			    			socket.close();
			    			obj.close();
			    			break;
			    		}
	    	
			    	} else {
	    	
			    		System.out.println("No esperat");
	    	
			    	}
			    }
			    
			    serverSocket.close();
			    
		    } catch (Exception e) {
					// TODO: handle exception
			    	e.printStackTrace();
			    	
				}
		} catch (Exception e) {
					// TODO: handle exception
			    	e.printStackTrace();

				}
	   }
	  
	 public static void executa(int num_accio){
		
		try{
			switch (num_accio) {
			case 1:
			    Process p = Runtime.getRuntime().exec("gnome-terminal");
				break;
			case 2:
			    Process o = Runtime.getRuntime().exec("firefox");
			    break;
			default:
				break;
			}
		} catch (Exception e){
			e.printStackTrace();
		}
		
	}
}

client andoid

package com.iesebre.DAM2_201213.CarlesRisa.socket;

import java.io.IOException;
import java.net.UnknownHostException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.io.ObjectOutputStream;
import com.iesebre.DAM2_201213.carlesrisa.socket.R;
import android.app.Activity;
import android.view.View;
import android.os.Bundle;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	
	private Button boto1, boto2, desconectar;
	//private TextView titul;
	
	Socket client;
	
	//private boolean conectat = false;
	ObjectOutputStream out;
	ObjectInputStream in;
	missatge mdata;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		boto1 = (Button) findViewById(R.id.b_boto1);
		boto2 = (Button) findViewById(R.id.b_boto2);
		desconectar = (Button) findViewById(R.id.b_desconectar);
		
		desconectar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					desconectar();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		terminal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				conectat();
				missatge mster = new missatge(false, "", 1);
				enviar(mster);
				
				
				//try {
				//	desconectar();
				//} catch (IOException e) {
					// TODO Auto-generated catch block
				//	e.printStackTrace();
				//}
			}
		});
	
		eclipse.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				conectat();
				missatge msecli = new missatge(false, "", 2);
				enviar(msecli);
				
				
				//try {
				//	desconectar();
				//} catch (IOException e) {
					// TODO Auto-generated catch block
				//	e.printStackTrace();
				//}
			}
		});
	}

	protected void desconectar() throws IOException {
		// TODO Auto-generated method stub
		missatge mdesc = new missatge(true, "", -1);
		enviar(mdesc);
		client.close();
	}

	public boolean enviar(missatge msg){
		try {
			
			out = new ObjectOutputStream(client.getOutputStream());
			missatge missatge = new missatge();
			
			if(client.isConnected()){
				
				missatge = msg;
				out.writeObject(missatge);
				
				return true;
			} else {
				return false;
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
	
	public boolean conectat(){
		
		try {
			client = new Socket("192.168.202.244",8888);
			if (client.isConnected() == true){
				return true;
			} else {
				return false;
			}
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return false;
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}

Resultat

Carlesrisa exercici socket.png