25th Day Notes

Source: Internet
Author: User

Dark Horse programmer <a href= "http://www.itheima.com" target= "blank" >java training </a>

25th Day Notes


1. Network Programming (overview of network programming)
A: Computer network
Refers to the different geographical location of multiple computers with independent functions and their external devices, through the communication lines connected, in the network operating system, network management software and network communication Protocol management and coordination, to achieve the sharing of resources and information transmission computer system.
B: Network Programming
Is the data exchange between programs running on different computers that are used to implement network interconnection.
2. Network Programming (IP overview of three elements of network programming)
Unique identification of each device in the network
Each network terminal has a separate address in the network, and we use this address to transmit data over the network.
A.ipconfig: View Native IP192.168.17.30
B.ping: Test Connection 192.168.40.62

C. Local Loop Address: 127.0.0.1 255.255.255.255 is a broadcast address
D.pv4:4 of bytes, 4 of 0-255. About 4.2 billion, 3 billion are in North America, Asia 400 million. At the beginning of 2011 was exhausted.

3. Network programming (Overview of the port number of the three elements of network programming)

A. Unique identification of each program on the device

B. Each network program needs to bind a port number, when transmitting data in addition to determine which machine to send, but also to the specific program to send.

C. Port number range from 0-65535

D. Write a network application will need to bind a port number, try to use more than 1024, 1024 or less is basically used by the system program.

E. Common ports
mysql:3306
oracle:1521
Web:80
tomcat:8080
qq:4000
feiq:2425

4. Network programming (three-factor protocol for Network programming)
A collection of rules, standards, or conventions established for data exchange in a computer network.
Udp
For no connection, data is unsafe and fast. The client and server are not differentiated.

Tcp
Connection-oriented (three-time handshake), data security, slightly lower speed. Divided into client and server.
Three-time handshake: The client initiates a request to the server, the server responds to the request, transmits the data
5. Network programming (socket communication schematic diagram)
A:socket Sockets Overview:
An IP address and port number that is uniquely identified on the network can be combined to form a uniquely identifiable identifier socket.
Sockets are available at both ends of the communication.
Network communication is actually the communication between sockets.
Data is transmitted via IO stream between two sockets.
The socket is created in the application and establishes a relationship with the driver through a binding mechanism, telling itself the corresponding IP and port.
6. Network programming (UDP transmission)

1. Sending send
Create Datagramsocket, random port number
Create Datagrampacket, specify data, length, address, port
Send Datagrampacket using Datagramsocket
Close Datagramsocket

2. Receiving receive
Create Datagramsocket, specify port number
Create Datagrampacket, specify array, length
Receive Datagrampacket using Datagramsocket
Close Datagramsocket
Get data from Datagrampacket

3. Receiver gets IP and port number
String IP =packet.getaddress (). gethostaddress ();
int port =packet.getport ();

7. Network programming (UDP transmission Optimization)

Receive-end receive Datagramsocket socket=NewDatagramsocket (6666);//creating a socket is equivalent to creating a dockDatagrampacket packet =NewDatagrampacket (New byte[1024], 1024);//creating a packet is equivalent to creating a container                 while(true) {socket.receive (packet); //Receiving goods            byte[] arr =Packet.getdata (); intLen =packet.getlength (); String IP=packet.getaddress (). gethostaddress (); SYSTEM.OUT.PRINTLN (IP+ ":" +NewString (arr,0, Len)); } send-side send Datagramsocket socket=NewDatagramsocket ();//creating a socket is equivalent to creating a dockScanner sc =NewScanner (system.in);  while(true) {String str=Sc.nextline (); if("Quit". Equals (str)) Break; Datagrampacket Packet=//creating a packet is equivalent to creating a container                    NewDatagrampacket (Str.getbytes (), str.getbytes (). Length, Inetaddress.getbyname ("127.0.0.1"), 6666);            Socket.send (packet); //Shipping} socket.close ();


