No connection Communication UDP
Client
Packagecom.swift.test;Importjava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;ImportJava.util.Scanner;/*Write a Java program using UDP communication. Required: Gets the contents of the keyboard input. Sent to the server. After the server is received. Print the received content on the console client 1. Create a keyboard entry object. Get keyboard entry Data 2. Create a Datagramsocket object 3. Create a DA The Tagrampacket object. and encapsulates the data entered by the keypad 4. Send data 5. Release the stream resource service Side 1. Create Datagramsocket object 2. Create Datagrampacket object 3. Receive data stored to Datagrampack Et object 4. Gets the contents of the Datagrampacket Object 5. Print received content on the console 6. Releasing Streaming resources*/ Public classdemoudpclient { Public Static voidMain (string[] args)throwsIOException {Scanner scan=NewScanner (system.in); System.out.println ("Please enter what you want to convey:"); String Str=Scan.nextline (); Datagramsocket Datagramsocket=NewDatagramsocket (); byte[] buf=str.getbytes (); Datagrampacket Datagrampacket=NewDatagrampacket (Buf,buf.length,inetaddress.getbyname ("192.168.105.22"), 6464); Datagramsocket.send (Datagrampacket); Datagramsocket.close (); }}
Service side
Packagecom.swift.test;Importjava.io.IOException;ImportJava.net.DatagramPacket;ImportJava.net.DatagramSocket;Importjava.net.InetAddress;/*Write a Java program using UDP communication. Required: Gets the contents of the keyboard input. Sent to the server. After the server is received. Print the received content on the console client 1. Create a keyboard entry object. Get keyboard entry Data 2. Create a Datagramsocket object 3. Create a DA The Tagrampacket object. and encapsulates the data entered by the keypad 4. Send data 5. Release the stream resource service Side 1. Create Datagramsocket object 2. Create Datagrampacket object 3. Receive data stored to Datagrampack Et object 4. Gets the contents of the Datagrampacket Object 5. Print received content on the console 6. Releasing Streaming resources*/ Public classDemoudpserver { Public Static voidMain (string[] args)throwsIOException {datagramsocket datagramsocket=NewDatagramsocket (6464); InetAddress IP=Inetaddress.getlocalhost (); String ipstring=ip.gethostaddress (); String namestring=Ip.gethostname (); System.out.println (ipstring+" "+namestring); byte[] buf=New byte[1024]; Datagrampacket Datagrampacket=NewDatagrampacket (buf,1024); Datagramsocket.receive (Datagrampacket); System.out.println (NewString (BUF)); Datagramsocket.close (); }}
Results
There is a connection to the communication TCP
Client
Packagecom.swift.test;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.InetAddress;ImportJava.net.Socket;/*using the socket to write a Java program, requires: The client sends the content "I am the client" to the server, after the server receives, print the received content on the console, and return the content "I come from the server" to the client, the client receives the service side sends the content to print in the console ? Client 1. Create the client socket Object 2. Gets the output stream object of the socket 3. Write data to server 4. Gets the input stream object of the socket 5. Use input stream, read feedback 6. Close Streaming Resources*/ Public classdemotcpclient { Public Static voidMain (string[] args)throwsIOException {Socket s=NewSocket (Inetaddress.getbyname ("10.254.3.116"), 8888); OutputStream out=S.getoutputstream (); byte[] buf= "I am the client". GetBytes (); Out.write (BUF); InputStream in=S.getinputstream (); byte[] bytes=New byte[128]; In.read (bytes); System.out.println (NewString (bytes));
S.close (); }}
Service side
Packagecom.swift.test;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.InetAddress;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*using the socket to write a Java program, requires: The client sends the content "I am the client" to the server, after the server receives, print the received content on the console, and return the content "I come from the server" to the client, the client receives the service side sends the content to print in the console ? server Side 1. Create server-Side ServerSocket object, specify server-side port number 2. Open the server, waiting for the client socket object connection, if there is a client connection, return the client socket object 3. Gets the client's input through the client's socket object Stream, in order to achieve the acquisition of data sent by the client 4. Gets the data in the stream by the client's input stream 5. Through the client's socket object, gets the client's output stream, in order to implement the feedback information to the client 6. Write data to stream 7 through the client's output stream. Close Streaming Resources*/ Public classDemotcpserver { Public Static voidMain (string[] args)throwsIOException {serversocket serversocket=NewServerSocket (8888); InetAddress IP=Inetaddress.getlocalhost (); System.out.println (Ip.gethostaddress ()); Socket s=serversocket.accept (); InputStream in=S.getinputstream (); byte[] buf=New byte[128]; In.read (BUF); System.out.println (NewString (BUF)); OutputStream out=S.getoutputstream (); Out.write ("I'm from the server.". GetBytes ());
Serversocket.close ();
S.close ();
}}
Java Base UDP communication