java网络编程客户端,服务器端

  1. 服务器端test1
        ServerSocket server = new ServerSocket(6060);
        Socket socket = server.accept();
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();

        os.write("周红伟是个可爱的姑娘!".getBytes());
        byte arr[] = new byte[1024];
        int len = is.read(arr);
        System.out.println(new String(arr,0,len));
  1. 客户端test1
        Socket socket = new Socket("127.0.0.1",6060);
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        byte arr[] = new byte[1024];
        int len = is.read(arr);
        System.out.println(new String(arr,0,len));

        os.write("顾小磊也是个可爱的人儿!".getBytes());
        socket.close();
  1. Demo2_Server
        ServerSocket server = new ServerSocket(6060);
        while (true){
            final Socket socket = server.accept();
            new Thread(){
                public void run(){
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        PrintStream ps = new PrintStream(socket.getOutputStream());
                        ps.println("zhouxiaowei shi ge da 坏蛋!");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();

        }
  1. Demo2_Client
    Socket socket = new Socket("127.0.0.1",6060);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //BufferedWriter bw =new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        PrintStream ps = new PrintStream(socket.getOutputStream());

        System.out.println(br.readLine());


        socket.close();

你可能感兴趣的:(java网络编程客户端,服务器端)