Using Java to write a network communication program

Source: Internet
Author: User
Tags join socket

This paper describes the application of the Java programming language to write a network communication program based on the client/server model, and discusses the socket mechanism, input and output stream, and program implementation code.

Keywords JAVA, network, Socket,applet

The system structure on the network is the client/server mode, the server is responsible for the storage, maintenance, management and transmission of data and image, the client is responsible for the operation of Man-machine interface, sending out the demand and displaying the recovered data.

Here's how to use Java for network programming:

1 because the client through IE with the server to establish contact, so the client use applet, server-side use application;

2 The server should be set up into multiple threads to answer requests from multiple customers;

3 two ends of communication using the socket mechanism.

Input/output flow concepts in Java:

In addition to FilterInputStream and Filteroutputstream, the filtering flow DataInputStream and DataOutputStream respectively Implement Interface Datainput and DataOutput. The methods defined in interface Datainput mainly include reading basic types of data from the stream, reading a row of data, or reading a specified length of bytes, such as Readboolean () readInt (), ReadLine (), readfully (), and so on. The method defined in interface DataOutput is primarily to write basic types of data into a stream or to write a byte array of a length, such as Writechar (), writedouble () DataInputStream can read the machine-independent basic type data from the connected input stream to implement an input mode that is separate from the concrete platform; DataInputStream can write basic types of data to the connected output stream.

Socket mechanism

Socket is designed for client/server model, two programs on the network realize the exchange of data through a two-way communication connection, one end of this two-way link is called a socket. The socket is typically used to implement a connection between the client and the service party. The client can write a request to the socket, the server processes the request, and then returns the result to the user via the socket.

The socket communication mechanism provides two modes of communication: There is a connection and no connection, respectively, for different application requirements. When using a join method, the communication link provides a reliable, Full-duplex byte throttling service. In this way, the two sides of the communication must create a join process and establish a communication link, the subsequent network communication operation is entirely between the pair of processes, communication has finished closing the join process. When using a no Join method, its system overhead is smaller than that of no join, but the communication link provides unreliable datagram services and does not guarantee that the data transmitted by the source will reach the host. In this way, the communication parties do not have to create a join process and establish a communication link, network communication operations between different hosts and processes forward.

Related Article

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.