socket数据流直接通信方式DataInputsteam,Dataoutputsteam以及Bufferreader和BufferWriter缓冲区方式

说明: 注释内为数据流直接写入读出方式,真正实现的是Buffer缓冲区类

此段为服务器端:


try{
    ServerSocket socket=new ServerSocket(6666);
           while(true){
               Socket socket1=socket.accept();


              /* OutputStream os=socket1.getOutputStream();
               *DataOutputStream is=new DataOutputStream(os);
               *is.writeInt(3);
               *is.close();
               *System.out.print("客户端得到数据");**/
               BufferedReader bus=new BufferedReader(new InputStreamReader(socket1.getInputStream()));
               String ss=bus.readLine();
               System.out.println(ss);
            }}catch(IOException e){e.printStackTrace();}

2.此段为客户端

  try {
            Socket socket = new Socket("192.168.43.10", 6666);
            /**InputStream in = socket.getInputStream();
            DataInputStream os = new DataInputStream(in);
            int x = os.readInt();
            System.out.print(x);*/
            BufferedWriter bu=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            String str="nihao";
            bu.write(str);
            bu.newLine();
            bu.flush();




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

服务器端循环接收。

你可能感兴趣的:(socket数据流直接通信方式DataInputsteam,Dataoutputsteam以及Bufferreader和BufferWriter缓冲区方式)