Java, TCP Communication Program

I. Overview

  TCP communication enables data exchange between the two ends of the two computers, communications, should be strictly divided into client (Client) and server (Server).

  Ends of the communication step:

    1, the server program, you need to start in advance, waiting for client connections;

    2, client actively connected to the server, the connection is successful to communicate. The server can not take the initiative to connect the client.

  In Java, it provides two classes to implement TCP communication program:

    1. Client : java.net.Socket class represents. Socket object created, a connection request sent to the server, the server responds to the request to establish a connection both to start communication.

    2, the server : java.net.ServerSocket class represents. Create a ServerSocket object, which is equivalent to open a service, and waits for the client connection.

Two, Socket class

  Socket class: This class implements client sockets, socket refers to the endpoint of communication between the two devices.

  1, construction method

public Socket (String host, int port): create socket object and connects it to the specified port number on the specified host. If the specified host is null, the equivalent of the specified address as the loopback address.

  Tips: loopback address (127.xxx) is the local loopback address (Loopback Address), mainly for inter-network software testing as well as local interprocess communication, no matter what the program, by using the loopback address to send data, return immediately, without any network transmission .

  2, members of the method

public InputStream getInputStream (): returns the input stream for this socket.
    •    If the communication has an associated Socket, all operations generated InputStream also associated with this channel.
    •    Close-generated InputStream will close the associated Socket.
public OutputStream getOutputStream (): Returns the socket output stream
    •    If the channel has an associated Socket, all operations of the generated OutputStream also associated with the channel.
    •    Close-generated OutputStream will close the associated Socket.
public void close (): Closes this socket
    •  Once a Socket is closed, it can not be reused.
    •   Close this Socket will close the associated InputStream and OutputStream.
public void shutdownOutput (): disable socket output stream
    •   Any previously written data to be transmitted, followed by terminating the output stream.

Three, ServerSocket class

  ServerSocket class: This class implements server socket, the object is waiting for a request over the network.

  1, construction method

public ServerSocket (int port): With this construction method when creating ServerSocket object, can be bound to a specific port number, the port number is a port parameter.

  Demo:

ServerSocket server = new ServerSocket(6666);

  2, members of the method

public Socket accept (): listens for and accepts the connection, returns a new Socket object, and for clients communicate. This method will block until the connection is established.

  

Fourth, the simple TCP network program

  Analysis illustrates TCP communication

    1, [server] start creating ServerSocket object, waiting for a connection.

   2, [client] start, create Socket object, requesting a connection.

   3, [server] receiving connector, accept method is called, and returns a Socket object

   4, [client] Socket object, get OutputStream, write data to the server

   5, [server] Socket object, get InputStream, reads the data sent by the client.

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11546166.html