Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- public class Cliente extends JFrame implements ActionListener{
- private JButton tablero[][];
- private JPanel panelTablero;
- private Container contenedor;
- private int size = 4;
- private int numeroJugador;
- private Socket conexion;
- private int cliente;
- private ObjectInputStream entrada;
- private ObjectOutputStream salida;
- private Point punto;
- private ImageIcon ficha1;
- private String IP;
- public Cliente(int sizeTablero){
- size = sizeTablero;
- ficha1 = new ImageIcon("foto1.gif");
- contenedor = getContentPane();
- panelTablero = new JPanel(new GridLayout(sizeTablero, sizeTablero));
- tablero = new JButton [sizeTablero][sizeTablero];
- for (int i =0; i<sizeTablero ; i++){
- for (int j =0; j<sizeTablero ; j++){
- tablero[i][j] = new JButton();
- tablero[i][j].addActionListener(this);
- panelTablero.add(tablero[i][j]);
- }
- }
- contenedor.add(panelTablero);
- setVisible(true);
- setSize(200,200);
- }
- public void conectarServidor(){
- IP=JOptionPane.showInputDialog("Introduce ip servidor(localhost para máquina local):");
- //Conexion CON EL SERVIDOR por el puerto 5001
- try
- {
- conexion=new Socket(IP,12345);
- salida=new ObjectOutputStream(conexion.getOutputStream());
- entrada=new ObjectInputStream(conexion.getInputStream());
- }
- catch(IOException ioe)
- {
- System.out.println("No es posible conetar");
- }
- }
- public void actionPerformed(ActionEvent e) {
- for (int i =0; i<size ; i++){
- for (int j =0; j<size ; j++){
- if(e.getSource().equals(tablero[i][j])){
- enviarPunto(""+i+j);
- try {
- procesarConexion();
- } catch (IOException ex) {
- System.out.println("error de lectura-escritura");
- }
- }
- }
- }
- }
- public void enviarPunto(String punto) {
- // enviar objeto al cliente
- try {
- salida.writeObject(punto);
- salida.flush();
- System.out.println(punto);
- }
- // procesar problemas que pueden ocurrir al enviar el objeto
- catch ( IOException excepcionES ) {
- System.out.println( "\nError al escribir objeto" );
- }
- }
- // procesar la conexi?n con el servidor
- public void procesarConexion() throws IOException
- {
- // enviar mensaje de conexi?n exitosa al cliente
- String mensaje = "Conexi?n exitosa";
- //enviarDatos( mensaje );
- do { // procesar los mensajes enviados por el cliente
- // leer el mensaje y mostrarlo en pantalla
- try {
- mensaje = ( String ) entrada.readObject();
- int x = Integer.parseInt(""+mensaje.charAt(1));
- int y = Integer.parseInt(""+mensaje.charAt(2));
- punto = new Point(x,y);
- cambiarFicha(punto);
- //System.out.println( punto.x+","+punto.y);
- }
- // atrapar problemas que pueden ocurrir al tratar de leer del cliente
- catch ( ClassNotFoundException excepcionClaseNoEncontrada ) {
- System.out.println( "\nSe recibi? un tipo de objeto desconocido" );
- }
- } while (!mensaje.contains("#"));
- } // fin del m?todo procesarConexion
- public void cambiarFicha(Point punto) {
- tablero[punto.x][punto.y].setIcon(ficha1);
- }
- public static void main(String args[]){
- //String dimension = JOptionPane.showInputDialog("Digite el tamaño del tablero");
- Cliente cliente = new Cliente(4);
- cliente.conectarServidor();
- cliente.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
- import java.awt.*;
- import java.io.*;
- import java.net.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Servidor{
- private Jugador[] jugadores;
- private ServerSocket servidor;
- private int jugadorX;
- private int tablero[][];
- private int size = 4;
- public Servidor(int sizeTablero){
- jugadores = new Jugador[2];
- size = sizeTablero;
- tablero = new int[size][size];
- for (int i =0; i<size ; i++){
- for (int j =0; j<size ; j++){
- tablero[i][j]=0;
- }
- }
- // set up ServerSocket
- try {
- servidor = new ServerSocket( 12345, 2 );
- }
- // process problems creating ServerSocket
- catch( IOException ioException ) {
- ioException.printStackTrace();
- System.exit( 1 );
- }
- }
- // wait for two connections so game can be played
- public void execute()
- {
- // wait for each client to connect
- for ( int i = 0; i < jugadores.length; i++ ) {
- // wait for connection, create Player, start thread
- try {
- jugadores[ i ] = new Jugador( servidor.accept(), i );
- jugadores[ i ].start();
- }
- // process problems receiving connection from client
- catch( IOException ioException ) {
- ioException.printStackTrace();
- System.exit( 1 );
- }
- }
- }// end method execute
- public void validarMover(Point punto){
- if (tablero[punto.x][punto.y] == 0){
- tablero[punto.x][punto.y] = 1;
- // wait for each client to connect
- for ( int i = 0; i < jugadores.length; i++ ) {
- jugadores[ i ].enviarPunto("#"+punto.x+punto.y);
- }
- }
- }
- public static void main(String args[]){
- Servidor cliente = new Servidor(4);
- cliente.execute();
- }
- class Jugador extends Thread{
- private int numeroJugador;
- private int ficha;
- private int fichaX;
- private int fichaO;
- private Socket conexion;
- private ObjectInputStream entrada;
- private ObjectOutputStream salida;
- private boolean suspender;
- private Point punto;
- public Jugador(Socket socket, int numero){
- numeroJugador = numero;
- conexion = socket;
- // obtain streams from Socket
- try {
- entrada = new ObjectInputStream( conexion.getInputStream() );
- salida = new ObjectOutputStream( conexion.getOutputStream() );
- }
- // process problems getting streams
- catch( IOException ioException ) {
- ioException.printStackTrace();
- System.exit( 1 );
- }
- }
- public void enviarPunto(String punto) {
- // enviar objeto al cliente
- try {
- salida.writeObject(punto);
- salida.flush();
- System.out.println(punto);
- }
- // procesar problemas que pueden ocurrir al enviar el objeto
- catch ( IOException excepcionES ) {
- System.out.println( "\nError al escribir objeto" );
- }
- }
- public void suspender(boolean estado){
- suspender = estado;
- }
- @Override
- public void run() {
- do { // procesar los mensajes enviados por el cliente
- try {
- // procesar los mensajes enviados por el cliente
- // leer el mensaje y mostrarlo en pantalla
- String mensaje = (String) entrada.readObject();
- int x = Integer.parseInt(""+mensaje.charAt(0));
- int y = Integer.parseInt(""+mensaje.charAt(1));
- punto = new Point(x,y);
- validarMover(punto);
- System.out.println( punto.x+","+punto.y);
- } catch (IOException ex) {
- Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
- }
- } while ( true );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement