91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java 網絡編程-TCP-多人聊天(面向對象封裝)

發布時間:2020-07-07 13:35:30 來源:網絡 閱讀:512 作者:wx5d21d5e6e5ab1 欄目:編程語言

客戶端:
多個客戶可以正常收發信息,因為可以同時發送和接受信息,不是發送完信息后等待
返回信息,所以要加入多線程

 public class Client {

public static void main(String[]args) throws UnknownHostException, IOException
{
    System.out.println("客戶端啟動中...");

    Socket client =new Socket("localhost",9999);

    new Thread(new Send(client)).start();

    new Thread(new Receive(client)).start();

}
}

客戶端的發送端封裝了:
發送消息
從鍵盤讀取消息
run()
釋放資源

 public class Send implements Runnable{

private BufferedReader br;
private DataOutputStream dos;
private Socket client;
private boolean flag=true;

public Send(Socket client)
{
    this.client=client;
    br=new BufferedReader(new InputStreamReader(System.in));
    try {
        dos=new DataOutputStream(client.getOutputStream());
    } catch (IOException e) {

        this.release();
    }
}
public void run()
{
    while(flag)
    {
        System.out.println("請輸入消息:");
        String msg=getstr();
        send(msg);

    }
}
private void release()
{
    this.flag=false;
    utils.close(dos,client);
}
//從控制臺獲取消息
private String getstr()
{
    try {
        String msg=br.readLine();
        return msg;
    } catch (IOException e) {

        e.printStackTrace();
    }
    return null;
}
private void send(String msg)
{
    try {
        dos.writeUTF(msg);
        dos.flush();

    } catch (IOException e) {
    release();
    }
}
}

客戶端的接收端封裝了:
接收消息
run()
釋放資源

public class Receive implements Runnable {
private DataInputStream dis;
private Socket client;
private boolean flag=true;

public Receive(Socket client)
{
    this.client=client;
    try {
        dis=new DataInputStream(client.getInputStream());
    } catch (IOException e) {
        release();
    }
}

private String receive()
{
    String msg;
    try {
        msg = dis.readUTF();
        return msg;
    } catch (IOException e) {
        release();
    }
    return null;
}
public void run()
{
    while(flag)
    {
        String msg=receive();
        System.out.println(msg);
    }

}
private void release()
{
    this.flag=false;
    utils.close(dis,client);
}
}

作為釋放資源工具類

public class utils {

public static void close(Closeable... target )
{
    for(Closeable it:target)
    {
            try {
                if(null!=it)
                {
                it.close();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }

    }       
}
}

在線聊天室
服務器

 public class Chat {

public static void main(String[]args) throws IOException
{
    System.out.println("服務器啟動中...");

    ServerSocket server=new ServerSocket(9999);

    while(true)
    {
        Socket client =server.accept();
        System.out.println("一個客戶端建立了連接");
        new Thread(new channel(client)).start();

    }

}

static class channel implements Runnable{
    private DataInputStream dis;
    private DataOutputStream dos;
    private BufferedReader br;
    private Socket client;
    private boolean flag;

    public channel(Socket client)
    {
        this.client=client;
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
            flag=true;
        } catch (IOException e) {
            release();          }
    }
    //接收消息
    private String receive() 
    {
        try {
            String msg=dis.readUTF();
            return msg;
        } catch (IOException e) {
            release();
        }
        return null;

    }
    //發送給消息
    private void send(String msg)
    {
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            release();
        }

    }
    private void release()
    {
        this.flag=false;
        utils.close(dis,dos,client); //寫一個工具類,使用Closeable...可變參數    
    }
    public void run()
    {
        while(flag)
        {
            String msg=receive();
            send(msg);
        }
        release();

    }

}
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

崇礼县| 上虞市| 博兴县| 岑溪市| 翁牛特旗| 弥渡县| 伊金霍洛旗| 木兰县| 会理县| 清徐县| 济南市| 应城市| 垫江县| 称多县| 井陉县| 凌海市| 莲花县| 海安县| 广平县| 宣威市| 化州市| 常州市| 松原市| 泾阳县| 临泉县| 同仁县| 信宜市| 鲜城| 宁明县| 抚松县| 洱源县| 武清区| 河源市| 资讯| 胶州市| 比如县| 甘肃省| 秦皇岛市| 荣成市| 都江堰市| 广灵县|