Chapter One introduction to Network programming

Source: Internet
Author: User
Tags rfc file transfer protocol

1.1 OSI Reference Model

1. Physical Layer
Transmission media, such as twisted pair and coaxial cable.
2. Data Link Layer
Responsible for error-free transmission of data in frames at two adjacent nodes. This layer is responsible for establishing, maintaining, and releasing connections to data links, such as switches.
3. Network layer
This layer is responsible for selecting the appropriate inter-network routing and switching nodes. such as routers.
4. Transport Layer
The task of this layer is to provide two end-to-end session layers with the ability to establish, maintain, and cancel transport connections based on the best utilization of network resources based on the characteristics of the communication subnet.
5. Session Layer
Manages the session process between processes.
6. Presentation Layer
Convert the upper data.
7. Application Layer
Determine the actual purpose of inter-process communication.

1.2 TCP/IP Reference Model

TCP: Transmission Control Protocol, is a connection-oriented, reliable protocol.

UDP: Unreliable, no connection protocol.

1. Application Layer

TCP-based application layer protocols are available in the following ways:

FTP: File Transfer Protocol

TELNET: The virtual Terminal protocol, which allows login from host A to remote Host B, so that a acts as a virtual terminal for B.

HTTP: Hypertext Transfer Protocol

HTTPS: Secure Hypertext Transfer Protocol

POP3: Post Office Protocol-version 3

Imap4:internet Message Access Protocol-version 4 allows users to navigate through the browser and manipulate mail and mail folders on remote servers.

The UDP-based application layer protocol has the following:

SNMP: Simple Network Management Protocol

DNS: Domain Name System protocol

2. Transport Layer
3. Network Interconnect layer
4. Host-Network layer

1.3 IP protocol

The IP address consists of two parts: the IP URL and the IP host address.

The IP host address represents the address of the host in the network. The netmask is used to determine which part of the IP address is the URL and which part is the host address.

such as ip:192.166.3.4 mask: 255.255.255.0

The binary of the mask corresponds to: 11111111.11111111.11111111.00000000

The mask and IP address are binary and the operation gets the IP address

1. Sub-Network partitioning

If the URL is: 192.166.0.0, you can have 2^16-2 (65534) host, you can separate three subnets: 192.166.1.0 192.166.2.0 192.166.3.0 The mask of three subnets is 255.255.255.0

Note: 192.166.0.0 is the network address, 192.166.111.111 is the broadcast address and cannot be used as the host address.

2. The process of sending a packet

IP is a packet-oriented protocol, and data is divided into multiple packets, which are transmitted separately. Hosts on IP networks can only send packets to other hosts on the local network.

The host has two addresses of different natures: Physical address and IP address. The physical address is identified by the host's network card. The physical address is the real address of the host.

When host a sends a packet to Host B on the same network, it obtains the other's physical address through the Address Resolution Protocol (ARP) and sends the package to the other party.

Personal understanding: As an example of a local router, when host a sends a packet to Host B, if the access address is not a local area network address, it is forwarded through the WAN port via the default route and continues searching through the carrier's route.

3. Domain Name

The rightmost part of the domain name is the top-level domain name and the leftmost machine name.

The general form is: Host machine name. Unit name. Network Name. Top-level domain name

4.URL (Unified Resource Locator)

such as: http://www.javathinker.org/bbs/index.jsp

"http" refers to Hypertext Transfer Protocol, "www.javathinker.org" is the domain name of the server, "BBS" is the path of the Web page, "index.jsp" is the corresponding Web page file.

1.4 TCP protocol and port

The TCP protocol enables the process of two hosts to communicate smoothly without worrying about packet loss or packet order confusion. TCP tracks packet order, but the package order is scrambled when the package is reorganized in the correct order. If the packet is lost, TCP requests the source host to re-contract.

The IP protocol sends data to the corresponding host based on the IP address of the host. TCP uses ports to differentiate processes. The port is not a physical device, but a logical address.

