TCP communication -C / S and the Socket ServerSocket

Client class: Socket class


  • The client TCP communication: connection request sent to the server, transmits data to the server, the server reads the data, two streams IO

  • java.lang.Object
    successor java.net.Socket

  • Construction method:

    1. Socket (String host, int port)
      Creates a stream socket and connects it to the specified port number on the specified host.
      Parameters:
      String Host: server host name / IP address
      int port: port number of the server
  • Member method:
    1. OutputStream getOutputStream ()
      Returns the output stream for this socket.
    2. InputStream getInputStream ()
      Returns the input stream for this socket.
    3. void close ()
      Closes the socket
  • note:
    1. When the client and server-side interaction must be used to provide network flow Socket too, you can not use the stream object you create yourself
    2. When we create a client Socket object, it will use the TCP protocol to establish a connection with the server path, if the server does not start, it will throw an exception.

Server class: ServerSocket class


  • Server-side TCP communication: to accept the request of the client, the client sends the data to read, to write client-side data back twice IO streams, has been in a wait state

  • java.lang.Object
    successor java.net.ServerSocket

  • Construction method:
    1. ServerSocket (int port)
      Create a server socket on a specified port, the operating system is not set randomly assigned, can not communicate.
  • Member method
    1. Socket accept ()
      listens for and accepts the connection to this socket.
  • Note: the server must clear one thing, you must know which server is requested by the client, so you can use the accept () method to get to the requesting client object Socket


package cn.learn.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class client {
    public static void main(String[] args) throws IOException {
        //1.创建客户端对象Socket,构造方法绑定服务器IP地址和端口号
        Socket socket = new Socket("127.0.0.1",8020);
        //2.使用Socket对象中的方法getOutputStream()获取网络字节输出流OutputStream对象
        OutputStream outputStream = socket.getOutputStream();
        //3.使用流中的write方法给服务器发送数据,需要转换成字节数组
        outputStream.write("服务器你好".getBytes());

        //4.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
        InputStream clientIn = socket.getInputStream();
        //5.使用InputStream对象中的read()方法,读取服务器回写的数据
        byte[] bytes = new byte[1024];
        int len =clientIn.read(bytes);
        //打印看看
        System.out.println(new String(bytes,0,len));

        //6.释放资源,只关闭Socket的IO流就行
        socket.close();

    }
}

package cn.learn.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

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

        //1.设置通信端口号,不然系统随机分配
        ServerSocket server = new ServerSocket(8020);

        //2.使用serverSocket对象中的方法accept,获取到请求的客户端对象Socket(含地址和端口号)
        Socket socket1 = server.accept();

        //3.使用Socket对象中的方法getInputStream()获取网络字节输入流InputStream对象
        InputStream serveIn = socket1.getInputStream();

        //4.使用serveIn的方法read,读取客户端发送的数据
        byte[] bytes = new byte[1024];
        //获取读取的数据有效长度
        int len = serveIn.read(bytes);
        //打印看看
        System.out.println(new String(bytes,0,len));

        //5.使用Socket对象中的方法getOutputStream()获取网络字节输入流OutputStream对象
        OutputStream serverOut = socket1.getOutputStream();

        //6.使用serverOut中的write方法回写给客户端
        serverOut.write("我收到了".getBytes());

        //7.释放socket1与server的流
        server.close();
        socket1.close();
    }
}

Guess you like

Origin www.cnblogs.com/huxiaobai/p/11610095.html