<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    點(diǎn)對點(diǎ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í)例化一個服務(wù)器
    ????????cs.startServer();????//啟動服務(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 閱讀(621) 評論(2)  編輯  收藏 所屬分類: J2SE

    評論

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

    來個人更好的更好  回復(fù)  更多評論   

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

    @更多時候該
    543543   回復(fù)  更多評論   

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

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類(16)

    隨筆檔案(16)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产一精品一aⅴ一免费| 亚洲国产AV一区二区三区四区| 国产精品二区三区免费播放心| 免费国产黄网站在线观看视频| 无人视频免费观看免费视频 | 亚洲欧美日韩一区二区三区 | 99re6在线精品视频免费播放| 免费手机在线看片| 中文日韩亚洲欧美制服| 亚洲美女精品视频| 亚洲AV日韩AV永久无码下载| 亚洲最大AV网站在线观看| 免费a在线观看播放| 国产美女无遮挡免费视频网站 | 日产亚洲一区二区三区| 亚洲AV综合色区无码一区爱AV| 中文字幕第一页亚洲| 四虎永久免费地址在线网站| 日韩毛片无码永久免费看| 女人18毛片水真多免费看| 中文字幕影片免费在线观看| 国产精品怡红院永久免费| 日韩免费高清大片在线| 日韩精品在线免费观看| 国内少妇偷人精品视频免费| 西西人体免费视频| 野花香在线视频免费观看大全| 国产午夜精品理论片免费观看| 久久久久久毛片免费看| 国产精品永久免费| 中文字幕a∨在线乱码免费看| 中文字幕不卡免费高清视频| 成人免费ā片在线观看| 两个人日本免费完整版在线观看1| 久久久受www免费人成| 国产做国产爱免费视频| WWW免费视频在线观看播放| a级毛片在线免费| 91久久青青草原线免费| 成人午夜免费福利视频| 免费观看AV片在线播放|