Java Basic Learning Summary--network programming

Source: Internet
Author: User

Never give up, everything is possible!!!

Only to find a way to succeed, not to find excuses for failure!

Java Basic Learning Summary--network programming one, network basic concept

First, clear a concept: Network programming! = website Programming, network programming is now generally referred to as TCP/IP programming.

  

Second, network communication protocol and interface

  

Third, the idea of layered communication protocols

  

Iv. Reference Model

  

Five, IP protocol

  

Each person's computer has a unique IP address so that it does not pass the wrong message when communicating with each other.

The IP address is divided into four segments with one point, and the IP address in the computer is represented by four bytes, one byte represents a paragraph, and each byte represents the maximum number of 255 to be reached.

vi. TCP protocol and UDP protocol

  

TcpAndUdpLocated on the same level, are built on theIpLayer on the basis of the above. Since there is a difference between the two computersIpAddress, so the two computers can be separated, and they can talk to each other. Calls generally have two ways of calling: the first isTcp, the second type isUdptcp is a reliable connection, tcp Upload anything is reliable, as long as two machines built up a connection, The data sent on this machine will be transmitted to each other's machine, udpudp tcpudp transmitting data is unreliable, but it is delivered fast .

Seven, Socket programming

General network programming is called Socket programming, Socket in English means "socket".

  

Both computers are installed with a socket, and then plugged into two computer sockets using both ends of a line, so that the two computers are connected. This socket is the socket .

Because they can communicate with each other, and I say you are mine.ServerJust in a logical sense, I should send things to you first, and then by you to deal with, forward. So you calledServer。 But technically speaking, onlyTcpWill be dividedServer and udpserver and clienttcp The socket is called serversocket,client sockets are called

When two computers are connected to each other, you must first know their IP address, but it is not enough to provide an IP address, and you must have a connected port number, which is the application to which you want to connect.

   The port number is used to distinguish between different applications on a machine .。 The port number inside the computer is accounted for2A byte. Up to a single machine65536A port number. An application can occupy multiple port numbers. Port number if it is used by an application, other applications will no longer be able to use the port number. Remember, the program we're writing takes up the port number.1024The port number above,1024tcp port and port, tcp 8888 Port and 8888 port is a completely different two port. tcp Port and ports have 65536

Eight, TCP socket communication model

  

Nine, Socket use example

Server-side serversocket

1Import java.net.*;2Import java.io.*;3PublicClasstestserversocket{4PublicStaticvoid Main (String args[])Throwsexception{5 ServerSocket SS =New ServerSocket (6666);6/*When a ServerSocket object is created, it is often assigned a port number7Specifying the port number means that the new ServerSocket object is to be used by the8Which port number, which port number to listen to the client's connection9So the meaning of specifying a port number is to tell the computer that the ServerSocket object10Where to listen for client connections*/11/*The server-side request to receive client connections is received uninterrupted, so the server-side12Programming is usually a dead loop, and it runs endlessly.*/13WhileTrue){Socket s =Ss.accept ();15/*Calling the Accept () method on the server side accepts the client's connection object, and the Accept () method is16A blocking method that has been innocently waiting for a client to request a connection17The socket socket on the server side is then connected to the client socket socket.*/18/*The client can connect to the server side, depending on whether the server accepts the client's connection request19If you accept a connection request from a client, install the previous socket socket on the server side20Through this socket and connection to the client can establish a connection, communicate with each other*/System.out.println ("A Client connected!");22/*Use the InputStream stream to receive information sent from the client, using the DataInputStream data stream to process the received information*/DataInputStream dis =NewDataInputStream (S.getinputstream ());24/*Use readUTF (method to read all the received information, stored in the variable str inside25The readUTF () method is also a blocking method that innocently waits for the client to send a message and then reads the received information.26If the client does not write something, it will always be waiting on the server side innocently, until the client writes something.27Plug-in method efficiency is often not high, such as a client connection up, but it is slow to send information,28Then the server side of the program is blocked, so that another client can not connect, because another client to connect29On the server side, the Accept () method must be called on the server side, and the accept () method must be in the next loop to be Call, now the server side of the program run to call readUTF () This method is blocked, it waits for already connected to the  client sent the message to read out, if the client has not sent information to the server side, Then the readUTF () method has been unable to read the information, then the server side of the program will be blocked here, the next cycle can not be done, so that other clients can  not connect to the server side of * * *String str = Dis.readutf ();  System.out.println (str);                 (+}) 

Client Socket

1Import java.net.*;2Import java.io.*;3PublicClasstestclientsocket{4PublicStaticvoid Main (String args[])Throwsexception{5 Socket s =New Socket ("127.0.0.1", 6666);6/*Client request connection to server side*/7/*After connecting to the server side, you can output information to the server side and receive the information returned from the server side8 output information and receive return information are processed using streaming input and output principles */ 9 /**/10 outputstream os = S.getoutputstream (); 11 dataoutputstream dos = new DataOutputStream (OS); Span style= "color: #008080;" >12 Thread.Sleep (30000); /* client sleeps 30 seconds before sending information to the server 13 dos.writeutf ("Hello server!" ); 14 }15}     

The client requests the connection through Port 6666 to the server side, after the server accepts the client's connection request, installs a socket on the server side, and then lets the socket with the client socket Connection so that the server can communicate with the client, and when another client requests a connection, the server accepts it and then installs another socket with the client socket .

Java Basic Learning Summary--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.