Java Network programming

Source: Internet
Author: User
Tags socket connect

First declare: This article reprinted from: @ Aloof Wolf https://home.cnblogs.com/u/xdp-gacl/First, the network basic concept

First, clear a concept: Network programming! = website Programming, network programming is now generally referred to as TCP/IP programming.

  

Second, network communication protocol and interface

  

Third, the idea of layered communication protocols

  

Iv. Reference Model

  

Five, IP protocol

  

Each person's computer has a unique IP address so that it does not pass the wrong message when communicating with each other.

The IP address is divided into four segments with one point, and the IP address in the computer is represented by four bytes, one byte represents a paragraph, and each byte represents a maximum of only 255.

Vi. TCP protocol and UDP protocol

  

TCP and UDP are located on the same level, and are built on the IP layer. Because there are different IP addresses between the two computers, the two computers can be separated, and they can talk to each other. Calls generally have two ways of calling: The first is TCP, the second is UDP. TCP is a reliable connection,TCP is like a phone call, need to get through the phone, waiting for the other party to have a response to continue to talk to each other, that is, it is necessary to confirm that the message will be sent out after the message. TCP upload anything is reliable, as long as two machines to establish a connection, the data sent on this machine will certainly be able to reach the other side of the machine, UDP is like a telegram, sent to the finished, the other party has not received it all regardless, so UDP is unreliable. Although TCP transmits data reliably, but transmits slowly, the UDP transmits the data to be unreliable, but transmits fast .

Seven, socket programming

General network programming is called socket programming, socket in English means "socket".

  

Both computers are installed with a socket, and then plugged into two computer sockets using both ends of a line, so that the two computers are connected. This socket is the socket.

Because each other can communicate with each other, I said you are my server just logically, I should send things to you first, and then by you to deal with, forward. So you call the server. However, technically speaking, only TCP will be divided between server and client. For UDP, in the strict sense, there is no so-called server and client. The socket for the TCP server is called the socket of the serversocket,client.

When two computers are connected to each other, you must first know their IP address, but it is not enough to provide an IP address, and you must have a connected port number, which is the application to which you want to connect.

  The port number is used to differentiate between different applications on a single machine . The port number is accounted for 2 bytes inside the computer. There is a maximum of 65,536 port numbers on a single machine. An application can occupy multiple port numbers. Port number if it is used by an application, other applications will no longer be able to use the port number. Remember, we write a program to occupy the port number, the port number of more than 1024, the port number below 1024 do not occupy, because the system may be requisitioned at any time. The port number itself is divided into TCP port and UDP port, TCP 8888 port and UDP 8888 port is completely different two ports. There are 65,536 TCP ports and UDP ports.

Eight, TCP socket communication model

  

Nine, Socket use example

Server-Side ServerSocket

 1 Import java.net.*; 2 Import java.io.*; 3 public class testserversocket{4 public static void Main (String args[]) throws exception{5 Serv Ersocket ss = new ServerSocket (6666);                 6/* When you create a ServerSocket object, you often give it a port number 7 specifies the port number, which means that the new ServerSocket object will use 8                 is which port number, which port number to listen to the client's connection 9 so specifying a port number is meant to tell the computer where ServerSocket object 10 listens to the client's connection */11 /* The server-side receive client connection request is uninterrupted, so the server-side 12 programming is generally a dead loop, never stop running. */13 while (true) {Socket s = ss.accept (); 15/* In service                             The Accept () method accepts the client's connection object, and the Accept () method is a 161 blocking method that has been innocently waiting for a client to request a connection up to 17                             The socket socket on the server is then connected to the client socket socket and the */18/* client can connect to the server side, depending on whether the server accepts the client's connection request 19 If you accept a client connection request, install the last socket socket on the server side 20 through this socket and connect the client can establish a connection, mutualCommunication of */21 System.out.println ("A Client connected!"); 22/* Use the InputStream stream to receive information sent from the client, using the DataInputStream data stream to process the received information */23 DATAINPUTSTR                             EAM dis = new DataInputStream (S.getinputstream ()); 24/* Use readUTF (method to read all the received information, stored in the variable str 25 The readUTF () method is also a blocking method that innocently waits for the client to send a message and then reads the received information out 26 if the client does not write something over it                             has been on the server side innocently waiting, until the client write something up so far 27 plug-in method efficiency is often not high, such as a client connection up, but it is slow to send information, 28 Then the server side of the program is blocked, so that another client can not connect up, because another client to connect 29 on the server side, you have to call the Accept () method on the server side, can accept () method                             Must be in the next cycle to be able to be called by 30, now the server-side program to call readUTF () This method is blocked, it waits for the connected up to the 31 The client sends a message to read the information, if the client has not sent information to the server side, then readUTF () method 32 has been unable to read the information, then the server side of the program will be blocked here, the next loop, so               Other clients are 33 unable to connect to the server side */34              String str = DIS.READUTF (); System.out.println (str); 36}37 }38}

Client socket

1 Import java.net.*; 2 Import java.io.*; 3 public class testclientsocket{4 public     static void Main (String args[]) throws exception{5         Socket s = new Socke T ("127.0.0.1", 6666); 6         /*client request to connect to server side *         /7/* After the server side of the connection, you can output information to the server side and receive information returned from the server 8         output information and receive return information to use the flow of input and output principle for information processing * /9/         * Here is the output stream outputstream to the server-side outputs information */10         outputstream OS = S.getoutputstream ();         DataOutputStream dos = New DataOutputStream (OS),         Thread.Sleep (30000),/* Client sleeps 30 seconds before sending information to the server */13         dos.writeutf ("Hello server!");     }15}

The client requests the connection through port 6666 to the server side, after the server accepts the client's connection request, installs a socket on the server side, and then lets the socket connect with the client, so that the server can communicate with the client. When another client requests a connection, the server accepts it and then installs another socket to connect to the client's socket.

Java Network programming

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.