Monster Card Game design and implementation of the client-side chapter

Source: Internet
Author: User

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

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.