JAVA基础(TCP协议)

1,TCP协议

【1】客户端(Android)

  • 创建Socket连接服务端(指定ip地址,端口号)通过ip地址找对应的服务器

  • 调用Socket的getInputStream()和getOutputStream()方法获取和服务端相连的IO流

  • 输入流可以读取服务端输出流写出的数据

  • 输出流可以写出数据到服务端的输入流

 

【2】服务端(JAVAEE)

  • 创建ServerSocket(需要指定端口号)

  • 调用ServerSocket的accept()方法接收一个客户端请求,得到一个Socket

  • 调用Socket的getInputStream()和getOutputStream()方法获取和客户端相连的IO流

  • 输入流可以读取客户端输出流写出的数据

  • 输出流可以写出数据到客户端的输入流

 

客户端

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.Socket;

import java.net.UnknownHostException;

public class Demo1_Client {

    /**

     * * 1.客户端

        * 创建Socket连接服务端(指定ip地址,端口号)通过ip地址找对应的服务器

        * 调用Socket的getInputStream()和getOutputStream()方法获取和服务端相连的IO流

        * 输入流可以读取服务端输出流写出的数据

        * 输出流可以写出数据到服务端的输入流

     * @throws IOException

     * @throws UnknownHostException

     */

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

        Socket socket = new Socket("127.0.0.1", 12345);

        

        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();

    }





}

 

服务器

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.ServerSocket;

import java.net.Socket;

public class Demo1_Server {

    /**

     * * 2.服务端

    * 创建ServerSocket(需要指定端口号)

    * 调用ServerSocket的accept()方法接收一个客户端请求,得到一个Socket

    * 调用Socket的getInputStream()和getOutputStream()方法获取和客户端相连的IO流

    * 输入流可以读取客户端输出流写出的数据

    * 输出流可以写出数据到客户端的输入流

     * @throws IOException

     */

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

        ServerSocket server = new ServerSocket(12345);

        

        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));           //将数据转换成字符串并打印

        

        socket.close();

    }

}





2,TCP协议代码优化,

  • 客户端

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.PrintStream;

import java.net.Socket;

import java.net.UnknownHostException;

public class Demo2_Client {

    /**

     * * 1.客户端

        * 创建Socket连接服务端(指定ip地址,端口号)通过ip地址找对应的服务器

        * 调用Socket的getInputStream()和getOutputStream()方法获取和服务端相连的IO流

        * 输入流可以读取服务端输出流写出的数据

        * 输出流可以写出数据到服务端的输入流

     * @throws IOException

     * @throws UnknownHostException

     */

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

        Socket socket = new Socket("127.0.0.1", 12345);

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));        //将字节流包装成了字符流

        PrintStream ps = new PrintStream(socket.getOutputStream());                    //PrintStream中有写出换行的方法

        

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

        ps.println("我想上天");

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

        ps.println("大哭!!!能不能给次机会");

        

        socket.close();

    }

}
  • 服务端

public class Demo2_Server {





    /**

     * * 2.服务端

    * 创建ServerSocket(需要指定端口号)

    * 调用ServerSocket的accept()方法接收一个客户端请求,得到一个Socket

    * 调用Socket的getInputStream()和getOutputStream()方法获取和客户端相连的IO流

    * 输入流可以读取客户端输出流写出的数据

    * 输出流可以写出数据到客户端的输入流

     * @throws IOException

     */

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

        //demo1();

        ServerSocket server = new ServerSocket(12345);

        

        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());                    //PrintStream中有写出换行的方法

                        

                        ps.println("欢迎上天");

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

                        ps.println("不好意思,爆满了");

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

                        socket.close();

                    } catch (IOException e) {

                        

                        e.printStackTrace();

                    }

                }

            }.start();

        }

    }





    public static void demo1() throws IOException {

        ServerSocket server = new ServerSocket(12345);

        

        Socket socket = server.accept();                        //接受客户端的请求

        

        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));        //将字节流包装成了字符流

        PrintStream ps = new PrintStream(socket.getOutputStream());                    //PrintStream中有写出换行的方法

        

        ps.println("欢迎上天");

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

        ps.println("不好意思,爆满了");

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

        socket.close();

    }





}

 

 

 

     

 

 

 

 

 

 

你可能感兴趣的:(JAVA基础,JAVA基础)