书小宅java——socket通信

客户端代码

package com.hwadee.shiyan;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

class RecvMsgThread extends Thread {
    private DataInputStream dis;

    public RecvMsgThread(Socket client) {
        try {
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                String content = dis.readUTF();
                System.out.println(content);
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }
    }
}

public class Client {
    public static void main(String[] args) {
        try {
            Socket client = new Socket("172.16.60.5", 1177);
            DataOutputStream dos = new DataOutputStream(client.getOutputStream());
            Scanner sc = new Scanner(System.in);
            new RecvMsgThread(client).start();
            while (true) {
                String content = sc.nextLine();
                dos.writeUTF(content);
                //System.out.println("写给server:"+content);
                if(content.equals("quit")) {
                    break;
                }
            }
            dos.close();
            client.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务器

package com.hwadee.shiyan;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

class ClientThread extends Thread {
    public static Map<String, ClientThread> clients = new HashMap<>();

    private DataOutputStream dos;
    private DataInputStream dis;

    boolean loginedUser=false;

    public ClientThread(Socket client) {
        try {
            dos = new DataOutputStream(client.getOutputStream());
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            while (true) {
                //login:aa
                //chat:aa:bb:hello
                String content = dis.readUTF();
                String[] params = content.split(":");
                switch (params[0]) {
                    case "login":
                        if(loginedUser==true){
                            clients.remove(params[1]);
                            dos.writeUTF("老的账户已失效");
                        }
                        clients.put(params[1], this);
                        loginedUser=true;
                        break;
                    case "chat":
                        ClientThread ct = clients.get(params[2]);
                        ClientThread lg = clients.get(params[1]);
                        if(lg==null) {
                            dos.writeUTF("您还未登录");
                        }
                        else if(ct==null){
                            dos.writeUTF("您的好友已下线");
                        }
                        else if(ct!=null&&lg!=null){
                            ct.dos.writeUTF(content);
                        }
                        break;
                    case "group":
                        Set<String> cc = clients.keySet();
                        String client="";
                        for(String name:cc) {
                            ct = clients.get(name);
                            if(ct!=this)
                                ct.dos.writeUTF(params[1]+":"+params[2]);
                        }
                        break;
                    default:
                        break;
                }

                if(content.equals("quit")) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 删除退出用户
            Set<String> cc = clients.keySet();
            String client="";
            for(String name:cc) {
                if(clients.get(name)==this) {
                    client=name;
                    break;
                }
            }
            clients.remove(client);

            try {
                dis.close();
                dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Server {
    public static void main(String[] args) {
        ServerSocket server = null;
        try {
            server = new ServerSocket(1177);
            while (true) {
                Socket client = server.accept();

                new ClientThread(client).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                server.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值