Advertisement
Guest User

Untitled

a guest
May 30th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import javax.swing.*;
  9.  
  10. public class Cliente extends JFrame implements ActionListener{
  11. private JButton tablero[][];
  12. private JPanel panelTablero;
  13. private Container contenedor;
  14. private int size = 4;
  15. private int numeroJugador;
  16. private Socket conexion;
  17. private int cliente;
  18. private ObjectInputStream entrada;
  19. private ObjectOutputStream salida;
  20. private Point punto;
  21. private ImageIcon ficha1;
  22. private String IP;
  23.  
  24. public Cliente(int sizeTablero){
  25. size = sizeTablero;
  26. ficha1 = new ImageIcon("foto1.gif");
  27. contenedor = getContentPane();
  28. panelTablero = new JPanel(new GridLayout(sizeTablero, sizeTablero));
  29. tablero = new JButton [sizeTablero][sizeTablero];
  30. for (int i =0; i<sizeTablero ; i++){
  31. for (int j =0; j<sizeTablero ; j++){
  32. tablero[i][j] = new JButton();
  33. tablero[i][j].addActionListener(this);
  34. panelTablero.add(tablero[i][j]);
  35. }
  36. }
  37. contenedor.add(panelTablero);
  38. setVisible(true);
  39. setSize(200,200);
  40.  
  41. }
  42.  
  43. public void conectarServidor(){
  44. IP=JOptionPane.showInputDialog("Introduce ip servidor(localhost para máquina local):");
  45. //Conexion CON EL SERVIDOR por el puerto 5001
  46. try
  47. {
  48. conexion=new Socket(IP,12345);
  49. salida=new ObjectOutputStream(conexion.getOutputStream());
  50. entrada=new ObjectInputStream(conexion.getInputStream());
  51.  
  52. }
  53. catch(IOException ioe)
  54. {
  55. System.out.println("No es posible conetar");
  56. }
  57. }
  58.  
  59. public void actionPerformed(ActionEvent e) {
  60. for (int i =0; i<size ; i++){
  61. for (int j =0; j<size ; j++){
  62. if(e.getSource().equals(tablero[i][j])){
  63. enviarPunto(""+i+j);
  64. try {
  65. procesarConexion();
  66. } catch (IOException ex) {
  67. System.out.println("error de lectura-escritura");
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. public void enviarPunto(String punto) {
  75. // enviar objeto al cliente
  76. try {
  77. salida.writeObject(punto);
  78. salida.flush();
  79. System.out.println(punto);
  80. }
  81.  
  82. // procesar problemas que pueden ocurrir al enviar el objeto
  83. catch ( IOException excepcionES ) {
  84. System.out.println( "\nError al escribir objeto" );
  85. }
  86. }
  87.  
  88. // procesar la conexi?n con el servidor
  89. public void procesarConexion() throws IOException
  90. {
  91. // enviar mensaje de conexi?n exitosa al cliente
  92. String mensaje = "Conexi?n exitosa";
  93. //enviarDatos( mensaje );
  94.  
  95. do { // procesar los mensajes enviados por el cliente
  96.  
  97. // leer el mensaje y mostrarlo en pantalla
  98. try {
  99. mensaje = ( String ) entrada.readObject();
  100. int x = Integer.parseInt(""+mensaje.charAt(1));
  101. int y = Integer.parseInt(""+mensaje.charAt(2));
  102. punto = new Point(x,y);
  103. cambiarFicha(punto);
  104. //System.out.println( punto.x+","+punto.y);
  105. }
  106.  
  107. // atrapar problemas que pueden ocurrir al tratar de leer del cliente
  108. catch ( ClassNotFoundException excepcionClaseNoEncontrada ) {
  109. System.out.println( "\nSe recibi? un tipo de objeto desconocido" );
  110. }
  111.  
  112. } while (!mensaje.contains("#"));
  113.  
  114. } // fin del m?todo procesarConexion
  115.  
  116. public void cambiarFicha(Point punto) {
  117. tablero[punto.x][punto.y].setIcon(ficha1);
  118. }
  119.  
  120. public static void main(String args[]){
  121. //String dimension = JOptionPane.showInputDialog("Digite el tamaño del tablero");
  122.  
  123. Cliente cliente = new Cliente(4);
  124. cliente.conectarServidor();
  125. cliente.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  126. }
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133. import java.awt.*;
  134. import java.io.*;
  135. import java.net.*;
  136. import java.util.logging.Level;
  137. import java.util.logging.Logger;
  138.  
  139.  
  140. public class Servidor{
  141. private Jugador[] jugadores;
  142. private ServerSocket servidor;
  143. private int jugadorX;
  144. private int tablero[][];
  145. private int size = 4;
  146.  
  147. public Servidor(int sizeTablero){
  148. jugadores = new Jugador[2];
  149. size = sizeTablero;
  150. tablero = new int[size][size];
  151. for (int i =0; i<size ; i++){
  152. for (int j =0; j<size ; j++){
  153. tablero[i][j]=0;
  154. }
  155. }
  156. // set up ServerSocket
  157. try {
  158. servidor = new ServerSocket( 12345, 2 );
  159. }
  160.  
  161. // process problems creating ServerSocket
  162. catch( IOException ioException ) {
  163. ioException.printStackTrace();
  164. System.exit( 1 );
  165. }
  166. }
  167.  
  168. // wait for two connections so game can be played
  169. public void execute()
  170. {
  171. // wait for each client to connect
  172. for ( int i = 0; i < jugadores.length; i++ ) {
  173.  
  174. // wait for connection, create Player, start thread
  175. try {
  176. jugadores[ i ] = new Jugador( servidor.accept(), i );
  177. jugadores[ i ].start();
  178. }
  179.  
  180. // process problems receiving connection from client
  181. catch( IOException ioException ) {
  182. ioException.printStackTrace();
  183. System.exit( 1 );
  184. }
  185. }
  186. }// end method execute
  187. public void validarMover(Point punto){
  188. if (tablero[punto.x][punto.y] == 0){
  189. tablero[punto.x][punto.y] = 1;
  190. // wait for each client to connect
  191. for ( int i = 0; i < jugadores.length; i++ ) {
  192. jugadores[ i ].enviarPunto("#"+punto.x+punto.y);
  193. }
  194. }
  195. }
  196.  
  197. public static void main(String args[]){
  198. Servidor cliente = new Servidor(4);
  199. cliente.execute();
  200. }
  201.  
  202.  
  203.  
  204. class Jugador extends Thread{
  205. private int numeroJugador;
  206. private int ficha;
  207. private int fichaX;
  208. private int fichaO;
  209. private Socket conexion;
  210. private ObjectInputStream entrada;
  211. private ObjectOutputStream salida;
  212. private boolean suspender;
  213. private Point punto;
  214.  
  215. public Jugador(Socket socket, int numero){
  216. numeroJugador = numero;
  217.  
  218.  
  219.  
  220. conexion = socket;
  221.  
  222. // obtain streams from Socket
  223. try {
  224. entrada = new ObjectInputStream( conexion.getInputStream() );
  225. salida = new ObjectOutputStream( conexion.getOutputStream() );
  226. }
  227.  
  228. // process problems getting streams
  229. catch( IOException ioException ) {
  230. ioException.printStackTrace();
  231. System.exit( 1 );
  232. }
  233. }
  234.  
  235. public void enviarPunto(String punto) {
  236. // enviar objeto al cliente
  237. try {
  238. salida.writeObject(punto);
  239. salida.flush();
  240. System.out.println(punto);
  241. }
  242.  
  243. // procesar problemas que pueden ocurrir al enviar el objeto
  244. catch ( IOException excepcionES ) {
  245. System.out.println( "\nError al escribir objeto" );
  246. }
  247. }
  248.  
  249. public void suspender(boolean estado){
  250. suspender = estado;
  251. }
  252.  
  253. @Override
  254. public void run() {
  255.  
  256. do { // procesar los mensajes enviados por el cliente
  257.  
  258. try {
  259. // procesar los mensajes enviados por el cliente
  260. // leer el mensaje y mostrarlo en pantalla
  261. String mensaje = (String) entrada.readObject();
  262. int x = Integer.parseInt(""+mensaje.charAt(0));
  263. int y = Integer.parseInt(""+mensaje.charAt(1));
  264. punto = new Point(x,y);
  265. validarMover(punto);
  266. System.out.println( punto.x+","+punto.y);
  267. } catch (IOException ex) {
  268. Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
  269. } catch (ClassNotFoundException ex) {
  270. Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
  271. }
  272.  
  273. } while ( true );
  274.  
  275.  
  276. }
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement