当前位置:网站首页>Day26 job
Day26 job
2022-07-26 04:13:00 【Hello World️】
List of articles
- A: Short answer
- B: Programming questions
- 1、 complete UDP The sender ( Keyboard Entry )
- 2、 complete UDP The receiver ( Console output )
- 3、UDP agreement ( Multithreaded version chat )
- 4、TCP client ( Keyboard Entry )、 Server side ( Console output )
- 5、TCP client ( Keyboard Entry ) Data is stored in server-side files
- 6、TCP The client reads the text file 、 Server side ( Console output )
- 7、TCP Client upload file
- 8、TCP Client upload file ( Multithreaded version )
- 9、TCP agreement ( Multithreaded version chat )
A: Short answer
1、 Please put the methods in all the classes we have explained in API Find , And use your own words to describe
InetAddress
public static InetAddress getByName(String host)
By hostname host Determine the host IP Address
public String getHostAddress()
Return to the host's IP Address
public String getHostName()
Return the hostname
DatagramSocket
public void close()
Close the datagram socket
public void receive(DatagramPacket p)
adopt p Receive datagram packets
public void send(DatagramPacket p)
adopt p Send datagram packets
DatagramPacket
public InetAddress getAddress()
return IP Address
public byte[] getData()
Returns the data buffer
public int getLength()
Returns the length of data sent or received
ServerSocket
public Socket accept()
The server is waiting for the connection
public InetAddress getInetAddress()
Returns the local address of this server socket
Socket
public void close().
Close socket
public InetAddress getInetAddress()
Return the address of the socket connection
public InputStream getInputStream()
Returns the input stream for this socket
public OutputStream getOutputStream()
Returns the output stream for this socket
public void shutdownOutput()
Set end tag
Disable output stream of socket
2、 Please briefly describe what is network programming ?
Network programming is to realize data exchange between programs running on different computers connected by the network
3、 Please briefly OSI(Open System Interconnection Open Systems Interconnection ) The reference model has several layers ? What are the differences ?
OSI The reference model has seven layers
application layer : Mainly some terminal applications
The presentation layer : It mainly explains the received data 、 encryption and decryption 、 Compression and decompression, etc
The session layer : Through transport layer ( Port number Transmission port and receiving port ) To establish a path for data transmission
Transport layer : Some protocols and port numbers for data transmission are defined
The network layer : The data received from the lower layer is mainly processed IP Encapsulation and de encapsulation of address
Data link layer : The data received by the physical layer is mainly MAC Address ( network address ) Package and unpackage
The physical layer : Define physical device standards It is mainly used to transmit bitstreams
4、 Please briefly IP Classification of addresses .
IP Address = network address + The host address
A class 1.0.0.1---127.255.255.254
B class 128.0.0.1---191.255.255.254
C class 192.0.0.1---223.255.255.254
D class 224.0.0.1---239.255.255.254 Multicast address
E class 240.0.0.1---247.255.255.254 Reserved address
5、 Please briefly describe based on UDP And TCP The main difference between socket communication ?
1.TCP Connection oriented UDP There is no connection
2.TCP The protocol transmission speed is slow UDP The transmission protocol is fast
3.TCP There is a packet loss retransmission mechanism UDP No,
4.TCP Provide reliable service UDP There is no guarantee of reliability
5.TCP Byte stream oriented UDP Message oriented
6.TCP The connection can only be point-to-point UDP Support one-to-one , One to many , For one more , Many to many interactive communication
7.TCP First cost 20 Bytes UDP First cost 8 Bytes
8.TCP The logical communication channel of is a full duplex reliable channel UDP It's an unreliable channel
6、 What is Socket? And Socket Principle mechanism of ?
Socket Socket :
Unique identification on the network IP Address and port number are combined to form a unique identifier socket
Socket Principle mechanism of :
There are... At both ends of the communication Socket
Network communication is actually Socket Communication between
The data is in two Socket Interpass IO transmission
B: Programming questions
1、 complete UDP The sender ( Keyboard Entry )
Programming , adopt UDP agreement , complete UDP The sender , Constantly send the data entered by the keyboard to the receiving end , And test the .
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Enter what to send ");
String s = sc.nextLine();
DatagramPacket packet = new DatagramPacket
(s.getBytes(), s.getBytes().length, InetAddress.getLoopbackAddress(), 9999);
socket.send(packet);
}
}
}
2、 complete UDP The receiver ( Console output )
Programming , adopt UDP agreement , complete UDP The receiver , Constantly print the data entered by the keyboard at the sending end , And test the .
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
while (true) {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
socket.receive(packet);
byte[] data = packet.getData();
int length = packet.getLength();
String s = new String(data, 0, length);
System.out.println(s);
if ("886".equals(s)){
System.out.println(" Program exit ");
System.exit(0);
}
}
}
}
3、UDP agreement ( Multithreaded version chat )
Programming , adopt UDP agreement , Complete the multi-threaded version of the chat room program .
public class UDPClient {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
// The sub thread receives the server message
UDPClientThread clientThread = new UDPClientThread(socket);
clientThread.start();
// The main thread sends a message to the server
Scanner sc = new Scanner(System.in);
while (true){
System.out.println(" Enter the message to be sent to the server ");
String s = sc.nextLine();
byte[] data = s.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLoopbackAddress(), 9999);
socket.send(packet);
if ("886".equals(s)){
System.out.println(" The client stopped chatting Program exit ");
System.exit(0);
}
}
}
}
public class UDPClientThread extends Thread {
private DatagramSocket socket;
public UDPClientThread(DatagramSocket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
socket.receive(packet);
data = packet.getData();
int length = packet.getLength();
String s = new String(data, 0, length);
System.out.println(packet.getPort() + " port Server side :" + s);
if ("886".equals(s)) {
System.out.println(" The server stopped talking Program exit ");
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class UDPServer {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(9999);
// The sub thread receives the client message
UDPServerThread serverThread = new UDPServerThread(socket);
serverThread.start();
// The main thread sends a message to the client
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Enter the message to send to the client ");
String s = sc.nextLine();
byte[] data = s.getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getLoopbackAddress(), 8888);
socket.send(packet);
if ("886".equals(s)) {
System.out.println(" The server stopped talking Program exit ");
System.exit(0);
}
}
}
}
public class UDPServerThread extends Thread {
private DatagramSocket socket;
public UDPServerThread(DatagramSocket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
socket.receive(packet);
data = packet.getData();
int length = packet.getLength();
String s = new String(data, 0, length);
System.out.println(packet.getPort() + " port client :" + s);
if ("886".equals(s)) {
System.out.println(" The client stopped chatting Program exit ");
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、TCP client ( Keyboard Entry )、 Server side ( Console output )
Programming , adopt TCP agreement , Complete the client keyboard entry , Server side console output .
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
// Keyboard input the information sent to the server
Scanner sc = new Scanner(System.in);
while (true){
System.out.println(" Enter the information sent to the server ");
String s = sc.nextLine();
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write(s);
bw.newLine();
bw.flush();
// Judge the end of sending
if ("886".equals(s)){
System.out.println(" The client will no longer send messages Program exit ");
bw.close();
break;
}
}
socket.close();
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(" The server is waiting for the connection ....");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(" client : " + s);
if ("886".equals(s)) {
System.out.println(" The client will no longer send messages Program exit ");
br.close();
System.exit(0);
}
}
}
}
5、TCP client ( Keyboard Entry ) Data is stored in server-side files
Programming , adopt TCP agreement , Complete the keyboard entry of the client and store the data in the server-side file .
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
// Keyboard input the information sent to the server
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Enter the information sent to the server ");
String s = sc.nextLine();
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write(s);
bw.newLine();
bw.flush();
// Judge the end of sending
if ("886".equals(s)) {
System.out.println(" The client will no longer send messages Program exit ");
bw.close();
break;
}
}
socket.close();
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(" The server is waiting for the connection ....");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("ClientEnter.txt")));
String s = null;
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
if ("886".equals(s)) {
System.out.println(" The client will no longer send messages Program exit ");
br.close();
System.exit(0);
}
}
}
}
6、TCP The client reads the text file 、 Server side ( Console output )
Programming , adopt TCP agreement , Complete the client to read the contents of the text file , Server side console output
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("user.txt")));
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
String s = null;
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
}
System.out.println(" sent ");
br.close();
bw.close();
socket.close();
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(" The server is waiting for the connection ....");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String s = null;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
}
}
7、TCP Client upload file
Programming , adopt TCP agreement , Finish uploading files from the client to the server .
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("user.txt")));
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
String s = null;
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
}
System.out.println(" sent ");
br.close();
bw.close();
socket.close();
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(" The server is waiting for the connection ....");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy.txt")));
String s = null;
while ((s = br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
}
System.out.println(" Upload finished ");
bw.close();
br.close();
socket.close();
serverSocket.close();
}
}
8、TCP Client upload file ( Multithreaded version )
Programming , adopt TCP agreement , Finish uploading files from the multithreaded version client to the server , The server side gives feedback .
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
// Upload the file to the server
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\123.jpg"));
byte[] bytes = new byte[1024 * 8];
int readLen = 0;
while ((readLen = bis.read(bytes)) != -1) {
bos.write(bytes, 0, readLen);
bos.flush();
}
socket.shutdownOutput();
// Receive feedback from the server
bis = new BufferedInputStream(socket.getInputStream());
bytes = new byte[1024];
readLen = 0;
while ((readLen = bis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLen));
}
}
}
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println(" The server is waiting for the connection ....");
while (true) {
Socket socket = serverSocket.accept();
System.out.println(" user " + socket.getInetAddress() + " Successful connection ");
TCPServerThread serverThread = new TCPServerThread(socket);
serverThread.start();
}
// The server is generally not shut down
}
}
class TCPServerThread extends Thread {
private Socket socket;
public TCPServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
// Receive files uploaded by the client And save
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\" + System.currentTimeMillis() + "upload.jpg"));
byte[] bytes = new byte[1024 * 8];
int readLen = 0;
while ((readLen = bis.read(bytes)) != -1) {
bos.write(bytes, 0, readLen);
bos.flush();
}
bos.close();
System.out.println(" Upload finished ");
// Feed back the upload situation to the client
bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(" Upload finished ".getBytes());
bos.flush();
// Release resources
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
9、TCP agreement ( Multithreaded version chat )
Programming , adopt TCP agreement , Complete the multi-threaded version of client and server chat
// client
public class TCPClient {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(InetAddress.getLocalHost(), 8888);
// Read the server information Sub thread
TCPClientThread clientThread = new TCPClientThread(socket);
clientThread.start();
// Send information to the server The main thread
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Please enter the content Send it to the server ");
String data = sc.nextLine();
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write(data);
bw.newLine();
bw.flush();
if ("886".equals(data)) {
System.out.println(" The client stopped chatting Program exit ");
System.exit(0);
}
}
}
}
class TCPClientThread extends Thread {
private Socket socket;
public TCPClientThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String read = null;
while ((read = br.readLine()) != null) {
System.out.println(" Server side : " + read);
if ("886".equals(read)) {
System.out.println(" The server stopped talking Program exit ");
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Server side
public class TCPServer {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println(" The server is waiting for the connection ");
Socket socket = serverSocket.accept();
// Read client information
TCPServerThread serverThread = new TCPServerThread(socket);
serverThread.start();
// Send information to the client
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println(" Please enter the content Send to client ");
String data = sc.nextLine();
OutputStream outputStream = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
bw.write(data);
bw.newLine();
bw.flush();
if ("886".equals(data)) {
System.out.println(" The server stopped talking Program exit ");
System.exit(0);
}
}
}
}
class TCPServerThread extends Thread {
private Socket socket;
public TCPServerThread(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
while (true) {
try {
InputStream inputStream = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String read = null;
while ((read = br.readLine()) != null) {
System.out.println(" client : " + read);
if ("886".equals(read)) {
System.out.println(" The client stopped chatting Program exit ");
br.close();
System.exit(0);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
边栏推荐
- `Oi problem solving ` ` leetcode '2040. The k-th minor product of two ordered arrays
- [digital ic/fpga] Hot unique code detection
- laravel8 实现接口鉴权封装使用JWT
- (translation) the button position convention can strengthen the user's usage habits
- PHP method to find the location of session storage file
- Leetcode:1184. Distance between bus stops -- simple
- LeetCode. 6115 count the number of ideal arrays
- What model is good for the analysis of influencing factors?
- Apple removed the last Intel chip from its products
- 【第019问 Unity中对SpherecastCommand的理解?】
猜你喜欢

Sentinel fusing and current limiting

Inventory the concept, classification and characteristics of cloud computing

(translation) the button position convention can strengthen the user's usage habits

Acwing第 61 场周赛【完结】

Working ideas of stability and high availability guarantee

综合评价与决策方法

文献|关系研究能得出因果性结论吗

How to transfer English documents to Chinese?

Apisex's exploration in the field of API and microservices

吴恩达机器学习课后习题——逻辑回归
随机推荐
[in depth study of 4g/5g/6g topic-42]: urllc-13 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -7-low delay technology-1-subcarrier spacing expansio
JS get some attributes of the object
Opencv learning notes -- Hough transform
Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
Acwing game 61 [End]
图互译模型
How to transfer English documents to Chinese?
dijango学习
Opencv learning notes - edge detection and Canny operator, Sobel operator, lapiacian operator, ScHARR filter
PHP 对象转换数组
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
Summary of senior report development experience: understand this and do not make bad reports
Operator new, operator delete supplementary handouts
Overview of wavelet packet transform methods
PathMatchingResourcePatternResolver解析配置文件 资源文件
图论:拓扑排序
Constructing verb sources for relation extraction
LeetCode. 6115 count the number of ideal arrays
PHP save array to var file_ export、serialize
Opencv learning notes - remapping