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

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

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

    點對點聊天簡陋版

    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);?//實例化一個服務器
    ????????cs.startServer();????//啟動服務器
    ????}

    }


    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: 點對點聊天簡陋版 2015-12-21 23:54 更多時候該

    來個人更好的更好  回復  更多評論   

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

    @更多時候該
    543543   回復  更多評論   

    <2015年12月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類(16)

    隨筆檔案(16)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲嫩草影院在线观看| 大桥未久亚洲无av码在线| 毛片免费视频在线观看| 亚洲男同gay片| 亚洲精品无码久久千人斩| 男人的好看免费观看在线视频| 瑟瑟网站免费网站入口| 久久亚洲国产成人精品性色| 永久黄网站色视频免费| 日本亚洲欧洲免费天堂午夜看片女人员| 亚洲中文字幕无码av在线| 亚洲精品国产自在久久| 亚洲大片免费观看| 一级毛片无遮挡免费全部| 亚洲乱码卡一卡二卡三| 亚洲精品成人久久久| 在线观看免费高清视频| 你懂的网址免费国产| 亚洲国产综合AV在线观看| 中文字幕亚洲第一在线| 丁香五月亚洲综合深深爱| 在线免费观看一级毛片| 18以下岁毛片在免费播放| 国产精品小视频免费无限app| 亚洲免费综合色在线视频| 亚洲成AV人片在| 亚洲高清视频一视频二视频三| 亚洲一区二区三区免费在线观看 | 国产一级a毛一级a看免费视频| 中文字幕 亚洲 有码 在线| 亚洲精品无码不卡在线播HE| 日韩视频免费一区二区三区| 99久在线国内在线播放免费观看 | 亚洲自偷自偷偷色无码中文| 女人与禽交视频免费看| 亚洲精品免费在线观看| 国产精品免费大片一区二区| 美景之屋4在线未删减免费| 亚洲一级特黄特黄的大片| 亚洲色成人网一二三区| 亚洲AV日韩精品久久久久久|