JAVA-network programming, java Network Programming pdf
Every time I mention the network, I feel like it's getting taller. But is network programming really mysterious? Next, let's review it.
Many people regard website programming as equivalent to network programming. Here, I want to correct it. network programming! = Website programming. What is network programming? Network programming is based on TCP and UDP, such as games and social products. For TCP communication, it is a programming with Q & A, while UDP is a communication similar to walkie talkie. The two have their own advantages. TCP is safer than UDP, UDP is faster than TCP transmission, and UDP communication is often used in game development.
I am not very clear about the deep differences between them. If you know, please feel free to talk about them.
The following is a simple example of network programming:
1. TCP-based simple network connection:
Server code:
Public class TestSeriver {/** server side * @ param args */public static void main (String [] args) {try {// There are 65526 ports on our machine, when we set the port, try to select the port after 124, because the port before 124 has been set by a specific program, such as: 80: browser address port ServerSocket ss = new ServerSocket (888); // here 888: the port number Socket s = ss set to facilitate client connection. accept (); // used to receive client connections. Note that this method is blocking. That is to say, if there is no client connection, the program will stay in this position. out. println ("A client connect! ");} Catch (IOException e) {e. printStackTrace ();}}}
Client code:
Public class TestClient {/** client * @ param args */public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 888 ); // here, "127.0.0.1": Make our host number, 888: the port number we need to connect} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}
Here, it is explained that this type of server-client connection can only connect to one client at a time without any data communication. How can we achieve multi-Client Connection and complete communication? Next, we will introduce a method:
Server code:
Public class TestSeriver1 {/** server side * @ param args */public static void main (String [] args) {try {ServerSocket ss = new ServerSocket (999 ); while (true) {Socket s = ss. accept (); System. out. println ("A client connect! "); InputStream is = s. getInputStream (); DataInputStream dis = new DataInputStream (is); System. out. println (dis. readUTF (); // The readUTF () method here is also blocking. To see this effect, I added a pause at the place entered by the client, already noted} catch (IOException e) {e. printStackTrace ();}}}
Client code:
Public class TestClient1 {/** client * @ param args */public static void main (String [] args) {try {Socket s = new Socket ("127.0.0.1", 999 ); outputStream OS = s. getOutputStream (); DataOutputStream dos = new DataOutputStream (OS); Thread. sleep (3000); // set the pause for three seconds. I have a description of dos in the thread blog. writeUTF ("Hello Server! ");} Catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
I have added some knowledge about JAVA-I/O Stream. If you don't understand it, I gave a detailed introduction to JAVA-I/O Stream in my blog yesterday, you can go over the network first.
Statement: if any of the above content is inappropriate, please leave a message. Thank you.
How to Learn java Network Programming
Java is oriented to j2ee.
J2ee is more than just socket programming, including the core technology in section 13.
Core APIs and components of J2EE
The J2EE platform consists of a complete set of Services, application interfaces (APIs), and Protocols. It provides Function Support for developing Web-based multi-layer applications, the following is a brief description of the 13 technical specifications in J2EE (limited by space ):
1. JDBC (Java Database Connectivity ):
JDBC APIs provide a unified way to access different databases. Like ODBC, JDBC shields developers from some details. In addition, JDCB's access to the database is also platform-independent.
2. JNDI (Java Name and Directory Interface ):
The jndi api is used to run the name and directory services. It provides a consistent model to access and operate enterprise-level resources such as DNS and LDAP, local file systems, or objects on application servers.
3. EJB (Enterprise JavaBean ):
One of the reasons why J2EE technology has won widespread media attention is EJB. They provide a framework for developing and implementing distributed business logic, significantly simplifying the development of scalable and highly complex enterprise-level applications. The EJB specification defines when and how the EJB component interacts with their containers. Containers are responsible for providing public services, such as directory services, transaction management, security, resource buffer pools, and fault tolerance. However, it is worth noting that EJB is not the only way to implement J2EE. Due to the openness of J2EE, some vendors can achieve the same purpose in parallel with EJB.
4. RMI (Remote Method Invoke ):
As its name indicates, the RMI protocol calls methods on remote objects. It uses serialization to transmit data on the client and server. RMI is a more underlying protocol used by EJB.
5. Java IDL/CORBA:
With the support of Java IDL, developers can integrate Java and CORBA. They can create Java objects and expand them in corba orb, or they can also create Java classes and serve as the customers of the CORBA objects expanded with other ORB. The latter method provides another way through which Java can be used to integrate your new applications with old systems.
6. JSP (Java Server Pages ):
A JSP page consists of HTML code and Java code embedded in it. The server processes the Java code after the page is requested by the client, and then returns the generated HTML page to the browser of the client.
7. Java Servlet:
Servlet is a small Java program that extends the functions of Web servers. As a server-side application, it is executed when requested, which is similar to CGI Perl script. Most of the functions provided by Servlet are similar to those provided by JSP, but the implementation methods are different. JSP usually embeds a small amount of Java code in most HTML code, while servlets is all written in Java and generates HTML.
8. XML (Extensible Markup Language ):
XML is a language that can be used to define other Markup languages. It is used to share data in different business processes.
The development of XML and Java are mutually independent. However, the same objective of XML and Java is platform independence. By combining Java and XML, you can get a perfect platform-independent solution.
9. JMS (Java Message Service ):
MS is an application interface (API) used to communicate with message-oriented middleware ). It supports point-to-point domains and supports Publishing.
Java Network Programming program Problems
P. add ("South", p); this line of code is incorrect and p cannot be added to itself.