Introduction to Java socket programming [1]

Source: Internet
Author: User
Tags abstract format connection pooling interface reference socket domain
The first chapter of programming: Learning Tips





am I fit to study this tutorial




The
socket provides a mechanism for communicating between two computers, and is already famous before James Gosling notes the Java language. The Java language makes it easier and more efficient to use socket development without having to understand the underlying mechanism of the operating system. Most of the books that discuss Java coding do not cover this topic, or just "to the point", leaving the reader with plenty of room for imagination. This tutorial is about the knowledge you need to use the socket more effectively in your Java code. Contains the following:


What is a socket


where it is located in your application


The simplest socket routines that work--helps you understand the basics of Java sockets


Detailed analysis of the other two examples of how to use sockets in multithreaded and with connection pooling environments


briefly discusses a socket application in the real world





If you can describe how to use the classes in the java.net package, this tutorial may be a bit of a base for you. Although it's a good piece of information to improve your technology, it may be annoying if you've had a long socket development experience on your PC or other platform, but if you don't really understand the socket and just want to know what the socket is and How to apply the socket to your Java code ", this tutorial is a good start."





Chapter II: Socket base





Introduction





most programmers, whether they use Java programming or not, don't want to know too much about the underlying knowledge of how different computers communicate with each other. Programmers want to handle more table hacking badger bland olive Frog 3 sash m  sheath yu Silky 膉 Ava's structure, through the intuitive interface with the object interaction.


sockets exist in two areas--we prefer to process abstraction layers to avoid the underlying details, and the bottom details explained in this chapter are only for the knowledge you need to understand the abstract application.





Computer Group Network


There is a very simple way to operate and communicate between computers. A computer chip is a switch that stores and transmits data in 1 and 0来. When computers want to share data, all they need to do is transmit millions of streams of data back and forth at the same speed, in the same order, and at the same time. When you want to communicate between two applications, how do you want to consider these details?


in order to avoid the above mentioned things, we need a package of packages each time to do the same work. This will enable us to handle application-level work without having to worry about the specifics of the underlying network technology. This set of package protocols is called "stack." TCP/IP is the most common protocol stack. Most protocol stacks (including TCP/IP) follow the ISO's Open Systems Interconnection Reference Model (open Systems Interconnect Reference model,osirm) in general. OSIRM defines the computer network as 7 logical layers


(see below) Many companies have contributed to the construction of some layers of the model. From generating electronic signals () to providing data to the application. TCP/IP maps to two layers of the OSI model, as shown in the figure.





we don't have to delve too much into the details of these layers, but we should know where the socket is in the model.













Where does the
socket exist?

The
socket is located roughly at the session layer of the OSI model (the following figure), and the session layer is sandwiched between the layer on the application-oriented (top) and the real-time data communication layer (bottom). The session layer provides services for managing and controlling data flow between two computers. As part of this layer, the socket provides an abstraction that hides the complexity of acquiring bytes and bits from the transmission line. In other words, the socket allows us to let the application express that it wants to send bytes.

The
socket hides the specifics of completing the job.














when you pick up the phone and talk, the phone converts your voice to electrical signals. A telephone is an interface between a person and a telecommunications network. You don't need to know the details of how your voice is converted, just know who you want to contact. In the same way, the socket hides the complex transmission of binary data and plays a role as a high-level interface.





socket
to an application




When you write code using the socket, your code works on the presentation layer. The presentation layer provides a public information representation that allows the application tier to be used (representation of information). If you want to connect your application to the CC banking system, you can only use EBCDIC. Your domain object (Field objects) stores information in ASCII format. In that case, you have to be responsible for writing code at the presentation layer to convert the EBCDIC data into ASCII format, and then (for example) to provide a domain object (Field objects) to the application layer. The application tier can do whatever it wants with domain objects (domains objects).

















the socket processing code you write exists only in the presentation layer. The application layer does not need to know how the socket works.


What is a socket?





now we know the role of the socket, then the remaining question is: what is a socket? Bruce Eckel describes the socket in his book Java Programming thought:





socket is a software abstraction that is used to express the connection "terminal" between two machines. For a given connection, there is a socket on each machine, you can imagine a virtual "cable" working between two machines, "cable" inserted in two machines socket. Of course, the connection between the physical hardware and the "cable" between the two machines is unknown, and all abstract purposes are to keep us from needing to know more details.





simply put, a socket on one computer creates a communication channel with another computer, and programmers can use the channel to send data between the two machines. When you send the data, each layer of the TCP/IP stack adds the appropriate header to your data. The good news is that the Java language hides all these details, which is why they are sometimes called "streaming sockets."


think of the socket as a telephone handset on either end of the phone-you and I are talking and answering through a dedicated channel. The session will continue until we decide to hang up (unless we use a cellular phone), unless we hang up, our respective phone lines will be busy.


If you need to communicate between two machines without using advanced mechanisms like orbs (as well as CORBA, RMI, IIOP, etc.), the socket is better for you. The underlying mechanism of the socket is rather tricky. Fortunately, the Java platform gives us some simple but rather powerful high-level abstractions that make it easier to create and use sockets.




Type of
socket





generally speaking, Java sockets have the following two types:





TCP socket (implemented by the socket class, which we'll discuss in the following sections)


UDP socket (implemented by Datagramsocket Class)





TCP and UDP play the same role, but implementations are different. Both receive transport protocol packets and pass them to the presentation layer. TCP decomposes information into packets (datagrams) and reassemble them at the receiving end. It also requests for retransmission of lost packets. TCP reduces the worry on the top. UDP does not have the ability to assemble and retransmit requests. It simply transmits packets. The higher-level layers must ensure the integrity of the information and the correctness of the combination sequence.





in general, UDP makes your application more efficient, but only when your application does not communicate a large amount of data immediately and does not need to assemble a large number of datagrams to complete a message. Otherwise, TCP is the simplest and most effective choice.





because most readers prefer to use TCP in relation to UDP, we limit our discussion to the TCP class in the Java language.





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.