點(diǎn)對(duì)點(diǎn)聊天簡(jiǎn)陋版

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

          public?class?ChatServer
          {
          ????ServerSocket?server?
          =?null;
          ????ArrayList
          <ClientConn>?clients?=?new?ArrayList<ClientConn>();
          ????
          ????
          public?ChatServer(int?port)?throws?Exception
          ????
          {
          ????????server?
          =?new?ServerSocket(port);
          ????}

          ????
          ????
          public?void?startServer()?throws?Exception
          ????
          {
          ????????
          while(true)
          ????????
          {
          ????????????Socket?s?
          =?server.accept();
          ????????????clients.add(?
          new?ClientConn(s)?);
          ????????}

          ????}

          ????
          ????
          class?ClientConn?implements?Runnable
          ????
          {
          ????????Socket?s?
          =?null;
          ????????
          public?ClientConn(Socket?s)
          ????????
          {
          ????????????
          this.s?=?s;
          ????????????(
          new?Thread(this)).start();
          ????????}

          ????????
          ????????
          public?void?send(String?str)?throws?IOException
          ????????
          {
          ????????????DataOutputStream?dos?
          =?new?DataOutputStream(s.getOutputStream());
          ????????????dos.writeUTF(str);
          ????????}

          ????????
          ????????
          public?void?run()
          ????????
          {
          ????????????
          try?{
          ????????????????
          ????????????????DataInputStream?dis?
          =?new?DataInputStream(s.getInputStream());
          ????????????????String?str?
          =?dis.readUTF();
          ????????????????
          while(str?!=?null?&&?str.length()?!=0)
          ????????????????
          {
          ????????????????????System.out.println(str);
          ????????????????????
          for(Iterator<ClientConn>?it?=?clients.iterator();?it.hasNext();?)
          ????????????????????
          {
          ????????????????????????ClientConn?cc?
          =?(ClientConn)it.next();
          ????????????????????????
          if(this?!=?cc)????
          ????????????????????????
          {
          ????????????????????????????cc.send(str);
          ????????????????????????}

          ????????????????????}

          ????????????????????str?
          =?dis.readUTF();
          ????????????????}

          ????????????????s.close();
          ????????????????clients.remove(
          this);
          ????????????}
          ?
          ????????????
          catch?(IOException?e)?
          ????????????
          {
          ????????????????System.out.println(
          "client?quit");
          ????????????????
          try?
          ????????????????
          {
          ????????????????????
          if(s?!=?null)
          ????????????????????????s.close();
          ????????????????????clients.remove(
          this);
          ????????????????}
          ?
          ????????????????
          catch?(IOException?ioe)
          ????????????????
          {
          ????????????????????ioe.printStackTrace();
          ????????????????}

          ????????????}

          ????????????
          ????????}

          ????}

          ????
          ????
          public?static?void?main(String[]?args)?throws?Exception
          ????
          {
          ????????ChatServer?cs?
          =?new?ChatServer(6666);?//實(shí)例化一個(gè)服務(wù)器
          ????????cs.startServer();????//啟動(dòng)服務(wù)器
          ????}

          }


          ChatClient.java
          import?java.io.*;
          import?java.net.*;
          import?java.awt.*;
          import?java.awt.event.*;

          public?class?ChatClient?extends?Frame
          {
          ????TextArea?ta?
          =?new?TextArea();
          ????TextField?tf?
          =?new?TextField();
          ????
          public?void?launchFrame()?throws?Exception
          ????
          {
          ????????
          this.add(ta,?BorderLayout.CENTER);
          ????????
          this.add(tf,?BorderLayout.SOUTH);
          ????????tf.addActionListener(
          ????????????
          new?ActionListener()?
          ????????????
          {
          ????????????????
          public?void?actionPerformed(ActionEvent?ae)
          ????????????????
          {
          ????????????????????
          try?{
          ????????????????????????String?sSend?
          =?tf.getText();
          ????????????????????????
          if(sSend.trim().length()?==?0)?return;
          ????????????????????????ChatClient.
          this.send(sSend);
          ????????????????????????tf.setText(
          "");
          ????????????????????????ta.append(sSend?
          +?"\n");
          ????????????????????}

          ????????????????????
          catch?(Exception?e)?{?e.printStackTrace();?}
          ????????????????}

          ????????????}

          ????????????);
          ????????
          ????????setBounds(
          300,300,300,400);
          ????????setVisible(
          true);
          ????????tf.requestFocus();
          ????}

          ????
          ????Socket?s;
          ????
          public?ChatClient()?throws?Exception
          ????
          {
          ????????s?
          =?new?Socket("127.0.0.1",?6666);
          ????????launchFrame();
          ????????(
          new?Thread(new?ReceiveThread(s))).start();
          ????}

          ????
          ????
          public?void?send(String?str)?throws?Exception
          ????
          {
          ????????DataOutputStream?dos?
          =?new?DataOutputStream(s.getOutputStream());
          ????????dos.writeUTF(str);
          ????}

          ????
          ????
          public?void?disconnect()?throws?Exception
          ????
          {
          ????????s.close();
          ????}

          ????
          ????
          public?static?void?main(String[]?args)?throws?Exception
          ????
          {
          ????????BufferedReader?br?
          =?new?BufferedReader?(
          ????????????????????????????????
          new?InputStreamReader(System.in));
          ????????ChatClient?cc?
          =?new?ChatClient();
          ????????String?str?
          =?br.readLine();
          ????????
          while(str?!=?null?&&?str.length()?!=?0)
          ????????
          {
          ????????????cc.send(str);
          ????????????str?
          =?br.readLine();
          ????????}

          ????????cc.disconnect();
          ????}

          ????
          ????
          class?ReceiveThread?implements?Runnable
          ????
          {
          ????????
          private?Socket?s;

          ????????
          public?ReceiveThread(Socket?s)?{
          ????????????
          this.s?=?s;
          ????????}


          ????????
          public?void?run()
          ????????
          {
          ????????????
          if(s?==?null)?return;
          ????????????
          try?{
          ????????????????DataInputStream?dis?
          =?new?DataInputStream(s.getInputStream());
          ????????????????String?str?
          =?dis.readUTF();
          ????????????????
          while?(str?!=?null?&&?str.length()?!=?0)
          ????????????????
          {
          ????????????????????ChatClient.
          this.ta.append(str?+?"\n");
          ????????????????????str?
          =?dis.readUTF();
          ????????????????}

          ????????????}
          ?
          ????????????
          catch?(Exception?e)
          ????????????
          {
          ????????????????e.printStackTrace();
          ????????????}

          ????????????
          ????????}

          ????}

          }

          posted on 2008-09-15 01:30 nonels 閱讀(629) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): J2SE

          評(píng)論

          # re: 點(diǎn)對(duì)點(diǎn)聊天簡(jiǎn)陋版 2015-12-21 23:54 更多時(shí)候該

          來(lái)個(gè)人更好的更好  回復(fù)  更多評(píng)論   

          # re: 點(diǎn)對(duì)點(diǎn)聊天簡(jiǎn)陋版 2015-12-21 23:55 更多時(shí)候該

          @更多時(shí)候該
          543543   回復(fù)  更多評(píng)論   

          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類(lèi)(16)

          隨筆檔案(16)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 大化| 哈巴河县| 洪雅县| 甘洛县| 昭觉县| 诸暨市| 城口县| 洛南县| 吉木萨尔县| 鹰潭市| 岳阳市| 民县| 神木县| 西盟| 闻喜县| 章丘市| 曲松县| 武隆县| 石首市| 云和县| 西峡县| 章丘市| 淮南市| 同德县| 海南省| 石家庄市| 青龙| 岗巴县| 江山市| 台东市| 溆浦县| 文安县| 浏阳市| 深泽县| 囊谦县| 涞源县| 新乡县| 万盛区| 盈江县| 大厂| 收藏|