Сервер:
| Код | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Random; import java.util.Vector;
/** * * @author User */ public class Main {
/** * @param args the command line arguments */ public static void main(String[] args) throws IOException { ServerSocket socket = new ServerSocket(3123); Socket acceptor = socket.accept(); PrintWriter out = new PrintWriter(acceptor.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader(acceptor.getInputStream())); String input = null; Vector<Point> mass = new Vector<Point>(); Random r = new Random(); while ((input = in.readLine()) != null){ mass.add(new Point(input, 0));
if(mass.size()==9) {}
int randX =-1; int randY = -1; while(true) { randX = r.nextInt(3); randY = r.nextInt(3); for (int i = 0; i < 9-mass.size(); i++) { if(mass.elementAt(i).getX() != randX && mass.elementAt(i).getY() != randY) { Point newp = new Point(randX+":"+randY+":1"); mass.add(newp); out.println("Button:"+randX+":"+randY); break; } } } } } }
|
у меня классы поинты нужны для сервера и клиента различаются..это все из-за лени:
| Код | package slovaserver;
/** * * @author User */ public class Point {
@Override public String toString() { return x+":"+y+":"+value; }
private int x; private int y; private int value; public Point(int x, int y, int value) { this.x = x; this.y = y; this.value = value; } public Point(String str) { String[] s = str.split(":"); this.x = Integer.parseInt(s[0].toString()); this.y = Integer.parseInt(s[1]); this.value = Integer.parseInt(s[2]); }
public Point(String str, int value) { String[] s = str.split(":"); this.x = Integer.parseInt(s[1].toString()); this.y = Integer.parseInt(s[2]); this.value = value; } public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; } public void setY(int y) { this.y = y; }
public int getValue() { return value; } public void setValue(int value) { this.value = value; }
}
|
клиент:
| Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */
package noughtsandcrosses;
import java.awt.Color; import java.awt.Component; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JComponent;
/** * * @author User */ public class Main extends JFrame {
public Main() throws IOException{ super(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //this.setLayout(null); this.setSize(250,250);
GridLayout gl = new GridLayout(3, 3); setLayout (new GridLayout(3, 3)); // 3 rows, 3 columns, no gaps JButton b; for (int row = 0; row < 3; row ++) { int i=0; b = new JButton(); b.setName("Button:"+i+":"+row); i++; b.addActionListener(new CommandButton()); add (b); b = new JButton(); b.addActionListener(new CommandButton()); b.setName("Button:"+i+":"+row); i++; add (b); b = new JButton(); b.setName("Button:"+i+":"+row); b.addActionListener(new CommandButton()); add (b);
}
Client();
} class CommandButton implements ActionListener {
public void actionPerformed(ActionEvent e) { try { JButton jb = (JButton)e.getSource();
jb.setBackground(Color.red); Send(jb);
jb.removeActionListener(this); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
}
Socket kkSocket = null; PrintWriter out = null; BufferedReader in = null; BufferedReader stdIn = null; void Client() throws IOException { try { kkSocket = new Socket("localhost", 3123); out = new PrintWriter(kkSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream())); stdIn = new BufferedReader(new InputStreamReader(System.in)); } catch (UnknownHostException e) { e.printStackTrace(); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection."); e.printStackTrace(); System.exit(1); } } void Send(JButton b) throws IOException { String fromServer; String fromUser;
fromUser = b.getName(); out.println(fromUser); System.out.println("Send to server: " + fromUser); fromServer = in.readLine(); this.getComponents(); //Найти Кнопку нажатую и задать задний фон и удалить обработчик Component[]x=this.getContentPane().getComponents(); for(int i=0;i<x.length;i++) { JButton but = (JButton)x[i]; if(but.getName().equals(fromServer)) { but.setBackground(Color.blue); but.removeActionListener(but.getActionListeners()[0]); stdIn.close(); break; }
} System.out.println("Received from server: " + fromServer); } void Close() throws IOException { out.close(); stdIn.close(); in.close(); kkSocket.close();
}
public static void main(String[] args) throws IOException { // TODO code application logic here Main form = new Main(); form.setVisible(true); form.setResizable(false); }
}
|
клиент поинт
| Код | /* * To change this template, choose Tools | Templates * and open the template in the editor. */
package noughtsandcrosses;
/** * * @author User */ public class Point {
@Override public String toString() { return x+":"+y+":"+value; }
private int x; private int y; private int value; public Point(int x, int y, int value) { this.x = x; this.y = y; this.value = value; } public Point(String str) { String[] s = str.split(":"); this.x = Integer.parseInt(s[0].toString()); this.y = Integer.parseInt(s[1]); this.value = Integer.parseInt(s[2]); } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; }
public void setY(int y) { this.y = y; } public int getValue() { return value; } public void setValue(int value) { this.value = value; }
}
|
|