Design and Implementation of Monster Card Game client, monstercard

Source: Internet
Author: User

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 );}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.