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他

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

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


          網站導航:
           
          主站蜘蛛池模板: 诏安县| 枞阳县| 青神县| 齐齐哈尔市| 资阳市| 安阳市| 江川县| 永年县| 莎车县| 临西县| 瑞安市| 连城县| 景谷| 武清区| 大安市| 台中县| 房产| 庆城县| 武宣县| 淅川县| 大田县| 齐齐哈尔市| 辽宁省| 阿拉善左旗| 伊金霍洛旗| 开平市| 无锡市| 山阳县| 临朐县| 务川| 山东省| 博乐市| 珠海市| 唐山市| 沈丘县| 镇原县| 五峰| 兴城市| 台州市| 遵义市| 古浪县|