亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? connectionmodel.java

?? 用JAVA實(shí)現(xiàn)的ICHAT源代碼!希望對大家有用!
?? JAVA
字號:
/*
 * @(#)ConnectionManager.java
 * Created on 2005-9-14
 * iChat LE. Copyright AllenStudio. All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package core;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * This implementation of <code>ChatClient</code>
 * interface uses {@link java.net.Socket} to send
 * and receive messages.<br>
 * This class is a background model for setting up 
 * connection and data transmission.<br>
 * <em>Note: for special reasons, this class is a
 * singleton</em>
 * 
 * @version 1.0 Sep 14, 2005
 * @author Allen Chue
 */
public class ConnectionModel implements IChatClient {

    public static final int SERVER_PORT = 1129;
    
    private static ConnectionModel instance = null;
    
    private ConnectionListener connectionListener;
    private MessageListener messageListener;
    private Thread daemonMessageChecker;

    /** Socket to receive messages (as a server) */
    private ServerSocket serverSocket;
    
    /** Socket for other client to connect to it */
    private Socket connectionSocket;
    
    /** Socket to send messages (as a client) */
    private Socket clientSocket;
    
    /** Stream to receive input from a remote friend */
    private DataInputStream inputFromFriend;
    
    /** Stream to send output to a remote friend */
    private DataOutputStream outputToFriend;
    
    private boolean isConnectedAsServer = false;
    private boolean isConnectedAsClient = false;
    
    private ConnectionModel() throws RuntimeException {
        if (!openPort()) {
            throw new RuntimeException("Cannot open the server port!");
        }
    }
    
    
    /* (non-Javadoc)
     * @see core.IChatClient#sendMessage(java.lang.String)
     */
    public boolean sendMessage(String message) {
        return false;
    }

    /* (non-Javadoc)
     * @see core.IChatClient#displayReceivedMessage()
     */
    public void displayReceivedMessage() {
    }

