Get the App
SLTechnology News&Howtos  ›  Servers  › 

12.7-full Stack Java Notes: Java Network programming (5)

Shulou Source: shulou.com Published: 2022-06-02 21:55:30 09月20日 Update

Realization of UDP Communication

1.DatagramSocket: used to send or receive packet

When the server wants to send data to the client, it needs to generate a DatagramSocket object on the server side and a DatagramSocket object on the client side. The DatagramSocket on the server sends the DatagramPacket to the network and is then received by the DatagramSocket of the client.

DatagramSocket has two constructors. One does not require any parameters and is often used on the client side. The other requires a specified port, which is often used on the server.

Common methods: send, receive, close

2.DatagramPacket: the role of data containers (packets)

Common methods: constructor, getAddrress (get the IP address of the sending or receiving computer), getData (get the sent or received data), setData (set the sent data)

The basic steps of 3.UDP communication programming:

A) create the DatagramSocket of the client. When creating, define the listening port of the client.

B) create a server-side DatagramSocket. When creating, define the server-side listening port.

C) define a DatagramPacket object on the server side to encapsulate the packets to be sent.

D) the server sends out the packet

E) the client receives the packet

[example 1] one-way communication between client and server

Import java.net.DatagramPacket

Import java.net.DatagramSocket

Import java.net.InetSocketAddress

Public class Client {

Public static void main (String [] args) throws Exception {

Byte [] b = "aaaa" .getBytes ()

/ / must tell where the packet is to be sent

DatagramPacket dp = new DatagramPacket (blocalhost b.temperthjingnew InetSocketAddress ("localhost", 8999))

/ / I myself occupy port 9000 to send data packets to the outside machine.

DatagramSocket ds = new DatagramSocket (9000)

Ds.send (dp)

Ds.close ()

}

}

[example 2] Server side of one-way communication between client and server

Import java.net.DatagramPacket

Import java.net.DatagramSocket

Public class Server {

Public static void main (String [] args) throws Exception {

DatagramSocket ds = new DatagramSocket (8999)

Byte [] b = new byte [1024]

DatagramPacket dp = new DatagramPacket (b.cm.length)

Ds.receive (dp); / / blocking method

String string = new String (dp.getData (), 0dp.getLength ()); / / dp.getLength () returns the number of bytes of data actually received

System.out.println (string)

Ds.close ()

}

}

Basic type data can be passed through ByteArrayInputStream and ByteArrayOutputStream.

[example 3] client

Public class Client {

Public static void main (String [] args) throws Exception {

Long n = 2000L

ByteArrayOutputStream bos = new ByteArrayOutputStream ()

DataOutputStream dos = new DataOutputStream (bos)

Dos.writeLong (n)

Byte [] b = bos.toByteArray ()

/ / must tell where the packet is to be sent

DatagramPacket dp = new DatagramPacket (blocalhost b.temperthjingnew InetSocketAddress ("localhost", 8999))

/ / I myself occupy port 9000 to send data packets to the outside machine.

DatagramSocket ds = new DatagramSocket (9000)

Ds.send (dp)

Ds.close ()

}

}

[example 4] Server side

Public class Server {

Public static void main (String [] args) throws Exception {

DatagramSocket ds = new DatagramSocket (8999)

Byte [] b = new byte [1024]

DatagramPacket dp = new DatagramPacket (b.cm.length)

Ds.receive (dp); / / blocking method

ByteArrayInputStream bis = new ByteArrayInputStream (dp.getData ())

DataInputStream dis = new DataInputStream (bis)

System.out.println (dis.readLong ())

Ds.close ()

}

}

Objects can be passed through ByteArrayInputStream and ByteArrayOutputStream.

[example 5] Person class (Person class is required for both client and server)

Class Person implements Serializable {

Int age

String name

Public Person (int age, String name) {

Super ()

This.age = age

This.name = name

}

}

[example 6] client

Public class Client {

Public static void main (String [] args) throws Exception {

Person person = new Person (20, "aa")

ByteArrayOutputStream bos = new ByteArrayOutputStream ()

ObjectOutputStream oos = new ObjectOutputStream (bos)

Oos.writeObject (person)

Byte [] b = bos.toByteArray ()

/ / must tell where the packet is to be sent

DatagramPacket dp = new DatagramPacket (blocalhost b.temperthjingnew InetSocketAddress ("localhost", 8999))

/ / I myself occupy port 9000 to send data packets to the outside machine.

DatagramSocket ds = new DatagramSocket (9000)

Ds.send (dp)

Ds.close ()

}

}

[example 7] Server side

Public class Server {

Public static void main (String [] args) throws Exception {

DatagramSocket ds = new DatagramSocket (8999)

Byte [] b = new byte [1024]

DatagramPacket dp = new DatagramPacket (b.cm.length)

Ds.receive (dp); / / blocking method

ByteArrayInputStream bis = new ByteArrayInputStream (dp.getData ())

ObjectInputStream ois = new ObjectInputStream (bis)

Person person = (Person) ois.readObject ()

System.out.println (person.name)

Ds.close ()

}

}

"full Stack Java Notes" is a series of Java engineer notes that can help you grow from zero to one. The author, known as Mr. G, has 10 years of experience in Java research and development. He has been engaged in software design and development in a research and development center of China Digital and Aerospace Academy, and has gradually become an engineer, senior engineer and architect. Proficient in Java platform software development, proficient in JAVAEE, familiar with various popular development frameworks.

The notes include six parts from shallow to deep:

Getting started with A-Java

B-database from entry to proficiency

C-hand blade moving front end and Web front end

D-J2EE from knowledge to actual combat

E-Java high-level framework refinement

F-Linux and Hadoop

Tags: Data server service client client example port end method object note engineer machine communication blocking advanced function front end one-way engineering Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Docker Shulou Tech Info Apple