Use the java.net package to establish two-way communication

Source: Internet
Author: User

ExploitationJavaThe network capability provided by the. NET package. Take the connected stream communication method as an example,

The operations on the server are generally:

(1) create a serversocket object to listen to requests sent from the client on the specified port.

(2) When receiving a request, the accept () method returns a socket object.

(3) Use the preceding socket object to create input and output stream objects.

(4) interaction with customers through input and output streams.

When the listener completes interaction, the input and output streams and the socket are closed.

Butler serviceProgramThe operation ends. Disable serversocket.

ImplementationCodeCode class:

Try {

Boolean flag = true;
Socket clientsocket = NULL;

Serversocket = new serversocket (0 );
System. Out. println ("server listen on:" + serversocket. getlocalport ());

While (FLAG ){
Clientsocket = serversocket. Accept ();
Datainputstream is = new datainputstream (New bufferedinputstream (Client
Socket. getinputstream ()));
Printstream OS = new printstream (New bufferedoutputstream (clientsocket.
Getoutputstream ()));
// Process the applet request
OS. Close ();
Is. Close ();
Clientsocket. Close ();
}
Serversocket. Close ();
} Catch (ioexception e ){
System. Err. println ("exception:" + E );
}

The operations on the client are as follows:

(1) create a socket object to establish a connection with the server.

(2) Use this socket object to create input and output streams.

(3) interact with the server.

(4) After interaction, close the input and output streams and socket.

The implementation code class is as follows:

Try {
Socket clientsocket = new socket ("servername", 7 );
Outputstream OS = clientsocket. getoutputstream ();
Datainputstream is = new datainputstream (clientsocket. getinputstream ())
;
// Other operations.
OS. Close ();
Is. Close ();
Clientsocket. Close ();
} Catch (exception e ){
System. Err. println ("exception:" + E );
}

This method only relies on standard Java Network support and does not require additional software packages or tools,
Therefore, it is quite concise and flexible, and easy to implement some special needs.

From: http://www.javah.net/wangluobiancheng/20070605/2330.html

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.