Java–UDP communication and TCP communication

 IPAddress and port number

Port numbers are represented by two bytes (16-bit binary numbers) and range from 0 to 65535, where the port numbers between 0 and 1023 are used for some well-known network services and applications.

A user’s normal application needs to use port numbers above 1024 to avoid being occupied by another application or service.

     InetAddress

common method

Code demo:

Copy code ></span></div>
<pre>1     public static void main(String[] args) throws UnknownHostException {
2         //InetAddress inet=InetAddress.getLocalHost();
3         //Host name +ip address4 InetAddress inet=InetAddress.getByName (
Copy code ></span></div>
</div>
<h1>UDPSignal communication</h1>
<h1><span lang=DatagramPacket

   DatagramSocket

 UDPNetwork program

 

 Code demo:

Copy code ></span></div>
<pre> 1 //Sending end2 public class UDPSend {3 public static void main (String[] args) throws IOException {Four//1. packaging5 / / clear data6 byte[] bytes=
Copy code ></span></div>
</div>
<div class=
Copy code ></span></div>
<pre> 1 //receiving end2 public class UDPReceive {3 public static void main (String[] args) throws IOException {4 / / clear port number5 DatagramSocket ds=new DatagramSocket (8888);6 / / create byte array for receiving data7 byte[] bytes=new byte[1024];8 / / create received packets9 DatagramPacket dp=new DatagrAmPacket (bytes, bytes.length);10 / / reception11 ds.receive (DP);12 / / get the data on the receiving packet.ThirteenInt length=dp.getLength ();14 String ip=dp.getAddress ().GetHostAddress ();FifteenInt port=dp.getPort ();16 System. out. println (
Copy code ></span></div>
</div>
<h1>   TCPSignal communication</h1>
<p><strong>One is the ServerSocket class, which is used to represent the server side, the other is the Socket class, which is used to represent the client.</strong></p>
<h2><span lang=ServerSocket

   Socket

Method declaration

Functional description

int getPort()

This method returns a int type object, which is the port number of the Socket object connected to the server side.

InetAddress getLocalAddress()

This method is used to get the local IP address bound by the Socket object and encapsulate the IP address as an object return of type InetAddress

void close()

This method is used to close the Socket connection and end this communication. Before closing the socket, all input / output streams associated with the socket should be shut down, because a good program should release all resources at the end of execution

InputStream getInputStream()

This method returns an input stream object of type InputStream, which, if returned by a server-side Socket, is used to read the data sent by the client, and vice versa, to read the data sent by the server-side.

OutputStream getOutputStream()

This method returns an OutputStream-type output stream object that, if returned by a server-side Socket, is used to send data to the client, and vice versa, to send data to the server.

Graphic:

Code demo:

Copy code ></span></div>
<pre> 1 //Sending end2 public class UDPSend {3 public static void main (String[] args) throws IOException {Four//1. packaging5 / / clear data6 Scanner sc=new Scanner (System.in);7 / / clear destination IPaddress8 InetAddress inet=InetAddress.getByName (
Copy code ></span></div>
</div>
<div class=
Copy code ></span></div>
<pre> 1 //receiving end2 public class UDPReceive {3 public static void main (String[] args) throws IOException {4 / / clear port number5 DatagramSocket ds=new DatagramSocket (8888);6 / / create byte array for receiving data7 byte[] bytes=new byte[1024];8 / / create received packets9 while (true) {TenDatagramPacket dp=new DatagramPacket (bytes, bytes.length);11 / / reception12 ds.recEive (DP);13 / / get the data on the receiving packet.14 int length=dp.getLength (); / / explicitly sent byte lengthFifteenString ip=dp.getAddress ().GetHostAddress ();16 int port=dp.getPort ();SeventeenSystem.out.println (
Copy code ></span></div>
</div>
<h2>  </h2>
<div class=
Copy code ></span></div>
<pre> 1 //Server-side2 public class TCPServer {3 public static void main (String[] args) throws IOException {4 / / create server socket5 ServerSocket server=new ServerSocket (7777);6 / / call accept methodCreate links with clients7 Socket socket=server.accept ();8 InputStream in=socket.getInputStream ();9 byte[] bytes=new byte[1024];10 int len=in.read (bytes);11 System.out.pRintln (New String (bytes, 0, len));12 / / server reply to client13 OutputStream out=socket.getOutputStream ();14 out.write (
Copy code ></span></div>
</div>
<div class=
Copy code ></span></div>
<pre> 1 //Client2 public class TCPCLient {3 public static void main (String[] args) throws IOException {4 //1. create Socket object, connect to server5 Socket socket=new Socket (
Copy code ></span></div>
</div>
<p> </p>
<h2>File upload cases</h2>
<p><img src=

Code demo:

Copy code ></span></div>
<pre> 1 public class TCPServer {
 2     public static void main(String[] args) throws IOException {
 3         ServerSocket server=new ServerSocket(5555);
 4         Socket socket=server.accept();
 5         //Clear data sources6 InputStream in=socket.getInputStream ();7 / / clear destination8 File file=newFile (
Copy code ></span></div>
</div>
<div class=
Copy code ></span></div>
<pre> 1 public class TCPClinet {
 2     public static void main(String[] args) throws IOException {
 3         Socket socket=new Socket(
Copy code ></span></div>
</div>
<h2>  File upload case multithreaded version</h2>
<p><img src=

Code demo:

Copy code ></span></div>
<pre>1 public class Demo {
2     public static void main(String[] args) throws IOException {
3         ServerSocket server=new ServerSocket(6000);
4         while(true){
5             Socket socket=server.accept();
6             new Thread(new Upload(socket)).start();
7         }    
8     }
9 }</pre>
<div class=Copy code ></span></div>
</div>
<div class=
Copy code ></span></div>
<pre> 1 public class Upload implements Runnable{
 2     private Socket socket;
 3     public Upload(Socket socket){
 4         this.socket=socket;
 5     }
 6     public void run() {
 7         //Clear data sources8 FileOutputStream fos=null;9 try {10 InputStream in= socket.gEtInputStream ();11 / / clear destination12 File file=new File (
Copy code ></span></div>
</div>
</div>
	</div><!-- .entry-content -->

	<footer class= Posted on Categories Java

Leave a Reply

Your email address will not be published. Required fields are marked *