##network programming

As the name suggests the need for a network programming network programming operation through the network, for network programming, general interview is going to be, what kind of problem?

  1, can tell the difference between UDP and TCP protocols, or features

  2, the name of the TCP protocol are two common classes

  3, can be understood using TCP upload Cases

So start today's knowledge review:

First, the software architecture CS / BS

  Getting :( 1.1 network programming for network programming we must first know)

  CS structure: the whole is Client / Server client and server architecture

  BS structure: full name is Browser / Server browser and server architecture

Network programming is to enable communication between the two computers in a certain protocol.  Either both architectures have their advantages are inseparable from the support network 

  1.2 Network Communication Protocol: a communication protocol is a computer must observe the rules for data exchange ------

TCP / IP Transmission Control Protocol / Internet Protocol layer hierarchical model 4

    1, the physical layer  

    2, the data link layer underlying network protocol

    3, the network layer IP

    4, the transport layer TCP UDP

    5, HTTP application layer

  1.3 protocol classification: java.net package contains the classes and interfaces

1.3.1TCP connection-oriented communication protocol it will be three-way handshake:

  1, the client sends a request to the server, the server waits for acknowledgment

  2, the server responds to the confirmation request

  3, the client again send confirmation request to the server, the connection confirmation

1.3.2UDP user data communication protocol connectionless-oriented protocol, data transmission need not be connected

Features: fast transmission, but easy to lose data, unreliable

Second, the three elements of the communications network: IP network protocol port number

2.1 network protocol has just been recalled or above, then we know for IP computer's IP Why?

  windos + r key to open our cmd console, enter IPconfig Enter to see our own computer's IP

So sometimes we have to make sure the computer can not access the Internet, how to see it?

  Just type ping the domain name space on the console can be, if there is access to the Internet return instructions

Here emphasize that in our own native computer IP: localhost 127.0.0.1

2.2 Port Number: a communication network is essentially two processes (applications) represents an integer of 0-65535 by two bytes

  0-1023 can not be used, because the well-known network services have been used

  Server port number 80

  mysql: 3306 Free

  oracle:1521

three. TCP communication RECAP

3.1 Client should strictly distinguish between the client and server: Server

Ends of the communication step:

  The client can take the initiative to connect to the server connection is successful communication

  The server must first start

3.2java provides two classes to implement TCP program

java.net.Socket ------> Create Object Socket ----- "send a request made to the server in response to both the server to establish communication

ServerSocket -----> Create Object ServerSocket ----> is equivalent to open a service to wait for client connections

four. 4 Socket Socket key and difficult

4.1 Overview: This class implements client sockets (also called just "sockets"). A socket is an endpoint of communication between two machines.

4.2 Constructor

public Socket (InetAddress address, int port) to create objects connected to the specified port number host of `

          public Socket(127.0.0.1,6666);

   4.3 Methods member

Input stream getInputStream () ------> socket

Output getOutputStream () ------> stream socket

​     close();

shutdownOutput (); disable socket output stream

public  class DemoLient {
     public  static  void main (String [] args) throws IOException {
         // create the back end Socket client object with its own IP address and port number 
        Socket socket = new new Socket ( "127.0.0.1", 8888 );
         / / client Socket obtain an output stream by outputting getOutputStream 
        the OutputStream OS = Socket.getOutputStream ();
         // output flow brother call server, and at the same time turn the string byte array 
        os.write ( "server hello!" .getBytes () );
         // client socket input stream created by the input stream InputStream 
        InputStream IS = Socket.getInputStream ();
         // create an array to make reading faster 
        byte[]bytes=new byte[1024];
        int len=is.read(bytes);
        System.out.println(new String(bytes,0,len));
        //释放资源
        os.close();
        socket.close();
    }
}

Fives. ServerScoket key and difficult

   5.1 ServerScoket constructor

    ServerSocket ** (int port) `bind a specified port number` ServerSocket (6666) `

  5.2 member method accept () to listen and accept connections for clients communicate blocked until a connection

public  class demoServer {
     public  static  void main (String [] args) throws IOException {
         // create ServerSocket server objects, a fixed port number 
        ServerSocket SE = new new ServerSocket (8888 );
         // ServerSocket objects listener method generates a call accept Socket object 
        Socket = Accept se.accept ();
         // generate network input byte stream Socket object using 
        the InputStream IS = accept.getInputStream ();
         // use of local input stream read 
        byte [] bytes = new new  byte [1024 ];
         int len = is.read (bytes);
        System.out.println ( new new String (bytes, 0 , len));
         // after receiving content client using a network transmission output stream of bytes, thank 
        the OutputStream OS = accept.getOutputStream (); 
        os.write ( "Thank receive "! .getBytes ());
         // release resources 
        accept.close (); 
        se.close (); 
    } 
}

six. Case upload pictures to the server

Principle file upload server

    1 using the local client file byte stream read local

    The client uses the network 2 bytes output of the pictures to the server

The network server 3 using byte stream read an input file uploaded by the client

4 stored on the server using the local output stream of bytes to the client's file server's hard disk

5 server network output stream to the client write transfer success

6 client uses network byte input stream to read the server write-back "transfer success"

Note: The client / server and the local file read and write must create their own flow

Interaction between a client / server network Socket stream provided by

    Client Local ---- "Copy ---" server

Server ---- "Copy ----" server local hard disk

It must be clear: the data source destination

Guess you like

Origin www.cnblogs.com/liurui-bk517/p/10939928.html