Design and Implementation of Monster Card Game client, monstercard
Client
ClientTest class: responsible for communicating with the server and establishing two threads to send and receive commands from the client and those from the server respectively.
ClientTest3.java
Package game; import java. io. *; import java.net. *; public class ClientTest3 {public static void main (String [] args) {try {// link sever Socket s1 = new Socket ("127.0.0.1", 10004); System. out. println ("Link in succeed... "); System. out. println ("Term 1:"); // instantiate the input stream InputStream is = s1.getInputStream (); DataInputStream dis = new DataInputStream (is ); // instantiate the output stream OutputStream OS = s1.getOutputStream (); DataOutputStream dos = new DataOutputStream (OS); // instantiate two processes Thread mcr = new MyClientReader (dis ); thread mcw = new MyClientWriter (dos); // start two processes: mcr. start (); mcw. start (); // catch exceptions} catch (SocketException e) {System. out. println (e);} catch (IOException e) {System. out. println (e) ;}}// create a process to receive and read data. class MyClientReader extends Thread {private DataInputStream dis; public MyClientReader (DataInputStream dis) {this. dis = dis;} @ Override public void run () {String msg; try {while (true) {msg = dis. readUTF (); System. out. println (msg); if (msg. equals ("bye") {System. out. println ("the other party goes offline, the program exits"); System. exit (0) ;}} catch (IOException e) {System. out. println (e) ;}}// create a process to write and send data class MyClientWriter extends Thread {private DataOutputStream dos; public MyClientWriter (DataOutputStream dos) {this. dos = dos;} @ Override public void run () {InputStreamReader isr = new InputStreamReader (System. in); BufferedReader br = new BufferedReader (isr); String msg; try {while (true) {msg = br. readLine (); dos. writeUTF (msg); if (msg. equals ("bye") {System. out. println ("deprecate yourself, exit the program"); System. exit (0) ;}} catch (IOException e) {System. out. println (e );}}}