溫馨提示×

溫馨提示×

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

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

java 網(wǎng)絡(luò)編程-多人登錄面向?qū)ο?/h1>
發(fā)布時間:2020-08-29 20:05:02 來源:網(wǎng)絡(luò) 閱讀:231 作者:wx5d21d5e6e5ab1 欄目:編程語言

TCP:模擬登錄 :多個客戶端,先后等待

public class tcp {

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

    ServerSocket server=new ServerSocket(8888);
    boolean flag=true;

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

    }

}
static class channel implements Runnable{
    private Socket client;
    private DataInputStream dis; //輸入流
    DataOutputStream dos;   //輸出流
    public channel(Socket client)
    {
        this.client=client;
        try {
            dis=new DataInputStream(client.getInputStream());
            dos=new DataOutputStream(client.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            release();
        }
    }

    public void run()
    {

        String uname="";
        String password="";
        String data=receive();
        String[] datas=data.split("&");

        uname=datas[0];
        password=datas[1];

        if(uname.equals("杜雨龍")&&password.equals("你最帥"))
        {
            send("登陸成功");
        }else
        {
            send("登錄失敗");
        }

        release();
    }
//接收數(shù)據(jù)
private String receive()
{
    String data="";
    try {
        data = dis.readUTF();
    } catch (IOException e) {

        e.printStackTrace();
    }
    return data;
}

//發(fā)送數(shù)據(jù)
private void send(String msg)
{
    try {
        dos.writeUTF(msg);
        dos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
//釋放資源
private void release()
{
    try {
        if(null!=dis)
        {
        dis.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if(null!=dis)
        {
        dos.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if(null!=client)
        {
        client.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}
}

客戶端: 存儲文件

public class tcp2 {

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

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

    //發(fā)送
    new send(client).sendto();
    new receive(client).receiveto();

    client.close();

}
static class send{
    private Socket client;
    private DataOutputStream dos;
    private BufferedReader br;
    private String msg;
    public send(Socket client)
    {
        this.client=client;
        br=new BufferedReader(new InputStreamReader(System.in));
        msg=init();
        try {
            dos =new DataOutputStream(client.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void sendto()
    {
        try {
            dos.writeUTF(msg);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String init()
    {

        try {
            System.out.println("請輸入用戶名");
            String name=br.readLine();
            System.out.println("請輸入密碼");
            String password=br.readLine();
            return name+"&"+password;
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }
}

static class receive{
    private DataInputStream dis;
    private Socket client;
    public receive(Socket client)
    {
        this.client=client;
        DataInputStream dis=new DataInputStream(client.getInputStream());
    }

    public void receiveto()
    {
        String data;
        try {
            data = dis.readUTF();
            System.out.println(data);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
}

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI