the_fire的技術博客

          勤奮讓我享受更多生命的樂趣
          數據加載中……
          Java實現一個控制臺的多線程聊天程序
          剛學習完Java基本內容,寫個程序

          就是想把多線程,IO操作,socket都用上,不過看起來好像簡單些了。不過這里面肯定有些問題是我沒想到的。
          大家看看,提點意見,主要是面向對象方面

          import java.net.*;
          import java.io.*;
          import java.util.*;

          public class SocketDemo {
              
          public static void main(String[] args) throws Exception {
                  
          if(args.length <= 0 || args.length > 2 ) {
                      System.out.println(
          "I just think you don't know how to use this program.");
                      System.out.println(
          "Please use '-help' to get more help");
                      System.exit(
          -1);
                  }

                  
                  
          if(args[0].equals("help"|| args[0].equals("-help")) {
                      System.out.println(IODemo.getHelp());
                      System.exit(
          -1);
                  }

                  
                  Thread tServer 
          = new TServer("server",Integer.valueOf(args[0]));
                  Client client 
          = new Client(Integer.valueOf(args[1]));

                  
          //get words from terminal
                  BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
                  
                  
          //Start the Server
                  tServer.start();
                  String s 
          = "";
                  
                  
          while(!(s = sin.readLine()).equals("")) {
                      client.send(s);
                      
                      IODemo.writeLog(s);
                      
                      
          if(s.equals("bye")) {
                          System.exit(
          -1);
                      }

                  }

              }

          }


          class TServer extends Thread {
              
          int port = 6666;
              
              TServer(String s,
          int port) {
                  
          super(s);
                  
          this.port = port;
              }


              
          public void run() {
                  String strMessage 
          = "";

                  
          try {
                      ServerSocket ss 
          = new ServerSocket(port);
                      Socket s;
                      DataInputStream dis;
                      
                      
          while(true{
                          s 
          = ss.accept();
                          dis 
          = new DataInputStream(s.getInputStream());
                          
                          
          if(!(strMessage = dis.readUTF()).equals("")) {
                              System.out.println(
          "Client: " + strMessage);
                              IODemo.writeLog(
          "Client: " + strMessage);
                              strMessage 
          = "";
                          }

                          
                          dis.close();
                          s.close();
                      }

                      
                  }
           catch(Exception e) {
                      
          //e.printStackTrace();
                      System.out.println("服務器異常退出!");
                      System.exit(
          -1);
                  }

                  
              }

          }


          class Client {
              
          int port = 8888;
              
              Client(
          int port) {
                  
          this.port = port;
              }

              
              
          public void send(String strMessage) throws Exception {
                  Socket s 
          = new Socket("127.0.0.1",port);
                  OutputStream os 
          = s.getOutputStream();
                  DataOutputStream dos 
          = new DataOutputStream(os);
                  dos.writeUTF(strMessage);
                  dos.flush();
                  dos.close();
                  s.close();
              }
              
          }


          class IODemo {

              
          public static String getHelp() {
                  String str 
          = "";
                  String strTmp 
          = null;
                  
          try {
                      BufferedReader br 
          = new BufferedReader(new FileReader("d:\\help.dat"));
                      
          while((strTmp = br.readLine())!=null{
                              str 
          += strTmp + "\n\r" ;
                      }

                      br.close();
                      
                  }
           catch(IOException e) {
                      
          //e.printStackTrace();
                      System.out.println("幫助文件獲得失敗!");
                      System.exit(
          -1);
                  }

                  
          return str;
                  
              }

              
              
          public static void writeLog(String s) {
                  
          try {
                      FileWriter fw 
          = new FileWriter("d:\\log.log",true);
                      PrintWriter log 
          = new PrintWriter(fw);
                      log.println(s
          +"------"+ new Date()+ "----");
                      log.flush();
                      log.close();
                  }
           catch(IOException e) {
                      
          //e.printStackTrace();
                      System.out.println("日志文件寫入錯誤");
                      System.exit(
          -1);
                  }

              }

              
          }


          按照下圖運行,當然了幫助文件要先寫好。在D盤help.dat文件中寫:
          There are two parameter in this program.
          Java TCPServer serverPort clientPort.

          For example:

          Run one as this:java TCPServer 6666 8888

          And another one as this:java TCPServer 8888 6666

          Then the two intances can communicate.

          And you can by pass 'bye' on the terminal to exit this program.

          Enjoy it!




          posted on 2010-09-21 14:55 the_fire 閱讀(1280) 評論(1)  編輯  收藏 所屬分類: Java

          評論

          # re: Java實現一個控制臺的多線程聊天程序 2014-11-29 14:32 wear他

          我覺得這個只能實現兩個人聊天啊
            回復  更多評論    

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 博爱县| 叶城县| 双城市| 来安县| 玉树县| 北海市| 阿荣旗| 辽宁省| 原平市| 乌什县| 宜兴市| 桃江县| 峨眉山市| 科技| 高要市| 福贡县| 彩票| 白朗县| 班戈县| 华亭县| 施甸县| 文登市| 昌江| 南宫市| 沐川县| 肥西县| 海城市| 会泽县| 烟台市| 陕西省| 阜平县| 拉萨市| 铜山县| 申扎县| 喀喇沁旗| 咸丰县| 华宁县| 循化| 鄄城县| 儋州市| 科尔|