socket简单通信

 服务器端:

public class ServerTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DataOutputStream dos = null;
        try {
            //创建端口
            ServerSocket ss = new ServerSocket(8888);
            //建立监听
            Socket s1 = ss.accept();
           //启用一个新的线程用来读信息
            new Message(s1).start();
           //使用数据输出流
            dos = new DataOutputStream(s1.getOutputStream());
            do {
                System.out.println("输入");
                String a = input.nextLine();
                dos.writeUTF(a);
            } while (true);
        } catch (IOException e) {
        } finally {
            try {
                dos.close();
            } catch (IOException e) {
                // TODO自动生成的 catch 块
                e.printStackTrace();
            }
        }
    }

    private static class Message extends Thread {
        Socket s1 = null;
        public Message(Socket socket) {
            this.s1 = socket;
        }
        public void run() {
            DataInputStream dis = null;
            try {
              //使用数据输入流
                dis = new DataInputStream(s1.getInputStream());
                do {
                    String str = dis.readUTF();
                    System.out.println("client   " + str);
                } while (true);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("关闭:"+s1.getRemoteSocketAddress());
            } finally {
                try {
                    dis.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

客户端:

public class ClientTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        DataOutputStream dos = null;
        try {
            Socket s1 = new Socket("127.0.0.1", 8888);
            new Message(s1).start();
            dos = new DataOutputStream(s1.getOutputStream());
            do {
                System.out.println("输入");
                String a = input.nextLine();
                dos.writeUTF(a);
            } while (true);
        } catch (IOException e) {
        } finally {
            try {
                dos.close();
            } catch (IOException e) {
            }
        }
    }
    private static class Message extends Thread {
        Socket s1 = null;
        public Message(Socket socket) {
            this.s1 = socket;
        }
        public void run() {
            DataInputStream dis = null;
            try {
                dis = new DataInputStream(s1.getInputStream());
                do {
                    String str = dis.readUTF();
                    System.out.println("server  " + str);
                } while (true);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    dis.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值