Created
December 11, 2014 14:24
-
-
Save mrabaev48/56b0876ac7e322ca500b to your computer and use it in GitHub Desktop.
ServerXO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package serverx0; | |
/** | |
* | |
* @author jshvartsman | |
*/ | |
import java.io.*; | |
import java.net.*; | |
class Connection extends Thread { | |
protected Socket client1; | |
protected Socket client2; | |
protected DataInputStream in1; | |
protected DataOutputStream out1; | |
protected DataInputStream in2; | |
protected DataOutputStream out2; | |
public Connection(Socket client1, Socket client2) { | |
this.client1 = client1; | |
this.client2 = client2; | |
try { | |
in1 = new DataInputStream(client1.getInputStream()); | |
out1 = new DataOutputStream(client1.getOutputStream()); | |
in2 = new DataInputStream(client2.getInputStream()); | |
out2 = new DataOutputStream(client2.getOutputStream()); | |
out1.writeInt(101); | |
out2.writeInt(101); | |
} catch (IOException exc) { | |
try { | |
client1.close(); | |
client2.close(); | |
} catch (IOException e) { | |
System.out.println(e.toString()); | |
System.out.println(); | |
} | |
System.out.println(exc.toString()); | |
return; | |
} catch (Exception exc) { | |
System.out.println(exc.toString()); | |
System.out.println(); | |
} | |
this.start(); | |
} | |
@Override | |
public void run() { | |
int box, turns, waste, i; | |
int[] board = new int[9]; | |
try { | |
/** * **********************/ | |
/* out1.writeInt(249); | |
String name1 = in1.readUTF(); | |
out2.writeInt(249); | |
String name2 = in2.readUTF(); | |
out1.writeInt(248); | |
out1.writeUTF(name2); | |
out2.writeInt(248); | |
out2.writeUTF(name1); | |
*/ | |
/** * *********************/ | |
for (turns = 0; turns < 9; turns++) { | |
if (turns % 2 == 0) { | |
waste = in1.available(); //кол-во байтов в потоке | |
for (i = 0; i < waste; i++) { in1.readByte(); } | |
box = in1.readInt(); | |
} else { | |
waste = in2.available(); | |
for (i = 0; i < waste; i++) { in2.readByte(); } | |
box = in2.readInt(); | |
} | |
if (board[box] == 0) { | |
i = turns % 2; | |
board[box] = i + 1; | |
// 1 - X 2 - 0 // 0 - 8 X //10 - 18 0 | |
out1.writeInt(box + (10 * i)); | |
out2.writeInt(box + (10 * i++)); | |
//255 - winner code //254 - loser code | |
if ( //horiz | |
(board[0] == i && board[1] == i && board[2] == i) | |
|| (board[3] == i && board[4] == i && board[5] == i) | |
|| (board[6] == i && board[7] == i && board[8] == i) | |
|| //vert | |
(board[0] == i && board[3] == i && board[6] == i) | |
|| (board[1] == i && board[4] == i && board[7] == i) | |
|| (board[2] == i && board[5] == i && board[8] == i) | |
|| //diag | |
(board[0] == i && board[4] == i && board[8] == i) | |
|| (board[2] == i && board[4] == i && board[6] == i)) { | |
if (turns % 2 == 0) { | |
out1.writeInt(255); out2.writeInt(254); | |
} else { | |
out1.writeInt(254); out2.writeInt(255); | |
} | |
break; | |
} /** | |
* **************************************** | |
*/ | |
else { | |
out1.writeInt(turns % 2 == 0 ? 252 : 251); | |
out2.writeInt(turns % 2 != 0 ? 252 : 251); | |
} | |
} else { | |
// out1.writeInt(9); | |
// out2.writeInt(9); | |
out1.writeInt(turns % 2 == 0 ? 9 : 253); | |
out2.writeInt(turns % 2 != 0 ? 9 : 253); | |
turns--; | |
} | |
/*** ********************/ | |
} | |
} catch (IOException exc) { System.out.println(exc.toString()); | |
/******************/ | |
try { | |
if (client1.isConnected()) { out1.writeInt(250); } | |
if (client2.isConnected()) { out2.writeInt(250); } | |
} catch (Exception ee) { System.out.println(ee.toString()); | |
} | |
/*****************/ | |
} catch (Exception exc) { | |
System.out.println(exc.toString()); | |
} finally { | |
try { client1.close(); client2.close(); | |
} catch (IOException exc) { System.out.println(exc.toString()); | |
} catch (Exception exc) { System.out.println(exc.toString()); | |
} | |
} | |
System.out.println("Disconnect"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package serverx0; | |
import java.io.*; | |
import java.net.*; | |
/** | |
* | |
* @author jshvartsman | |
*/ | |
public class Server extends Thread { | |
public final int DEFAULT_PORT = 1701; | |
protected int port; | |
protected ServerSocket listener_socket; | |
public Server(int port) { | |
if (port == 0) { | |
this.port = DEFAULT_PORT; | |
} else { | |
this.port = port; | |
} | |
try { | |
listener_socket = new ServerSocket(this.port); | |
} catch (IOException exc) { | |
System.out.println("Exception creating server socket"); | |
System.out.println(exc.getMessage()); | |
System.exit(1); | |
} catch (Exception exc) { | |
System.out.println("Exception creating server socket"); | |
System.out.println(exc.getMessage()); | |
System.exit(1); | |
} | |
System.out.println("Server: listening on port " + this.port); | |
this.start(); | |
} | |
@Override | |
public void run() { | |
while (true) { | |
try { | |
Socket client_socket1 = listener_socket.accept(); | |
Socket client_socket2 = listener_socket.accept(); | |
Connection c = new Connection( | |
client_socket1, client_socket2); | |
} catch (IOException exc) { | |
System.out.println("Exception while listening for connections"); | |
System.out.println(exc.toString()); | |
System.exit(1); | |
} catch (Exception exc) { | |
System.out.println(exc.toString()); | |
System.exit(1); | |
} | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package serverx0; | |
/** | |
* | |
* @author jshvartsman | |
*/ | |
public class ServerX0 { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
int port = 0; | |
if(args.length == 1){ | |
try{ | |
port = Integer.parseInt(args[0]); | |
}catch(Exception exc){ | |
System.out.println(exc.toString()); | |
} | |
} | |
Server server = new Server(port); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment