[java新手] IO和网络编程的问题

做网络编程的练习,用socket连接 传输文件,目的是把一个文件传到服务端保存下来
测试结果完全正常,文件也传输完成,但是catch捕捉到了异常???!
当运行服务端时候客户端catch报错,当运行客户端时候服务端catch报错 0.0
代码如下

修改了下代码,我把输出换成e.printStackTrace()打印了,还是这样 结果是实现了的,测试通过,但是有捕获到异常



import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

public class 网络编程_上传 {
    public static String ClientInteAddress = "127.0.0.1";
    public static String str = "9000";
    public static String ServerFileAddress = "E:\\111.xls";
    public static String ClientFileAddress = "D:\\15916\\下载\\111.xls";

    @Test//服务器
    public void server() {
        ServerSocket sss = null;
        Socket sk = null;
        InputStream is = null;
        BufferedInputStream bis = null;
        FileOutputStream fos = null;

        try {
            sss = new ServerSocket(new Integer(str));
            sk = sss.accept();
            is = sk.getInputStream();
            bis = new BufferedInputStream(is);
            fos = new FileOutputStream(ServerFileAddress);

            int buffi = 0;
            byte [] by = new byte[1024];

            while ((buffi = bis.read(by)) != -1) {
                fos.write(by);
            }

            fos.close();
            bis.close();
            sk.close();
            sss.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    if (fos != null) {fos.close();}
                    if (bis != null) bis.close();
                    if (sk != null) sk.close();
                    if (sss != null) sss.close();
                } catch (IOException e) {
                    System.out.println("服务器报错啦!:关闭流出错");
                }
            }

    }

    @Test//客户端
    public void client() {
        Socket sk = null;
        OutputStream os = null;
        BufferedOutputStream bos = null;
        FileInputStream fis = null;

        try {
            sk = new Socket(InetAddress.getByName(ClientInteAddress), new Integer(str));
            os = sk.getOutputStream();
            bos = new BufferedOutputStream(os);
            fis = new FileInputStream(ClientFileAddress);

            int buffi = 0;
            byte [] by = new byte[1024];
            while ((buffi = fis.read(by)) != -1) {
                bos.write(by);
            }

            fis.close();
            bos.close();
            sk.close();
        } catch (NumberFormatException | IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (sk != null) sk.close();
                if (fis != null) fis.close();
                if (bos != null) bos.close();

            } catch (IOException e) {
                System.out.println("客户端报错啦!:关闭流出错");
            }
        }

    }

}

那个捕捉的IO异常怎么来的,我不清楚。而且在我把catch内容换为输出之前,报错有:JVM绑定错误(socket赋值行) 连接错误

System.out.println(e);

输出下具体什么错误。

程序写的没问题。 但是那个端口在我的环境下有问题,我换成了9000端口,测试通过。建议更改端口测试一下,如果有问题M我

看下端口有没有占用,或者防火墙有没有禁止这个端口。

1.首先不要怕报错,报错是找出问题的关键
2.看看你报的是什么错
3.然后根据相应的错误排查问题
4.你这里socketServer接收,可以开一个线程,单独处理下载文件的任务,这样可以确定错误来自处理下载流这一部分还是socketServer这里有问题。

try to close the write stream first and set the reference to null just in case,
bos.close();
bos=null;
fis.close();
fix=null;
sk.close();
sk = null;
Same here,
if (bos != null) bos.close();
if (fis != null) fis.close();
if (sk != null) sk.close();

包名类名全英文建议,试试

不要用中文定义类,容易出现不能解决的问题,请认真对待java