java socket programming for client and server to receive information

java socket programming for client and server to receive information

#Learning Notes/Programming/socket

tcp/IP Four-Layer Model

  1. Link Layer: Data Physical Layer, Optical Fiber, Network Cable, etc.
  2. Network layer: Used to group transmitted data and assign it to the appropriate computer or port
  3. Transport Layer: Communication between networks
  4. Application layer: applications

Common Network Communication Protocols

Both sides of the network must abide by it for data exchange

UDP:

  1. Data sources and destinations are encapsulated in data packages
  2. Packet size <64k
  3. Poor security, fast speed, no connection oriented, easy to lose packets, for instant data transmission, such as video, audio;

TCP

It is safe to form a channel after three handshakes and then transfer data.

Three-time handshake

  1. Client sends connection request, waiting for server confirmation
  2. Server sends connection response to client
  3. The client sends the connection confirmation to the server and the connection is formed.

Network knowledge

  1. IPV4:32 Binary Number
  2. IPV6:128-bit binary number, divided into eight groups of hexadecimal
  3. Port number: The address occupied by the process, consisting of two bytes (0-65535)
    The operating system assigns a random port number to network software/or network software requests a fixed port number.(Ports prior to 1024 are not available)
    Common ports: 80 (network port), 3306 (mysql), 1521 (oracle), 8080 (Tomcat)

TCP Communication Program Flow

Before communication

  1. Server listens for requests
  2. Client active connection, connection contains IO objects, one data interaction uses four IO stream objects

The following is an example of client-side and server-side outgoing messages

Client

  1. Create client object, bind server IP address and port number Socket (String host, int port);
  2. Get network byte output stream object using getOutputStream() in client object
  3. Send data to server using write method of network byte output stream
  4. Get Network Byte Input Stream Object
  5. Read the data returned by the server using the read method
  6. Release resources;

Implementation Code

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

public class TCPClient {
    public static void main(String args[]) {
        try {
            Socket s1=new Socket("127.0.0.1",8888);//Create Client Object
            OutputStream out1=s1.getOutputStream();
            byte[] bytes="232323".getBytes();
            out1.write(bytes);
            System.out.println("Client sent out"+new String(bytes));
            InputStream i1=s1.getInputStream();
            byte[] bytes2=new byte[1024];
            int len=i1.read(bytes2);
            System.out.println("Client accepts"+new String(bytes2,0,len));
            s1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Server

  1. Create server object, port number
  2. Use accpet() in the server object; get the requested client object
  3. Get network byte input stream object using getINputStream() of client object
  4. Getting data using read method of network byte input stream
  5. Get Network Byte Output Stream Object
  6. Use write method to return data
  7. Release resources (Socket,ServerSocket);

code implementation

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

public class TCPServer {
    public  static void main(String[] args){
        ServerSocket s1 =null;
        try {
            s1= new ServerSocket(8888);
            Socket s2 = s1.accept();
            InputStream IN1=s2.getInputStream();
            byte[] bytes=new byte[1024];
            int len =IN1.read(bytes);
            System.out.println("Server Side Received+"+new String(bytes,0,len));
            byte[] bytes1="6666".getBytes();
            OutputStream out1=s2.getOutputStream();
            out1.write(bytes1);
            System.out.println("Server Send"+new String(bytes1));
            s2.close();
            s1.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

Some other api

InetAddress class

  1. getLocalhost(); returns the host address (string)
  2. getLocalName(); returns host name
  3. getByName(String host); determines the ip address of the host given its host name.

Tags: network Java socket Programming

Posted on Thu, 04 Jun 2020 12:45:15 -0700 by anf.etienne