Simple usage of serversocket and socket in Java

Source: Internet
Author: User

1. Differences between socket and serversocket
① In Java Network programming, the two are easy to mix. Serversocket is used to bind a port. It must be used by the write server. Socket is a common connection socket used to establish a network connection. It is used by the client and also by the server.
② Socket also becomes a "socket" used to describe the IP address and port.
③ After the connection between the client and the server is successful, a socket instance is generated at both ends.

2.java.net. Socket there are three common methods
① Accept method, used to generate blocking. Connect directly to a connection and return the socket object instance of a client. Blocking means that the program is temporarily stopped until the session is received. Generally, "blocking" is generated by loops.
② Getinputstream obtains the network connection input and returns an inputsteram object instance.
③ The other end of the getoutputstream connection will get the input and return an outputstream object instance.


PS: getinputstream and getoutputstream generate an ioexception and must be captured because the stream objects they return are usually used by another stream object.

3. Principles of server-client model development
Server:
Use serversocket to specify the listening port (generally more than 1024), and wait for the client to connect. Once the connection is successful, a session is generated. After the session ends, the session is closed.

Client:
Use SOCKET to send a connection request to a port of a server on the network. If the connection is successful, the session is opened. After the session is complete, the socket is closed.

① Serversocket class
Create a serversocker class and create a listening service at the specified port, for example:

ServerSocket lisenter = new ServerSocket(4000);

The specified listening port is 4000. To listen to possible client requests at any time, run the following statement:

Socket so = listener.accept();

At this time, the program will be blocked until it captures a request from the client and returns a socket object so used to communicate with the client. After that, the server only needs to read and write data to so to enable session with the client.

② Socket object
When the client needs to obtain information from the server, it should create a socket object:

Socket socket = new Socket("ServerName",4000);

The socket constructor has two parameters: the first parameter is the name of the host to be connected, and the second parameter is the port of the server. Then, you can use getinputstream () and getoutputstream () to communicate with each other.

PS: serversocket is generally used only to set the port number and listener. The socket of the server and the client is used for communication between the two.

4. if you create a socket object, you can use the getinputstream () method to obtain information from the service program, or call the getoutputstream () method to obtain the output stream to send messages.

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.