当前位置:网站首页>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));
}
}
边栏推荐
- thrift简单使用
- prometheus 监控之 ntp_exporter
- Differences and relationships among hyper convergence, software defined storage (SDS), distributed storage and server San
- Net framework system requirements
- Configuring MySQL for error reporting
- Recommend a very easy-to-use network communication framework HP socket
- 11.自定义hooks
- Financial private cloud infrastructure scheme evaluation (Architecture and storage)
- Clickhouse installation (quick start)
- MySQL index and data storage structure foundation
猜你喜欢
Recommend a very easy-to-use network communication framework HP socket

Abstract classes and interfaces

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
![[Ubuntu redis installation]](/img/66/d8054ae89007180b317641cf92d1cc.png)
[Ubuntu redis installation]

【AGC】构建服务3-认证服务示例

Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)

Distributed ID

Terminal -- Zsh of terminal three swordsmen

Techtarget: Interpretation of the basic concept of super fusion cloud

JVM tuning tool introduction and constant pool explanation
随机推荐
Difference between bow and cbow
Redis docker master-slave mode and sentinel
Returnjson, which allows more custom data or class names to be returned
训练一个图像分类器demo in PyTorch【学习笔记】
prometheus 监控之 ntp_exporter
Design of mfc+mysql document data management system based on VS2010
3.集成eslint、prettier
DDD interview
Redis docker 主从模式与哨兵sentinel
Forrester senior analyst: five important trends in the development of the hyper convergence market
Why won't gold depreciate???
UltraEdit delete empty line method
Tclistener server and tcpclient client
Eight sorts (I)
Golang magic code
Create thread pool demo
JVM garbage collector G1 & ZGC details
NTP of Prometheus monitoring_ exporter
八大排序(二)
MySQL-- Entity Framework Code First(EF Code First)