恐怖的Bug记录

恐怖的Bug记录,第1张

恐怖的Bug记录 恐怖的Bug记录

这是一个结合文件读取和保存套接字 TCP 多线程

的一个编程

预期:

照片被读取为字节形式,然后打包为udp包 ,发送到接收端,接收端保存。

保存的时候自动创建文件,文件名递增,保证不重复。

Bug:

死循环进程

占用cup资源25%左右

内存资源26%左右

在任务管理器显示 但是无法强制关闭程序,点击终止进程不会有反应

无奈 切断电源

事后:

重启电脑,查看文件夹,

*** 作对象 即女友照片,被循环复制35215份。

代码: client:
package org.ccdx.lsr.oop.client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;


public class Client {
    Scanner getKey = new Scanner(System.in);
    Socket clientSocket;
    OutputStream os;//发送流
    InputStream is;//接受流

    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() {//发送
            @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() {//接收
            @Override
            public void run() {
                byte[] B2;
                while (true){
                    B2 = new byte[1024];
                    try {
                        int len = is.read(B2);//将 数据流 读取为 byte形式, 此方法反回读取的自己个数
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.print("客户端接收窗口:");
                    try {
                        System.out.println(new String(B2,"UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();


//        接受服务器回应
//        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("已发送传输文件大小"+len/1024);
            while(is.read()!='T')//收到消息
                ;
            System.out.println("收到传输信号,号开始传输");
            for(int i = 0 ; i 
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;


public class Service {
    Scanner getKey = new Scanner(System.in);
    static int name=0;
    ServerSocket serviceSocket;//创建套接字指定端口
    Socket cs;//开启port监听
    InputStream is;//接收流
    OutputStream os;//发送流
    public Service() throws SocketException {
        try {
            serviceSocket = new ServerSocket();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            serviceSocket.bind(new InetSocketAddress("127.0.0.1",1000));//绑定IP和port
        } catch (IOException e) {
            e.printStackTrace();
        }
        serviceSocket.setSoTimeout(60000);//设置监听时间 ms

    }

    public void begin(){
            new Thread(new Runnable() {//接收
                @Override
                public void run() {
                    byte[] B;
                    while (true){
                        B = new byte[1024];
                        try {
                            is.read(B);//将 数据流 读取为 byte形式, 此方法反回读取的自己个数
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        System.out.print("服务器接收窗口:");
                        try {
                            System.out.println(new String(B,"UTF-8"));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();

            new Thread(new Runnable() {//发送
                @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("收到传输大小" + len);
                        byte b3 = 'T';
                        os.write(b3);
                        System.out.println("已就绪开始接收");
                        for (int i = 0; i < len + 1; i++) {
                            is.read(B);
                            fileOutputStream.write(B);
                        }
                        System.out.println("接收完毕");
                        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;


public class Test {
    public static void main(String[] args) throws IOException {
        String fNew = "D:\JAVA_SE\bin_my\workspace220122-网络编程-文件上传\src\img";
        String fOrg = "C:\Users198\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));

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5713020.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存