Java Learning Notes (11)--Network communication

Source: Internet
Author: User
first, the topic 1. What is the TCP/IP protocol. What is the characteristic of it.

TCP/IP protocol is a stable and efficient network communication protocol based on connection. It has a stable and efficient, sending and receiving information clear characteristics.
TCP/IP (Transmission Control Protocol/Internetwork Protocol) is the primary protocol of the Internet, which defines the rules used by computers and peripherals for communication (application layer, Transport layer, network layer, link layer). The TCP/IP protocol has the characteristics of free openness, wide application and high reliability. 2. Brief description of the difference between URL and socket communication.

The URL communication is to use the identifier to express the identified address in order to realize the communication between the two sides, while the socket communication is the communication between the IP and the port number to identify both sides of the communication, so as to achieve communication between the two sides to identify and confirm.
URL function is more advanced, through the URL Java program can directly send or read data on the network.
Socket is the most common way of traditional web programs, can be imagined as two different programs through the network communication channel. The specific differences are shown as follows:
(1) Communication mode
Socket on the server to run the communication program, Non-stop listening to the client's connection please be able to make the communication request, waiting for the client requests service when the client requests, immediately connect and communication;
When using a URL to communicate, a CGI program resides on the server side, but it is always dormant. It is activated only when a client requires a connection, and then communicates with the user. So when the URL is communicating, passively waits for the client's request.
(2) Number of connections
The socket server can communicate with multiple clients at the same time
URL communication means that the server can only communicate with one client. 3. Briefly describe the socket communication mechanism, how the client connects to the server.

Socket communication mechanism is the server monitor, client request, connection confirmation and data transmission, release connection.
The process of establishing a connection is as follows:
Server-side generates a ServerSocket instance object that listens for client connection requests at any time
The client generates a socket instance object and issues a connection request
After the server side receives a request from the client via the Accept () method, it opens up an interface to connect to it and generates the required I/O data stream
Communications are performed through a pair of InputStream () and OutputStream (). When the communication is over, turn off the corresponding socket interface separately
4. Description and attempt to read a text file continuously from the server through a URL and display the contents of the text file

Package xiti11;
Import java.net.*;
Import java.io.*;
public class Xiti11_4 {public
static void Main (String args[]) throws exception{
    url url = new URL ("https://www.ba Idu.com/?tn=90066238_hao_pg ");//url
    URLConnection URLC = url.openconnection ();//Create connection
    //Get server-side byte stream and read
    BufferedReader in = new BufferedReader (New InputStreamReader (Urlc.getinputstream ()));
    String inputline;//while
    ((Inputline=in.readline ())!=null) {
        System.out.println (inputline);//Output
    }
    In.close ()//Close File connection
}

}

5. Write the program, using the socket communication mechanism between the server and the client transfer files.

Server:

Package xiti11;

NETWORK_3,C/S the communication server side, receives the string and displays
Import java.net.*;
Import java.io.*;

public class Xiti11_5_server {

public static void Main (string[] args) throws IOException {

ServerSocket serversocket = null;
PrintWriter out = null;
BufferedReader in = null;

try {
  serversocket = new ServerSocket;
} catch (IOException e) {
   System.err.println ("Could not listen on p ort:80. ");
   System.exit (1);
}

Socket clientsocket = null;

try {
    clientsocket = serversocket.accept ();
    out = new PrintWriter (Clientsocket.getoutputstream (), true);
    in = new BufferedReader (New InputStreamReader (Clientsocket.getinputstream ()));
} catch (IOException e) {
    System.err.println ("Accept failed.");
    System.exit (1);
}


String Fromuser;
int strlen;

while (true) {
    fromuser= in.readline ();
    Strlen=fromuser.length ();
    Out.println ("received string ' s length is:" + strlen);
    System.out.println ("server:received string ' s length is:" + strlen);
    if (Fromuser.equalsignorecase ("Bye")) break
      ;

Out.close ();
In.close ();
Clientsocket.close ();
Serversocket.close ();
  }

Client:

Package xiti11;

NETWORK_3,C/S communication client, sending string
Import java.io.*;
Import java.net.*;

public class Xiti11_5_client {
public static void Main (string[] args) throws IOException {

    Socket echosocket = null;
    PrintWriter out = null;

    BufferedReader in = null;
        try {echosocket = new Socket ("127.0.0.1", 80);
        out = new PrintWriter (Echosocket.getoutputstream (), true);
    in = new BufferedReader (New InputStreamReader (Echosocket.getinputstream ()));
        catch (Unknownhostexception e) {System.err.println ("Don t know about Host:taranis.");
    System.exit (1); catch (IOException e) {System.err.println ("couldn" t get I/O for "+" the connection
        To:taranis. ");
    System.exit (1);
BufferedReader stdIn = new BufferedReader (new InputStreamReader (system.in));

String Userinput;
        while (true) {userinput = Stdin.readline ();
    Out.println (Userinput);

        System.out.println ("Echo:" + in.readline ());
if (Userinput.equalsignorecase ("Bye")) break;
} out.close ();
In.close (); Stdin.cLose ();
 Echosocket.close ();
 }
}

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.