当前位置:网站首页>Horrible bug records
Horrible bug records
2022-06-30 09:53:00 【weixin_ fifty-one million eight hundred and thirty-four thousan】
Horrible Bug Record
It's a combination \ File read and save \ Socket TCP \ Multithreading
A programming of
expect :
The picture is read in bytes , Then packaged as udp package , Send to the receiver , The receiving end saves .
Automatically create a file when saving , The file name is incremented , Guarantee not to repeat .
Bug:
Dead loop process
Occupy cup resources 25% about
Memory resources 26% about
Show in Task Manager But you can't force the program to close , Clicking to terminate the process will not respond
but Cut off the power supply
After the event :
Restart the computer , view folders ,
Action object That is, girlfriend photos , Be copied circularly 35215 Share .
Code :
client:
package org.ccdx.lsr.oop.client;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
/** * @Author Lishaoran * @Date 2022/1/21 Friday 16:16 * @Param IntelliJ IDEA * @Description //TODO Client emulation Server port number 10003 */
public class Client {
Scanner getKey = new Scanner(System.in);
Socket clientSocket;
OutputStream os;// Send stream
InputStream is;// Accept flow
public Client() throws IOException {
clientSocket = new Socket("127.0.0.1", 1000);
os = clientSocket.getOutputStream();
is = clientSocket.getInputStream();
}
public static void main(String[] args){
}
public void begin() throws IOException{
new Thread(new Runnable() {
// send out
@Override
public void run() {
byte[] B;
while (true){
B = new byte[1024];
try {
B = getKey.next().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
os.write(B);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
// receive
@Override
public void run() {
byte[] B2;
while (true){
B2 = new byte[1024];
try {
int len = is.read(B2);// take Data flow Read as byte form , This method reverses the number of itself read
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(" Client receive window :");
try {
System.out.println(new String(B2,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}).start();
// Accept server response
// int len = is.read(B);
// System.out.println(new String(B));
}
public void fileUpload(File f){
byte[] B = new byte[1024];
File[] files = f.listFiles();
for(File F : files)
try {
FileInputStream fileInputStream =new FileInputStream(F);
int len = fileInputStream.available();
os.write(len/1024);
System.out.println(" Transmitted file size "+len/1024);
while(is.read()!='T')// Received a message
;
System.out.println(" Receive transmission signal , Start transmission on ");
for(int i = 0 ; i<len/1024+1;i++) {
fileInputStream.read(B);
os.write(B);
}
System.out.println(" Transmission complete ");
fileInputStream.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
server:
package org.ccdx.lsr.oop.server;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;
/** * @Author Lishaoran * @Date 2022/1/21 Friday 16:15 * @Param IntelliJ IDEA * @Description //TODO */
public class Service {
Scanner getKey = new Scanner(System.in);
static int name=0;
ServerSocket serviceSocket;// Create socket to specify port
Socket cs;// Turn on port monitor
InputStream is;// Receive stream
OutputStream os;// Send stream
public Service() throws SocketException {
try {
serviceSocket = new ServerSocket();
} catch (IOException e) {
e.printStackTrace();
}
try {
serviceSocket.bind(new InetSocketAddress("127.0.0.1",1000));// binding IP and port
} catch (IOException e) {
e.printStackTrace();
}
serviceSocket.setSoTimeout(60000);// Set the listening time ms
}
public void begin(){
new Thread(new Runnable() {
// receive
@Override
public void run() {
byte[] B;
while (true){
B = new byte[1024];
try {
is.read(B);// take Data flow Read as byte form , This method reverses the number of itself read
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(" Server receive window :");
try {
System.out.println(new String(B,"UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
// send out
@Override
public void run() {
byte[] B2 = new byte[1024];
while (true){
try {
B2 = getKey.next().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
os.write(B2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void fileGet(String F) throws IOException {
try {
cs = serviceSocket.accept();
is = cs.getInputStream();
os = cs.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
new Thread(new Runnable() {
@Override
public void run() {
File f = new File(F + (name++));
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
int len = 0;
byte[] B = new byte[1024];
try {
FileOutputStream fileOutputStream = new FileOutputStream(f);
while (len == 0)
len = is.read();
System.out.println(" Received transfer size " + len);
byte b3 = 'T';
os.write(b3);
System.out.println(" Ready to start receiving ");
for (int i = 0; i < len + 1; i++) {
is.read(B);
fileOutputStream.write(B);
}
System.out.println(" End of reception ");
fileOutputStream.close();
cs.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
}
main:
package org.ccdx.lsr.oop.test;
import org.ccdx.lsr.oop.client.Client;
import org.ccdx.lsr.oop.server.Service;
import java.io.File;
import java.io.IOException;
import java.net.SocketException;
/** * @Author Lishaoran * @Date 2022/1/22 Saturday 9:06 * @Param IntelliJ IDEA * @Description //TODO */
public class Test {
public static void main(String[] args) throws IOException {
String fNew = "D:\\JAVA_SE\\bin_my\\workspace\\20220122- Network programming - Upload files \\src\\img";
String fOrg = "C:\\Users\\86198\\Pictures\\img";
new Thread(new Runnable() {
@Override
public void run() {
try {
Service s = new Service();
s.fileGet(fNew);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
Client c = new Client();
c.fileUpload(new File(fOrg));
}
}
边栏推荐
- Torch learning summary
- How do databases go to the enterprise cloud? Click to view the answer
- 11. customize hooks
- thrift简单使用
- ABAP-时间函数
- Clickhouse installation (quick start)
- Read the difference and connection between hyperfusion and private cloud
- What is the difference between ZigBee, Bluetooth and WiFi (copy and reprint)
- Distributed session
- 抽象类和接口
猜你喜欢

Practice of super integration and transformation of core production business of public funds

Pytorch graduate warm LR installation

MySQL-- Entity Framework Code First(EF Code First)

IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry

仿照微信Oauth2.0接入方案

Self service terminal handwritten Chinese character recognition input method library tjfink introduction

单片机 MCU 固件打包脚本软件

Cloud native database

目标检测yolov5开源项目调试

MySQL explain
随机推荐
IDC released the report on China's software defined storage and hyper convergence market in the fourth quarter of 2020, and smartx hyper convergence software ranked first in the financial industry
Flutter 中的 ValueNotifier 和 ValueListenableBuilder
ABAP time function
Solution to pychart's failure in importing torch package
Why won't gold depreciate???
prometheus 监控之 ntp_exporter
Golang magic code
小程序开发踩坑之旅
Based on svelte3 X desktop UI component library svelte UI
MySQL优化
JWT expiration processing - single token scheme
How can we have high performance and simple agility in the enterprise cloud on oracle?
Properties of string
[new book recommendation] DeNO web development
ReturnJson,让返回数据多一些自定义数据或类名
Experience of an acmer
Techtarget: Interpretation of the basic concept of super fusion cloud
Distributed ID
Principle and implementation of small program hand-held bullet screen (uni APP)
Terminal -- Zsh of terminal three swordsmen