Client
Clienttest class: Responsible for communicating with the server, establishing two threads, sending and receiving commands issued by the client and receiving commands from the server side respectively.
Clienttest3.java
Package Game;import java.io.*; Import java.net.*; public class clienttest3{public static void Main (string[] args) {try{//link Sever end Socket S1 = new Socket ("127.0.0.1", 10004); System.out.println ("Link in Succeed ..."); System.out.println ("Term 1:"); Instantiated input stream InputStream is = S1.getinputstream (); DataInputStream dis = new DataInputStream (IS); Instantiate the output stream outputstream OS = S1.getoutputstream (); DataOutputStream dos = new DataOutputStream (OS); Instantiation of two processes Thread MCR = new Myclientreader (dis); Thread MCW = new Myclientwriter (Dos); Start two processes Mcr.start (); Mcw.start (); Catch Exception}catch (SocketException e) {System.out.println (e); }catch (IOException e) {System.out.println (e); }}}//Create a process to receive 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 side downline, 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 ("self-downline, program exit"); System.exit (0); }}}catch (IOException e) {System.out.println (e); } } }
Monster Card Game design and implementation of the client-side chapter