• 网络编程—代码—TCP网络传输


    一、TCP:用字符流传输信息

    ------------------------------------------------------------------------------

    [用字符流的要点提示]

    1:

    将套接字关闭之后,字符流自然也就关闭了,所以只需要socket.shutdownOutput();/socket.shutdownInput();

    应为套接字开启的字节流封装在字符流Buffered里面,若果先关闭字符流bw.close(),将会关闭套接字。 抛出java.net.SocketException: Socket is closed异常,说明已经将套接字关闭。

    2:必须要写入换行符bw.newLine();(或者在内容末尾加 ),这样在用BufferedReader的readerLine() 接收数据包的时候才能读取到一个换行信息,否则会出现读取错误。

    ------------------------------------------------------------------------------

    public class Tcpc { // 客户端

     

    public static void main(String[] args) throwsUnknownHostException, IOException {

    //1.创建套接字指定主机和端口

    Socket socket = new Socket(InetAddress.getLocalHost(),8808);

    //2打开输入输出流

    InputStream is = socket.getInputStream();

    OutputStream os = socket.getOutputStream();

    //准备要撒送的数据

    String send = "我是客户端,请求登录!";

    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

    bw.write(send);

    bw.newLine();

    bw.flush();

    socket.shutdownOutput();

     

    //4.准备接受返回的信息,并用行读取

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String lin =br.readLine();

    System.out.println("客户端开始接受信息!");

    while(lin!=null){

    System.out.println(lin);

    lin=br.readLine();

    }

    //5关闭资源

    socket.shutdownInput();

     

    }

    }

    -------------------------------------------------------------------------

    public class Tcps { // 服务端

    public static void main(String[] args) throws IOException {

    //1创建套接字,到指定的端口接受信息

    ServerSocket serversocket = new ServerSocket(8808);

    //2监听客户端的消息

    Socket socket = serversocket.accept();

    //3.打开输入输出的流

    InputStream is = socket.getInputStream();

    OutputStream os = socket.getOutputStream();

    //4封装一个行读取的流

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String lin = br.readLine();

    while(lin!=null){

    System.out.println(lin);

    lin = br.readLine();

    }

    socket.shutdownInput();

    //5给客户端一反馈

    String replay="我是服务端,允许登录!";

    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));

    bw.write(replay);

    bw.newLine();

    bw.flush();

     

    //6关闭资源

     

    socket.shutdownOutput();

     

    }

    }

    ------------------------------------------------------------------------------

     

    二、TCP字节流传输信息

     

    TCP:字节流传输信息

    -------------------------------------------------------------------------------------

    public class Tcpc { //客户端

    public static void main(String[] args) throwsUnknownHostException, IOException {

    //1.创建套接字指定主机和端口

    Socket socket = new Socket(InetAddress.getLocalHost(),8808);

    //2打开输入输出流

    InputStream is = socket.getInputStream();

    OutputStream os = socket.getOutputStream();

    //准备要撒送的数据

    String send = "我是客户端,请求登录!";

    os.write(send.getBytes());

    socket.shutdownOutput();

     

    //4.准备接受返回的信息,并用行读取

    System.out.println("客户端接受完毕!");

    byte[] by = new byte[1024];

    is.read(by);

    System.out.println(new String(by,0,by.length));

    //5关闭资源

    socket.shutdownInput();

     

    }

    }

     

    -------------------------------------------------------------------------------------

    public class Tcps { //服务端

    public static void main(String[] args) throws IOException {

    //1创建套接字,到指定的端口接受信息

    ServerSocket serversocket = new ServerSocket(8808);

    //2监听客户端的消息

    Socket socket = serversocket.accept();

    //3.打开输入输出的流

    InputStream is = socket.getInputStream();

    OutputStream os = socket.getOutputStream();

    //4封装一个行读取的流

     

    System.out.println("服务端接收完毕!");

    byte[] by = new byte[1024];

    is.read(by);

    System.out.println(new String(by,0,by.length));

    socket.shutdownInput();

    //5给客户端一反馈

    String replay="我是服务端,允许登录!";

     

    System.out.println("服务端反馈完毕!");

    os.write(replay.getBytes());

    //6关闭资源

     

    socket.shutdownOutput();

     

    }

    }

     

  • 相关阅读:
    luogu P3239 [HNOI2015]亚瑟王
    android之软件键盘
    Eclipse输入智能提示设置
    防止反编译
    二进制数据读写
    数据类型转换
    类对象的读写文件
    Eclipse 快捷键
    修改IP
    Android eclipse 运行项目设置程序默认安装到SD卡
  • 原文地址:https://www.cnblogs.com/domi22/p/8046879.html
Copyright © 2020-2023  润新知