142.TCP-练习1__

package com.itheima.scoketdemo7;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss=new ServerSocket(10000);
        Socket accept = ss.accept();
        InputStream inputStream = accept.getInputStream();
        int b;
        while ((b=inputStream.read())!=-1){
            System.out.println((char) b);
        }
        System.out.println("你看我执行了吗?");
       /* OutputStream outputStream = accept.getOutputStream();
        outputStream.write("你谁啊?".getBytes());*/
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bw.write("你谁啊?");
        bw.newLine();
        bw.flush();
        bw.close();
        inputStream.close();
        accept.close();
        ss.close();
    }
}
package com.itheima.scoketdemo7;

import java.io.*;
import java.net.Socket;

public class ClientDemo {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("127.0.0.1",10000);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello".getBytes());
        socket.shutdownOutput();//仅仅关闭输出流,并写一个结束标记,对socket没有任何影响

        /*InputStream inputStream = socket.getInputStream();
        int b;
        while ((b=inputStream.read())!=-1){
            System.out.println((char) b);
        }*/
        BufferedReader br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();
        //inputStream.close();
        outputStream.close();
        socket.close();
    }
}

你可能感兴趣的:(JAVAWeb,udp,java,网络协议,开发语言,后端)