8. Network programming (UDP transmission multithreading)
A send and receive in a window complete

    Public classDemo3_morethread { Public Static voidMain (string[] args) {NewReceive (). Start (); NewSend (). Start (); }                }        classReceiveextendsThread { Public voidrun () {Try{datagramsocket Socket=NewDatagramsocket (6666);//creating a socket is equivalent to creating a dockDatagrampacket packet =NewDatagrampacket (New byte[1024], 1024);//creating a packet is equivalent to creating a container                                         while(true) {socket.receive (packet); //Receiving goods                        byte[] arr =Packet.getdata (); intLen =packet.getlength (); String IP=packet.getaddress (). gethostaddress (); SYSTEM.OUT.PRINTLN (IP+ ":" +NewString (arr,0, Len)); }                } Catch(IOException e) {e.printstacktrace (); }            }        }        classSendextendsThread { Public voidrun () {Try{datagramsocket Socket=NewDatagramsocket ();//creating a socket is equivalent to creating a dockScanner sc =NewScanner (system.in);  while(true) {String str=Sc.nextline (); if("Quit". Equals (str)) Break; Datagrampacket Packet=//creating a packet is equivalent to creating a container                                NewDatagrampacket (Str.getbytes (), str.getbytes (). Length, Inetaddress.getbyname ("127.0.0.1"), 6666);            Socket.send (packet); //Shipping} socket.close (); }  Catch(IOException e) {e.printstacktrace (); }            }        }



9. Network programming (TCP protocol)
1. Client
Create socket connection server (specify IP address, port number) to find the appropriate servers by IP address
Call the getInputStream () and Getoutputstream () methods of the socket to get the IO stream connected to the server
The input stream can read the data written out by the service-side output stream
Output stream can write data to the input stream on the server
2. Service-side
Create ServerSocket (requires port number specified)
Call ServerSocket's Accept () method to receive a client request and get a socket
Call the getInputStream () and Getoutputstream () methods of the socket to get the IO stream connected to the client
The input stream can read the data written out by the client output stream
Output stream can write data to the client's input stream

10. Network Programming (TCP Protocol code optimization)
Client

Socket socket =NewSocket ("127.0.0.1", 9999);//Create socket Specify IP address and port numberInputStream is = Socket.getinputstream ();//Get input streamOutputStream OS = Socket.getoutputstream ();//Get output streamBufferedReader br =NewBufferedReader (NewInputStreamReader (IS)); PrintStream PS=Newprintstream (OS);        System.out.println (Br.readline ()); Ps.println ("AAAAAA");        System.out.println (Br.readline ()); Ps.println ("NNNNNN"); Socket.close (); service-side ServerSocket server=NewServerSocket (9999);//Creating a serverSocket socket = server.accept ();//accepting requests from clientsInputStream is = Socket.getinputstream ();//Get input streamOutputStream OS = Socket.getoutputstream ();//Get output streambufferedreader BR=NewBufferedReader (NewInputStreamReader (IS)); PrintStream PS=Newprintstream (OS); Ps.println ("AAAAA");        System.out.println (Br.readline ()); Ps.println ("BBBBBB");        System.out.println (Br.readline ());        Server.close (); Socket.close ();



11. Network programming (the server is multi-threaded)

ServerSocket Server =NewServerSocket (9999);//Creating a server         while(true) {            FinalSocket socket = server.accept ();//accepting requests from clients            NewThread () { Public voidrun () {Try{bufferedreader br=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); PrintStream PS=NewPrintStream (Socket.getoutputstream ()); Ps.println ("Welcome to the Wisdom Podcast");                        System.out.println (Br.readline ()); Ps.println ("Full, please report the next issue.");                        System.out.println (Br.readline ());                    Socket.close (); } Catch(IOException e) {e.printstacktrace ();        }}}.start (); }    }





25th Day Notes

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.