Getting started with Java Socket [1]

xiaoxiao2021-03-06  77

This tutorial is provided by IBM DevelopWorks, copyright belongs to IBM

Originally: ROY MILLER translation: robinh00d [cstc]

The purpose of this article is just to practice, this tutorial copyright belongs to IBM, I don't have copyright

Chapter 1: Learning Tips

Do I fit this tutorial?

Socket provides a mechanism for communicating between two computers, which is already famous before James Gosling Note Java language. Java language makes you simpler and efficient use of Socket development without having to understand the underlying mechanism of the operating system. Most discussion of Java-coded books do not involve this topic, or just "point until", leave a large number of imagination spaces for readers. This tutorial is to let you know how to use the knowledge you need to use Socket more effect in your Java code. Contains the following:

• What is Socket

• What are its places in your application?

• The simplest Socket routine that can run properly - help you understand the basics of Java Socket

• Detailed differentials to explore how to use the use of sockets in multi-threading and connecting pool environments

• Summary discussing a Socket app in the real world

If you can describe how to use the classes in this package in Java.Net, this tutorial may be a bit based on you. Although it is a good information to improve technology, if you have had a long-term Socket development experience on the PC or other platform, this part of the prompt may make you bored, but if you are not very understanding Socket, and only If "What is Socket", "How to apply Socket App to" in your own Java code ", this tutorial is a good start.

Chapter II: Socket Foundation

Introduction

Most programmers, whether or not to use Java language programming, do not want to know too much underground knowledge about the mutual communication between different computers. The programmer wants to handle the processing of the table.

Socket exists in two areas - we would rather handle the abstract layer and avoid the underlying detail. The underlying detail explained by this chapter is only used for knowledge necessary for abstract applications.

Computer network

The computer is operated and communicated in a very simple way. The computer chip is a switch that stores and transmits data from 1 and 0. When the computer wants to share data, they need to be transmitted back to millions of data streams in the same speed, the same order, the same time, and the like. How do you want to consider these details when you want to communicate between two applications?

In order to avoid things mentioned above, we need a set of bag agreements to complete the work. This will enable us to handle the application level work without having to worry about the details of the underlying network technology. The package protocol thus set is referred to as "stack". TCP / IP is the most common protocol stack. Most protocol stacks (including TCP / IP) are roughly followed by ISO International Standardization Open System Interconnect Model (OSIRM). OSIRM defines computer networks as 7 logical layers

(See the figure below) has contributed to many companies that have contributed to certain layers of this model. Provide data from the generated electronic signal () to the application. TCP / IP is mapped to two layers of the OSI model, as shown.

We don't have to deepen the details of these layers, but we should know what the Socket is in the model.

What places are Socket exists?

Socket is substantially located in the session layer (below) of the OSI model, and the session layer is sandwiched between the dedicated high-level (upper) layer and real-time data communication layer (below). The session layer provides services to manage and control data streams between two computers. As part of this layer, Socket provides an abstraction hidden from the transfer line to get the complexity of BYTES and BITS. In other words, Socket allows us to let the app indicate that it wants to send Bytes. Socket hides the specific details of this work.

When you pick up the phone, call your sound to convert your voice. The telephone is an interface between people and telecommunications networks. You don't have to know how your voice conversion, just know if you want to contact. The same reason, Socket hides complex transmission binary data to play a role of high-level interfaces.

Exposure to an application Socket

When you write code with Socket, your code works in the representation layer. The representation provides a public information that allows the application layer to represent (Reresentation of Information). If you want to connect your application to the CC bank system and you can only use EBCDIC. Domain Objects store information with ASCII format. In this case, you have to be responsible for converting EBCDIC's data into an ASCII format in the representation of the layer writing code, and then (for example) provides a domain object (Domain Objects). Application layers can be done by domain objects (Domain Objects).

The Socket processing code you wrote exists only in the representation. The application layer does not need to know how the Socket works.

What is Socket?

Now we know the role of Socket playing, then the remaining problem is: What is Socket? Bruce Eckel in his "Java Programming Thought" book is described in Socket:

Socket is an abstraction for connecting "terminal" between two machines. For a given connection, there is a socket on each machine, you can imagine a virtual "cable" work between two machines, "cable" inserted on the Socket of two machines. Of course, "cables" between physical hardware and two machines are unknown, all of the abstract objectives are to let us not have to know more details.

Simply put, the Socket on a computer creates a communication channel with another computer, and the programmer can use this channel to send data between the two machines. When you send data, each of the TCP / IP protocol stack adds an appropriate header to your data. There is a good news is that the Java language hides all these details, which is why they are sometimes called "stream socket".

Think about the Socket like any end of the phone like a telephone receiver - you and I have a special channel to call and answer. The session will continue until we decide to hang up the phone (unless we use a cellular phone) unless we hang up the phone, our respective telephone lines will take up.

If you need to communicate between two machines without using advanced mechanisms like ORBS (and CORBA, RMI, IIOP, etc.), Socket is more suitable for you. The underlying mechanism of Socket is quite tricky. Fortunately, the Java platform gives us some simple but quite powerful high-level abstractions so that we create and use Socket easier.

Socket type

In general, Java Socket has two types:

• TCP Socket (implemented by the Socket class, the following chapter we will discuss it)

• UDP socket (implementation by DataGramsocket)

TCP and UDP play the same role, but implementation is different. Both receive transport protocol packets and pass them to represent layers. TCP breaks down information into a data package (DataGrams) and recommpting the receiver. It also reloads the lost packets. TCP reduces the worry of the upper layer. UDP does not have the function of assembling and retransmitting the request. It just transmits the packet. The higher layer layer must ensure the integrity of the information and the correctness of the combined order. In general, UDP makes your application more efficient, but only when your application is not intensing with a lot of data, and does not need to assemble a lot of datagram. Otherwise, TCP is the simplest and most effective choice.

Because most readers prefer to use TCP relative to UDP, we qualify our discussion to TCP classes in the Java language.

转载请注明原文地址:https://www.9cbs.com/read-93841.html

New Post(0)