Network communication

Network communication three elements:

IP Address: InetAddress

  Identifying the network device, easy to remember, the host name is available, the current 32-bit IPV4, has been exhausted, a 128-bit IPV6

  Public addresses can be used to direct Internet access, network address, is a local area network

The port number:

  Identifies the logical address, the process used to identify the different processes

Transfer Protocol

  Communications regulation, common agreement:

  TCP, three-way handshake to complete the connection, the protocol is reliable, efficient end-point, high security

  The UDP, the data package, the package, without establishing a connection, is only responsible for sending each packet in the 64K, high efficiency, but unsafe

  

InetAddress class - on behalf of ip address

Static methods:

getAddress-- return to the original ip address

getAllByName (String host (hostname)) - Array obtain an IP address consisting of

getByName (String host / ip address) - Get the IP address of the target

getLocalHost-- get local hostname

Object methods:

getHostAddress-- get host ip address

getHostName-- get hostname

 

Transmitting data using the UDP protocol

  Creating the sender Socket object

    DatagramSocket () - randomly assigned port numbers

    DatagramSocket (int port) - determine the port number

    DatagramSocket ds = new DatagramSocket();

  And create a data package

    DatagramPacket-- indicates packet

    String s = "hello";

    byte[] bys = s.getBytes();

    int length = bys.length;

    InetAddress address = InetAddress.getByName("主机名");

    int port = 8888;     

    DatagramPacket dp = new DatagramPacket(bys ,length,address,port);

  send data

    ds.send(dp);

  Release resources

    ds.close();

UDP protocol to accept data

  Create a Socket object receiving end

  DatagramSocket ds = new DatagramSocket (8888); the need to specify the port receiving end

  Accept data packets

  byte[] bys = new byte[1024];

  DatagramPacket dp = new DatagramPacket(bys,bys.length);

  ds.receive (dp); // blocked - waiting for data to come

  Resolution Packet

  InetAddress address = dp.getAddress();

  byte[] data =dp.getData();

  int length = dp.getLength();

  sysout(address.getHostAddress());

  sysout (new String (data, 0, length)); - received much data on how many accepted

  Release resources

   dp.close();

 

Send and receive data using UDP protocol Notes:

  Port number error, data can be sent normal, not abnormal, but not receive data

  Address already in user: Can not bind-- port number is occupied

 

Send data using the TCP protocol:

  // Create the sender Socket object (create a connection: ip, port)

  Socket s = new Socket (InetAddress.getByName ( "host name"), a port number);

  // Get the output stream object

  OutputStream os = s.getOutpuStream();

  //send data

  String str = "hello";

  os.write(str.getBytes());

  // release resources

  s.close();

 

Using the TCP protocol to accept data

  // Create the receiving end Socket object (create a connection: ip, port)

  ServerSocket ss = new ServerSocket (port);

  // Listen (blocking)

  Socket s = ss.accept();

  // Get the input stream object

  InputStream is = s.getInputStream();

  //retrieve data

  byte[] bys = new byte[1024];

  int len = is.read(bys);

  //Output Data

  sysout ( "client ->" + s.getInetAddress () getHostName ().); // output sent from the host name 

  sysout(new String(bys,0,len));

  // release resources

  s.close();

 

 

Connection refused: connect-- connection failure, was rejected

 

 

 

 

Guess you like

Origin www.cnblogs.com/dajingshao/p/11993393.html