J2se Network Programming

Source: Internet
Author: User

I. Network Basics

Network Communication Protocol: for speed and transmissionCodeCode structure, transmission control steps, error control, and other standards.

Network Communication Interface: hardware device: Implements information transmission between nodes.

Software device: Agreement for communications between the parties.

 

 

 

 

Software developers should master the layer-4 model on the right.

Ii. TCP/IP protocol

IP protocol: provides unique IP addresses.

-| IPv4

Subnet Mask

Gateway

-| IPv6

TCP: a connection-oriented protocol. Reliable, end-to-end byte stream communication protocol. Byte stream.

That is, data can be transmitted in place. Data Packets arrive in order without any disordered order. Such as phone number.

Relatively low efficiency than UDP

UDP: a method for sending encapsulated original IP datagram, and no connection is required during sending. Is an unreliable connection.

Only sends data, regardless of whether the recipient receives the data. Such as a telegram.

 

TCP is reliable and slow.

UDP is not reliable, fast, and efficient.

 

Iii. Socket

1. Used for client-server connection

2. The two socket classes and serversocket classes defined in the java.net package are respectively used to implement two-way connection between the client and server.

3. The addressing information required for establishing a connection is the IP address and port number of the remote computer (port number after 1024 during development)

-| The TCP port is different from the UDP port.

Each port has 65536 ports.

 Import  Java. Io. datainputstream;  Import  Java. Io. ioexception;  Import  Java.net. serversocket;  Import  Java.net. Socket;  Public  Class  Tcpserver {  Public   Static   Void Main (string ARGs []) Throws  Ioexception {serversocket SS = New Serversocket (6666 );  While ( True  ) {Socket s = SS. Accept (); datainputstream dis =New  Datainputstream (S. getinputstream (); system. Out. println (DIS. readutf (); DIS. Close (); S. Close ();}}}  Import  Java. Io. dataoutputstream;  Import  Java. Io. ioexception;  Import  Java. Io. outputstream;  Import  Java.net. Socket;  Import  Java.net. unknownhostexception;  Public  Class  Tcpclient {  Public   Static   Void Main (string ARGs []) Throws  Unknownhostexception, ioexception {socket s = New Socket ("127.0.0.1", 6666 ); Outputstream OS = S. getoutputstream (); dataoutputstream DoS = New  Dataoutputstream (OS); dos. writeutf ( "Hello gay! "); Dos. Flush (); dos. Close (); S. 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.