首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java Server Android客户端Wifi发送文件,套接字错误

Java Server Android客户端Wifi发送文件,套接字错误
EN

Stack Overflow用户
提问于 2013-10-08 00:09:26
回答 1查看 1K关注 0票数 0

我在从服务器(桌面应用程序)向客户端(Android)发送文件时遇到了一些问题。

在发送文件之前,服务器发送元数据,如文件大小、名称等。

服务端发送方式:

代码语言:javascript
复制
private void sendPdfData(OutputStream os, File file) throws IOException {

    os.flush();
    FileInputStream fis = new FileInputStream(file);
    byte[] buffor = new byte[1024];
    long count = 0L;
    long size = file.length();
    int current = 0;   

    while (count < size) {  
        current = fis.read(buffor, 0, buffor.length);
        os.write(buffor, 0, current);
        count += current;
    }

    fis.close();
    os.flush();
}

客户端接收方法:

代码语言:javascript
复制
@Override
protected String doInBackground(Void... params) {

    String pathToPdf = "";

    if (pdf.getLength() > 0) {

        InputStream is;

        try {
            byte b = 0;
            clientSocket.getOutputStream().write(b);
            is = clientSocket.getInputStream();
            pathToPdf = pathToExternalStorageFolder+pdf.getMeta().getName();
            pathToPdf = pathToPdf.replace(".\\", "/");
            pathToPdf = pathToPdf.replace("\\", "/");
            int size = pdf.getLength();
            byte[] buffor = new byte[1024];
            int current = 0;
            int count = 0;

            if (pdf.getMeta() != null) {
                FileOutputStream fos = new FileOutputStream(pathToPdf);

                while (count < size) {
                    current = is.read(buffor, 0, buffor.length);
                    fos.write(buffor, 0, current);
                    count += current;
                }
                fos.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }    
    return pathToPdf;
}

发送文件时出现一些随机错误:

代码语言:javascript
复制
java.net.SocketException: Software caused connection abort: socket write error
java.net.SocketException: Connection reset by peer: socket write error

File size: 2317679
Sended: 44032

更新08.09.2013

我创建桌面客户端应用程序来检查服务器应用程序。当我在NetBeans中运行服务器和客户端时,一切都运行得很好,我使用接口地址(而不是lopback)。当我从jar运行客户端时,我有问题:文件列表是空的,但在服务器端不是空的,当我从android连接时,我会得到文件列表wtfigo - magic。

EN

回答 1

Stack Overflow用户

发布于 2013-10-08 00:33:10

当本地网络系统中止连接时,可能会发生此错误,例如,当WinSock在数据重新传输失败后关闭已建立的连接时(接收方从不确认在数据流套接字上发送的数据)。

http://msdn.microsoft.com/en-us/library/ms832256.aspx

https://forums.oracle.com/thread/1691330

一个线程中的Socket.close(),而另一个线程正在从该套接字读取或写入该套接字时,会导致在该套接字关闭后抛出异常。

尝试将autoReconnect=true添加到jdbc连接字符串

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19229573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档