JAVA SOCKET Technology

Source: Internet
Author: User

Java socket technology is divided into server-side socket:serversocket and client Socket:socket

ServerSocket and Socket Communication mode:

Service side:

1, ServerSocket listening port, call accept () to create a socket, and wait for the client's connection

2. Get the socket InputStream and read the data sent from the client.

Client:

1. Create a socket for the specified IP, port, and get the OutputStream output stream of the socket

2. Write data to the output stream, then flush () to send the data

Because the socket is two-way communication, all ServerSocket can read the client's data, can also get outputstream write data to the client, the client can also get InputStream read the server sent over the data.

Note: 1, accept () is blocked, InputStream is blocked. 2, write data after the need to flush ();

Now introduce a more practical question: How does the server and the client handle the data that is received?

The idea is: the server, the client after receiving the data to judge, for the specific character is to call the appropriate method, so that can handle a lot of logical business.

Also end the socket by passing data to the server or client-specific characters to tell the wait to end reading the data.

There is also a practical problem, how to handle the relationship between multiple clients and a server?

Here a server is referred to as an IP and port, in order to handle the traffic in the server to deal with multiple clients can actually create multiple sockets to correspond processing.

In order to be able to process the client's request asynchronously, after the server has created the socket into the thread to run, so that the implementation of asynchronous processing, processing method in the thread of the Run method is called.

The socket setting timed out and timed out to interrupt read data blocking.

Client.setsotimeout (10*1000);

Socket encoding Problem:

The input stream encoding of the client is consistent with the output stream encoding of the service side

The output stream encoding of the client is consistent with the input stream encoding of the service side

Attached to a specific URL: http://haohaoxuexi.iteye.com/blog/1979837#comments

JAVA SOCKET Technology

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.