    /**
     * Connects to a remote friend by the host name.
     * @param host the name or IP address of remote client
     * @return true if connected successfully
     * @see core.IChatClient#connectToFriend(java.lang.String)
     */
    public boolean connectToFriend(String host, String selfName) {
        try {
            clientSocket = new Socket(host, SERVER_PORT);
            //Init the output stream
            outputToFriend = new DataOutputStream(
                    clientSocket.getOutputStream());
            
            send(selfName);
            
            fireConnectionAsClientChanged(true);            
            return true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * Opens the port to accept remote connection.
     * @see core.IChatClient#openPort()
     */
    public boolean openPort() {
        try {
            serverSocket = new ServerSocket(SERVER_PORT);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /* (non-Javadoc)
     * @see core.IChatClient#getFriendAddress()
     */
    public InetAddress getFriendAddress() {
        if (connectionSocket != null) {
            return connectionSocket.getInetAddress();
        }
        return null;
    }
    
    /**
     * Returns if this client is connected as a client
     * @return true if connected
     */
    public boolean isConnectedAsClient() {
        return isConnectedAsClient;
    }

    /**
     * Returns if this client is connected as a server,
     * that is, it is connected by a remote client.
     * @return true if connected
     */
    public boolean isConnectedAsServer() {
        return isConnectedAsServer;
    }
        
    /**
     * Accepts remote connection. This method is synchronized
     * since it is not supposed more than one clients try to
     * connect a single port. (iChat LE is currently peer to
     * peer, and exclusive).
     * @return true if a remote client connects to this client
     * successfully, false otherwise
     */
    public synchronized boolean acceptConnection() {
        if (serverSocket == null) {
            return false;
        }
        else {
            try {
                connectionSocket = serverSocket.accept();
                
                //Init input stream
                inputFromFriend = new DataInputStream(
                        connectionSocket.getInputStream());                
                
                fireConnectionAsServerChanged(true);
                return true;
            }
            catch (IOException e) {
                return false;
            }
        }
    }
    
    /**
     * Adds a <code>ConnectionListener</code> to this model.
     * Addition to existing listener has no effect.
     * @param l the listener to be added
     */
    public void addConnectionListener(ConnectionListener l) {
        if (connectionListener == null) {
            connectionListener = l;
        }
    }
    
    /**
     * Removes the <code>ConnectionListener</code> attached
     * to this model, if any.
     */
    public void removeConnectionListener() {
        connectionListener = null;
    }
    
    /**
     * Adds a <code>MessageListener</code> to this model.
     * Addition to existing listener has no effect.
     * @param l the listener to be added
     */
    public void addMessageListener(MessageListener l) {
        if (messageListener == null) {
            daemonMessageChecker = new Thread() {
                public void run() {
                    try {
                        String s = null;
                        while (true) {
                            if ((s = ConnectionModel.this.receive()) != null) {
                                fireMessageReceived(s);
                            }
                            Thread.sleep(1000);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            daemonMessageChecker.setPriority(3);
            daemonMessageChecker.start();
            messageListener = l;
        }
    }
    
    /**
     * Removes the <code>MessageListener</code> attached
     * to this model, if any.
     */
    public void removeMessageListener() {
        messageListener = null;
    }
    
    /**
     * Sends a <code>String</code> s.
     * @param s the String to send
     */
    public void send(String s) {
        if (outputToFriend != null) {
            try {
                outputToFriend.writeUTF(s);
                outputToFriend.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public String receive() {
        if (inputFromFriend != null) {
            try {
                return inputFromFriend.readUTF();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    public static ConnectionModel getInstance() throws RuntimeException {
        if (instance == null) {
            instance = new ConnectionModel();
            return instance;
        }
        else return instance;
    }

    /**
     * Subclasses may derive this method to change the
     * behavior when the connection as a server changes.
     * @param b true if the connection succeeds, false otherwise
     */
    protected void fireConnectionAsServerChanged(boolean b) {
        isConnectedAsServer = b;
        
        if (connectionListener != null) {
            connectionListener.connectionAsServerChanged(b);
        }
    }
    
    /**
     * Subclasses may derive this method to change the
     * behavior when the connection as a client changes.
     * @param b true if the connection succeeds, false otherwise
     */
    protected void fireConnectionAsClientChanged(boolean b) {
        isConnectedAsClient = b;
        
        if (connectionListener != null) {
            connectionListener.connectionAsClientChanged(b);
        }
    }
    
    /**
     * Subclasses may derive this method to change the
     * behavior when a message from remote friend is received.
     * @param s the message received
     */
    protected void fireMessageReceived(String s) {
        if (messageListener != null) {
            messageListener.messageReceived(s);
        }
    }
    

    /**
     * @param args
     */
    public static void main(String[] args) {

    }

}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲图片你懂的| 国产一区二区三区高清播放| 青青草伊人久久| 成人国产在线观看| 欧美电视剧在线观看完整版| 亚洲色图制服诱惑| 国产黄色91视频| 日韩免费成人网| 亚洲一区二区高清| 91在线免费播放| 国产欧美日韩不卡| 另类综合日韩欧美亚洲| 欧美日韩国产精品成人| 亚洲日本va午夜在线电影| 国产老肥熟一区二区三区| 在线观看91精品国产麻豆| 夜夜嗨av一区二区三区网页| 国产成人在线视频网站| 精品99一区二区| 蜜桃视频在线观看一区二区| 欧美色综合久久| 亚洲综合小说图片| 成人国产精品视频| 不卡欧美aaaaa| 91麻豆精品91久久久久久清纯| 中文字幕中文字幕在线一区 | 成人99免费视频| 国产欧美精品日韩区二区麻豆天美| 波多野结衣在线一区| 亚洲天天做日日做天天谢日日欢| 欧美视频在线一区二区三区| 国产欧美一区二区三区网站| 99久久99久久精品国产片果冻| 亚洲一区二区三区在线看| 在线成人av网站| 国产精品一卡二卡| 国产欧美精品区一区二区三区 | 国产乱人伦偷精品视频不卡| 国产欧美一区二区三区网站| 欧美日韩成人综合在线一区二区| 国模套图日韩精品一区二区| 亚洲免费资源在线播放| 精品久久人人做人人爱| 欧美三级午夜理伦三级中视频| 国模娜娜一区二区三区| 欧美国产日韩精品免费观看| 欧美一区二区网站| 国内精品写真在线观看| 香蕉av福利精品导航| 色综合久久99| 国产99久久精品| 亚洲自拍另类综合| 中文字幕一区二区三区不卡| 欧美精品乱码久久久久久按摩| 国产91丝袜在线观看| 美女视频黄 久久| 国产精品天干天干在线综合| 欧美mv和日韩mv的网站| 欧美麻豆精品久久久久久| 99精品1区2区| 国产91精品欧美| 国产一区不卡视频| 精品在线一区二区| 久色婷婷小香蕉久久| 日韩国产欧美三级| 日韩经典中文字幕一区| 一区二区三区在线看| 中文字幕日韩一区二区| 国产午夜三级一区二区三| 日韩免费高清视频| 日韩亚洲欧美一区二区三区| 欧美日韩精品是欧美日韩精品| 日本高清不卡在线观看| 一本久久综合亚洲鲁鲁五月天 | 亚洲天堂网中文字| 欧美成人伊人久久综合网| 777色狠狠一区二区三区| 国产精品一区二区不卡| 国内精品国产成人国产三级粉色| 日本在线不卡视频一二三区| 亚洲综合无码一区二区| 一级日本不卡的影视| 精品美女一区二区三区| 欧美成人在线直播| 91麻豆精品国产综合久久久久久| 欧美伊人久久久久久久久影院| 91国内精品野花午夜精品 | 亚洲自拍偷拍av| 亚洲综合精品自拍| 国产精品二区一区二区aⅴ污介绍| 国产欧美精品一区二区色综合朱莉| 欧美日韩国产大片| 欧美精品自拍偷拍| 欧美成人r级一区二区三区| 欧美日本在线观看| 欧美一卡二卡三卡| 欧美日韩免费在线视频| 欧美一级欧美三级在线观看 | 麻豆精品一二三| 五月天一区二区| 国内欧美视频一区二区| 蜜臀av性久久久久蜜臀aⅴ| 国产资源精品在线观看| 美日韩一区二区三区| 国产麻豆精品视频| 日本欧美大码aⅴ在线播放| 国产一区二区三区久久久| 麻豆高清免费国产一区| 久久国产尿小便嘘嘘尿| 国产ts人妖一区二区| 激情综合色综合久久| 成人av集中营| 欧美中文字幕一二三区视频| 国产精品全国免费观看高清| 中文字幕在线一区免费| 久久久久久久网| 中文字幕视频一区| 日本不卡一二三区黄网| 国产一区二区久久| 欧美午夜精品一区二区三区| 99久久精品免费观看| 日韩视频在线你懂得| 精品理论电影在线观看| 国产精品国产三级国产普通话99| 午夜精品福利视频网站| 老司机免费视频一区二区三区| 成人精品小蝌蚪| 91网站最新地址| 久久众筹精品私拍模特| 欧美一区二区三区在线| 国产精品久久久久久久第一福利| 久久久国际精品| 亚洲国产日韩一级| 裸体一区二区三区| 色嗨嗨av一区二区三区| 欧美成人伊人久久综合网| 亚洲免费视频中文字幕| 亚洲网友自拍偷拍| jizzjizzjizz欧美| 这里只有精品视频在线观看| 亚洲国产精华液网站w| 欧美日韩一区二区三区在线看| 欧美情侣在线播放| 国产精品美女久久久久久久 | 亚洲精品视频免费看| 国内外成人在线| 色综合久久66| 欧美国产乱子伦| 一区二区成人在线| av动漫一区二区| 久久精品视频免费观看| 国产精品日日摸夜夜摸av| 韩国精品久久久| 色婷婷精品久久二区二区蜜臀av | 精品国产乱码久久久久久老虎| 亚洲高清免费一级二级三级| 久久精品99久久久| 在线影院国内精品| 国产精品青草综合久久久久99| 日韩av网站免费在线| 欧美无砖专区一中文字| 欧美一二区视频| 亚洲黄色小说网站| 99久久久久久99| 精品国产乱码久久久久久浪潮| 美腿丝袜亚洲一区| 99re成人精品视频| 亚洲欧美色综合| 蜜臀a∨国产成人精品| 3d动漫精品啪啪一区二区竹菊| 亚洲综合色噜噜狠狠| 国产91丝袜在线播放0| 国产日韩欧美a| 亚洲国产毛片aaaaa无费看| 欧美性色黄大片| 自拍偷自拍亚洲精品播放| 国产69精品久久久久777| 日本一区二区三区在线不卡| 蜜臀91精品一区二区三区| 欧美大黄免费观看| 国产麻豆欧美日韩一区| 欧美国产精品劲爆| 99re8在线精品视频免费播放| 亚洲精品视频在线| 欧美久久久久久蜜桃| 经典一区二区三区| 国产精品另类一区| 91麻豆精品在线观看| 五月婷婷色综合| 精品欧美一区二区在线观看 | 青青草成人在线观看| 亚洲精品一区二区三区精华液 | 欧美日本一区二区在线观看| 日韩 欧美一区二区三区| 久久精品视频一区| 一本色道久久综合亚洲aⅴ蜜桃| 一区二区成人在线观看| 制服丝袜亚洲播放| 亚洲小说欧美激情另类| 欧美在线一二三四区|