Job 13-Network 1. Summary of this week's study
Summarize multiple web-related content in the way you like (mind Mapping, OneNote, or others).
2. Add network functions to your system (shopping cart, library management, landlords, etc.)-group complete
To allow your system to be used by multiple users over the network, you need to add network functionality to your system.
2.1 What network functions do you want to add to your system? What is the design idea?
A: The main is to add a NetServer
class, so that the code can be used by the client remote connection for a series of operations. To enable the system to be accessed by multiple clients at the same time, the use of TCP technology and multithreading technology is chosen. The NetServer
socket for the object is created in the class ServerSocket
, and then a class is created ThreadServer
to implement the multithreading technique, which implements the Runnable
interface and writes out the appropriate actions that the user can perform in the Run method. It then creates the thread in the main function and receives the new client continuously, thereby enabling multiple users to operate the system simultaneously.
2.2 What information does the system pass through the network? What is the format of the information?
A: The system through the network to the client or server operations carried out by the use of Io pass to the other side, the information using byte stream InputStream
and OutputStream
storage, and then through the character stream Scanner
and PrintWriter
packaging transmission.
2.3 Does your network module use TCP or UDP technology? What are the common classes in a module?
A: Using TCP technology, the most commonly used classes in modules are TCP network traffic classes and classes ServerSocket
, as well as Socket
Multi-threading aspects of Runnable
classes and Thread
classes
2.4 The source code of the network communication part, and carry on the simple explanation.
A: The main thing is to create a NetServer class, and define its port is 9700, and then open the server in the main function to receive the client, the received client placed in a thread to be processed separately. ThreadServer
implement the Run method in the class to set what the user can do.
2. Code cloud and PTA3.1 Statistics The amount of this week's work done
The weekly code statistics need to be fused into a single table.
Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
2 |
30R |
30R |
15 |
15 |
3 |
1642 |
1035 |
33 |
18 |
5 |
2044 |
402 |
42 |
9 |
6 |
2874 |
830 |
57 |
15 |
7 |
3161 |
287 |
63 |
6 |
8 |
4299 |
1138 |
72 |
9 |
9 |
4831 |
532 |
81 |
9 |
10 |
5475 |
644 |
93 |
12 |
11 |
5958 |
483 |
102 |
9 |
12 |
6819 |
861 |
116 |
14 |
13 |
7408 |
48X |
127 |
11 |
14 |
8348 |
940 |
141 |
14 |
Option: 4. Network Learning Guide (the following job content is selected) 1. Network Fundamentals 1.1 Comparison
ping www.baidu.com
And
ping cec.jmu.edu.cn
, what is the difference between the analysis and return results? Why is there such a difference?
- IP address is different: Because the server is located in the same region, so the IP is not the same.
117.185.17.151
from Shanghai, and 112.5.163.149
from Xiamen City, Fujian province.
- Round-Trip time is different: Xiamen servers are closer to us, so the round-trip time is faster.
1.2
telnet bbs.newsmth.net
, what port number does this command connect to the remote host?
A: The default port used by the remote login service is 23, this general knowledge, of course, can also be queried by the netstat
command.
1.3
telnet cec.jmu.edu.cn 80
, try to answer: what do you see from the screen? The returned content is not
cec.jmu.edu.cn
's homepage content? Try to use
GET /index HTTP/1.0
command, what will return? See the experimental Task Book topic 1.
Answer: See ... A dark, nothing. When the command is used GET /index HTTP/1.0
, the source code of the school homepage is returned. This is because the Web page of the school obeys the HTTP protocol, so you use the GET /index HTTP/1.0
send request to get the page content.
1.4 Start the Connectionwatcher, and then start a number of Internet-enabled programs (such as QQ, Telnet, browser, etc.), to see what the computer on the Internet, what port is used? What ports does QQ use to interact with those remote IPs, where are these IPs distributed? What protocols are used for communication?
- 360 Browsers use 443 ports
- Youdao Dictionary uses 80 port
- QQ uses 80 ports, 8080 ports, 443 ports and those remote IP to interact, these IP distributed in Guangdong province Shenzhen, using the TCP protocol and UDP protocol to interact
1.5 What are some of the essential information required to write a network program?
- Network protocol (IP protocol, TCP and UDP protocol)
- Ports and sockets (sockets, ServerSocket)
- Basic knowledge of IP and domain names
- Input/output stream
2. Write a TCP client program 2.1 reference Sockettest.java write a simple connection to the remote server applet Client.java, you can receive and display the information sent by the server. The remote host IP and port number must first be specified on the console at startup. (, show your own school number).
A: In fact, not how to change the source code, just add a console input, and then query the IP address of Baidu to receive the information of the page.
2.2 Change the URL of the program to Bbs.newsmth.net, the port number to 23, and observe the return result again? Try to execute
telnet bbs.newsmth.net 23
, comparing the returned results.
A: As you can see, some characters do not display properly except for some coding problems, and the results are almost the same.
2.3 Change the URL of the program to cec.jmu.edu.cn, and change the port number to 80 to see the results returned. No return results, why? How do I change the code to display properly?
choose to do:Why does it return garbled characters? How to solve?
A: There is no return result because no GET / HTTP/1.0\n
command is used to send a request, so there is no return result, plus the following code.
Garbled mainly because the default character set on Eclipse is GBK, and the character set on the Web page is not, I first look at the source code of the Web page, it can be seen that the character set of the site is UTF-8, and then simply modify the character set on Eclipse to UTF-8. Normal display when run again
2.4 Start the Network debugging assistant, set it to TCP Server, and start as shown (do not appear in the job)
Use Client.java
this server to send information to the client by attempting to connect to the TCP server. (Key code and run result)
2.5 Answer: The present
Client.java
Can only receive information, can be modified
Client.java
So that it can interact with the server (the server can send information to Client,client can also send information to the server), write the approximate idea, do not run.
A: In general, to achieve the client-server interaction, to use the knowledge of the thread, you can create two threads in the code, implement their run method, one thread for the client to send data to the server, another thread for the servers like the client to send data.
2.6 Optional: implementation 2.5. Intercept the key code and explain it.
3. ServerSocket with TCP server 3.1 simple single thread menuserver, title 3.1-3.4. (Key code and run, including the school number) can use the Network debugging assistant, set it to TCP Client (IP and port fill your menuserver IP and port), connect your menuserver. The file search function is optional.
3.2 Multi-threaded menuserver (intercept key code to prove that you are a multithreaded server, use multiple Telnet connection tests).
4. UDP Server 4.1 Retrofit 3.1 or 3.2, transforming it into UDP-based menuserver. (The key code of the TCP server that distinguishes the topic 3, need to truncate a large map, include the number, do not run the interface). You can use the Network debugging assistant to impersonate a UDP client and set it to
UDP
, to fill
本机IP与端口
Also need to fill in
远程服务器的IP与端口
。
4.2 Compared to the corresponding TCP-based menuserver, what is the main difference? In order to connect multiple clients, do you have to use multithreading technology? How do you differentiate between clients if you don't use them?
A: UDP is a non-connected way of exchanging data, you do not need to establish a connection before sending the data, and TCP to establish the connection before the data can be transmitted. Multithreading is required in order to connect multiple clients, or because UDP does not create a connection between the sender and the receiver, it is not possible to differentiate between different clients.
5. Topic: The network program and Flow 5.1 before the program was written with a lot of knowledge of the flow. Briefly, what kind of flow should I choose when writing a network program? Java network programming, the relationship between streams. Choose a scene description, such as: simple text chat, file transfer, Application layer protocol server, such as QQ, chat rooms, online games.
A: For a network chat room, we can use the BufferedReader and bufferedwriter character streams to do the data receiving and sending operations to achieve communication between users and users. In the Java network programming, we first use the socket to create an object, and then we want to communicate between the network, it is necessary to use the various IO streams in Java, therefore, Java IO Stream is the main way of data transmission in network programming.
6. Code Reading
Read the source code in the Serverandthread directory
6.1 Modify the program, in the GUI interface to display their own school number, and then run the program (, the number of the study). Describe the main functions of this program.
A: The function of this program is to connect the client and the server, so that they can communicate, similar to a simple chat system. The study number was added directly to the title bar, simple and rude ...
6.2 Draw the structure diagram of the source code (dependencies of each class). and briefly describe the functions of each class.
ServerChart
: interface Framework on the server side
ServerReadThread
: Communicates with the client and displays the received message
ServerActionListener
: Service-side action listener, adding listener events for buttons on the server interface
ClientChart
: Interface Framework for clients
ClientReaderThread
: communicates with the server and displays the received message
ClientListener
: Client action Listener, adding listener events for Client interface buttons
MainThread
: Let the client establish a connection with the server, put the thread into the list and let the server read
6.3 Briefly describe the shortcomings of this program and how to improve it.
1. Messages received by the Client interface cannot be distinguished from who sent them.
Improved method: When sending a message on the client and the server, it is flagged who sent it.
2. Messages received by the server do not have a newline.
Improved method: Displays a newline character on the server side of your message.
3. Regardless of which end is first closed, the other end will continue to request the input stream and continuously output error messages.
Improved method: When the other end is closed, the exit request is used directly when the exception is handled break
.
201621123031 Java Programming 13th Week of study summary