Java implementation of the most basic socket network communication

Source: Internet
Author: User

First, the basis of network communication

There are many communication entities in the network, and each communication entity has an identifier that is the IP address.

In reality, each network entity can communicate with multiple communication programs at the same time, which requires the use of port number to differentiate.

Second, basic network support in Java

1, the IP address uses the InetAddress class to represent.

The two ways to get a inetaddress instance are:

(1) Getbyname (String host) gets the corresponding InetAddress object based on the host

(2) getbyaddress (byte[] addr) Get InetAddress object based on IP address

2, InetAddress provides three methods to obtain the corresponding IP address and host name of the InetAddress instance

(1) String Getcanonicalhostname () gets the domain name of the permission for this IP address

(2) String gethostaddress () gets the IP address corresponding to the InetAddress instance

(3) String gethostname () gets the host name of this IP address

In addition, the InetAddress class uses the Getlocalhost () method to obtain a inetaddress instance of the native IP address, using the Isreachable () method to test whether the address can be reached.

Third, Java implementation of simple TCP/IP communication

The server side uses ServerSocket to create a TCP server, listens with accept (), and returns a socket corresponding to the client if the client request is received, otherwise it is in a waiting state.

The client connects to the server based on the IP of the server.

Server code:

Importjava.net.*;ImportJava.io.*;/**mobile phone side code as the server, get its own IP address and display for the client to connect*/ Public classphone_server{ Public Static voidMain (string[] args)throwsIOException {//Print the IP address of this machineInetAddress address=Inetaddress.getlocalhost (); System.out.println ("The IP address of this machine is" +address.gethostaddress ()); //Create a ServerSocket to listen for connection requests from the client socketServerSocket SS =NewServerSocket (30000); //using loops to continuously accept requests from clients         while(true)        {            //whenever a request to the client socket is received, the server side also generates a socketSocket s =ss.accept (); //wraps the output stream corresponding to the socket into a printstreamPrintStream PS =NewPrintStream (S.getoutputstream ()); //perform normal IO operationsPs.println ("Hello, you have received a New year greeting from the server!" "); //turn off the output stream and close the socketPs.close ();        S.close (); }    }}

Client code:

/**PC-side code PC as client, connect server based on server IP address and port number*/Importjava.net.*;ImportJava.io.*; Public classpc_client{ Public Static voidMain (string[] args)throwsIOException {//SOCKET socket = new Socket ("127.0.0.1", 30000);Socket socket =NewSocket ("192.168.47.1", 30000);//Here's the IP address to fill out the IP address of the mobile server//wraps the input stream corresponding to the socket into a bufferedreaderBufferedReader br =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //perform normal IO operationsString line =Br.readline (); System.out.println ("Data from the server:" +Line ); //close the input stream, socketBr.close ();    Socket.close (); }}

Java implementation of the most basic socket network communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.