The range of port numbers is 0-65535. The port number of 0-1023 is generally fixed to some services. For example 21 assigned to ftp,25 assigned to SMTP,80 is assigned to HTTP.

Note: The port value ranges for TCP and UDP in one host are separate, allowing the same TCP port and UDP port to exist.

1.5 RFC Introduction

The TCP/IP protocol is published in the form of an RFC document. The RFC is a document describing Internet-related technical specifications.

1.6 Java writing C/s program

The Java program in this paper is based on TCP/IP protocol and is committed to implementing the application layer. The transport layer provides the socket socket interface to the application layer. It encapsulates the data transfer details of the underlying, and the application layer's program uses the socket to establish a connection and data transfer to the remote host.

There are three types of socket classes in Java: Java.net,socket,java.net.serversocket,java.net.datagramsocket.

The first two are based on the TCP protocol, and the latter one is based on the UDP protocol.

1. Create Echoserver

registering the current process as a server process

ServerSocket Server = new ServerSocket (8000);// Specify 8000 port

Monitor

Socket socket = server.accept ()// waiting for user connection request

the Socket class provides the getInputStream () method and the Getoutputstream () method to return the input and output stream objects.

The code is as follows:

Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.ServerSocket; Import Java.net.socket;public class Echoserver {private int port=8000;private serversocket serversocket;public Echoserver () throws Ioexception{serversocket = new ServerSocket (port); SYSTEM.OUT.PRINTLN ("server Start");} public string Echo (String msg) {return "echo:" +MSG;} Private PrintWriter getwriter (socket socket) throws Ioexception{outputstream Socketout = Socket.getoutputstream (); return new PrintWriter (socketout,true);//set to True to indicate automatic refresh}private bufferedreader getreader (socket socket) throws Ioexception{inputstream Socketin = Socket.getinputstream (); return new BufferedReader (new InputStreamReader (SocketIn) );} public void Service () {while (true) {Socket socket=null;try{socket=serversocket.accept ();//socket.getport () Returns the local port that the client socket occupies System.out.println ("New connetion Accepted" +socket.getinetaddress () + ":" +sOcket.getport ()); BufferedReader br = getreader (socket); PrintWriter pw = getwriter (socket); String msg =null;while ((Msg=br.readline ())!=null) {System.out.println (msg);p w.println (Echo (msg)), if (Msg.equals (" Bye ")) {break;}}} catch (IOException e) {e.printstacktrace ();} Finally{try{if (Socket!=null) Socket.close ();} catch (IOException e) {e.printstacktrace ();}}}} public static void Main (String args[]) throws Ioexception{new Echoserver (). Service ();}}

  

2. Create Echoclient

String host= "localhost";

String port= "8000";

Socket socket=new socket (host,port);

Host indicates the name of the host on which the echoserver process resides, and port represents the port number that the Echoserver process listens on.

The code is as follows:

Import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.Socket;import Java.net.unknownhostexception;public class echoclient{private String host= "localhost";p rivate int port=8000;private Socket Socket;public echoclient () throws unknownhostexception, Ioexception{socket = new socket (host,port);} Private PrintWriter getwriter (socket socket) throws Ioexception{outputstream Socketout = Socket.getoutputstream (); return new PrintWriter (socketout,true);//set to True to indicate automatic refresh}private bufferedreader getreader (socket socket) throws Ioexception{inputstream Socketin = Socket.getinputstream (); return new BufferedReader (new InputStreamReader (SocketIn) );} public void Talk () {try{bufferedreader br =getreader (socket); PrintWriter PW =getwriter (socket); BufferedReader localreader = new BufferedReader (new InputStreamReader (system.in)); String Msg=null;while ((Msg=localreader.readline ())!=null) {pw.println (msg); System.out.println (Br.readline ()); if (Msg.equals ("Bye")) {break;}}} catch (IOException e) {e.printstacktrace ();} Finally{try{socket.close ();} catch (IOException e) {e.printstacktrace ();}}} public static void Main (string[] args) throws Unknownhostexception, IOException {new Echoclient (). talk ();}}

  

Chapter One introduction to Network programming

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.