Java Network Programming entry notes (1)

Source: Internet
Author: User

 

 

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class EchoPlayer {public String echo(String msg) {return echo: + msg ;}public void talk() throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String msg = null;while ((msg = br.readLine()) != null) {System.out.println(echo(msg));if (msg.equals(bye)) break;}}public static void main(String arg[])  throws IOException{new EchoPlayer().talk();}}

 

 

 

The following code is a server example: the client sends a message to the server, and the server sends the same message to the client after receiving the message.

EchoServer

 

import java.awt.Stroke;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);}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();System.out.println(New connection 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);pw.println(echo(msg));if (msg.equals(bye)) {break;}}} catch (IOException e) {// TODO: handle exceptione.printStackTrace();}finally{try {if (socket != null) {socket.close();}} catch (IOException e2) {// TODO: handle exceptione2.printStackTrace();}}}}public static void main(String args[]) throws IOException {new EchoServer().service();}}

In the service () method of the EchoServer program. the accept (); method returns a Socket object, which indicates that a link is established with a customer. This Socket object contains the customer's address and port information, you only need to call the Socket of the socket object. getInetAddress () and socket. the getPort () method can obtain the information separately.

 

 

 

Basic knowledge:

1. the Java Network Program is located at the Network Interconnection layer of the TCP/IP Reference Model.

 

2. the TCP and IP protocols are located at the transport layer.

 

3. If a process has occupied port 80 of TCP, can it still occupy port 80 of UDP? No

 

4. A customer process executes the following code;

Socket socket1 = new Socket (host, port );

Socket socket2 = new Socket (host, port );

The following statements are true:

A. socket1 and socket2 use different local ports.

B. When the Socket constructor returns successfully, a TCP link is established with the server.

 

5. There is a protocol that stipulates that if the client sends a line of string date, the server returns the current date information. If the client sends a line of string exit, the server end the communication with the client. What protocol should this protocol be used?

Answer: Application Layer

 

6. What is the TCP port used by the HTTP server by default? 80

 

7. What are the main tasks of customer programs and server programs in the Client/Server Communication Mode?

A. the client sends the request and receives the server response.

B. The server program receives and processes customer requests, and then sends response results to the customer

 

8. Where can I find a specific document describing the TCP/IP protocol. Official RFC website

 

9. A server process executes the following code:

ServerSocket serverSocket = new ServerSocket (80 );

Socket socket = serverSocket. accept ();

Int port = socket. getPot ();

A. the server process occupies port 80.

B. When the serverSocket. accept () method returns successfully, the server process receives a client connection request.

C. The socket. getPort () method returns the local port occupied by the client socket.

 

 

 

 

 

 


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.