简体   繁体   English

SocketException:sendto失败:EBADF(错误的文件描述符)

[英]SocketException: sendto failed: EBADF (Bad file descriptor)

I try to send data by Socket from Android phone. 我尝试通过Socket从Android手机发送数据。 I send pdf file to printer for printing. 我将pdf文件发送到打印机进行打印。 In Java it work correctly, but Kotlin throws SocketException: sendto failed: EBADF (Bad file descriptor), but with Kotlin code, printer whatever printing. 在Java中,它可以正常工作,但是Kotlin抛出SocketException:sendto失败:EBADF(错误文件描述符),但是使用Kotlin代码,打印机可以进行任何打印。

Full stackTrace: 完整堆栈

java.net.SocketException: sendto failed: EBADF (Bad file descriptor)
    at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542)
    at libcore.io.IoBridge.sendto(IoBridge.java:511)
    at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500)
    at java.net.PlainSocketImpl.-wrap1(PlainSocketImpl.java)
    at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
    at java.io.OutputStream.write(OutputStream.java:82)
    at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)
    at com.example.print.printerserver.connectors.OutputHelper.writeFooter(OutputHelper.java:22)
    at com.example.print.printerserver.connectors.PrinterConnector.fillPJL(PrinterConnector.kt:30)
    at com.example.print.printerserver.connectors.PrinterConnector.access$fillPJL(PrinterConnector.kt:8)
    at com.example.print.printerserver.connectors.PrinterConnector$print$1.subscribe(PrinterConnector.kt:17)
    at io.reactivex.internal.operators.single.SingleCreate.subscribeActual(SingleCreate.java:39)
    at io.reactivex.Single.subscribe(Single.java:2779)
    at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
Caused by: android.system.ErrnoException: sendto failed: EBADF (Bad file descriptor)
    at libcore.io.Posix.sendtoBytes(Native Method)
    at libcore.io.Posix.sendto(Posix.java:211)
    at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:278)
    at libcore.io.IoBridge.sendto(IoBridge.java:509)

Code, which throws exception: 代码,引发异常:

return Single.create<String> { emitter ->
            try {
                Socket(ip, port).use { socket ->
                    DataOutputStream(socket.getOutputStream()).use { output ->
                        fillPJL(output, filename, paperSize, copies)
                        emitter.onSuccess("Success printing")
                    }
                }
            } catch (e: Exception) {
                emitter.onError(e)
            }
        }

My identical Java example (With PJL commands): 我相同的Java示例(使用PJL命令):

return Single.create(new SingleOnSubscribe<String>() {
            @Override
            public void subscribe(SingleEmitter<String> emitter) throws Exception {
                Socket socket = null;
                DataOutputStream out = null;
                FileInputStream inputStream = null;
                try {
                    socket = new Socket(printerIP, printerPort);
                    out = new DataOutputStream(socket.getOutputStream());
                    DataInputStream input = new DataInputStream(socket.getInputStream());
                    inputStream = new FileInputStream(file);
                    byte[] buffer = new byte[3000];

                    final char ESC = 0x1b;
                    final String UEL = ESC + "%-12345X";
                    final String ESC_SEQ = ESC + "%-12345\r\n";

                    out.writeBytes(UEL);
                    out.writeBytes("@PJL \r\n");
                    out.writeBytes("@PJL JOB NAME = '" + filename + "' \r\n");
                    out.writeBytes("@PJL SET PAPER=" + paperSize.name());
                    out.writeBytes("@PJL SET COPIES=" + copies);
                    out.writeBytes("@PJL ENTER LANGUAGE = PDF\r\n");
                    while (inputStream.read(buffer) != -1)
                        out.write(buffer);
                    out.writeBytes(ESC_SEQ);
                    out.writeBytes("@PJL \r\n");
                    out.writeBytes("@PJL RESET \r\n");
                    out.writeBytes("@PJL EOJ NAME = '" + filename + "'");
                    out.writeBytes(UEL);

                    out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                    emitter.onError(e);
                } finally {
                    try {
                        if (inputStream != null)
                            inputStream.close();
                        if (out != null)
                            out.close();
                        if (socket != null)
                            socket.close();
                        emitter.onSuccess("Succ");
                    } catch (IOException e) {
                        e.printStackTrace();
                        emitter.onError(e);
                    }
                }
            }
        });

My fillPJL function: 我的fillPJL函数:

private fun fillPJL(output: DataOutputStream?, filename: String, paperSize: PaperSize, copies: Int) {
    val ESC = 0x1b.toChar()
    val UEL = ESC + "%-12345X"
    val ESC_SEQ = ESC + "%-12345\r\n"
    output?.writeBytes(UEL)
    output?.writeBytes("@PJL \r\n")
    output?.writeBytes("@PJL JOB NAME = '$filename' \r\n")
    output?.writeBytes("@PJL SET PAPER=" + paperSize.name)
    output?.writeBytes("@PJL SET COPIES=" + copies)
    output?.writeBytes("@PJL ENTER LANGUAGE = PDF\r\n")
    writeData(output)
    output?.writeBytes(ESC_SEQ)
    output?.writeBytes("@PJL \r\n")
    output?.writeBytes("@PJL RESET \r\n")
    output?.writeBytes("@PJL EOJ NAME = '\$filename'")
    output?.writeBytes(UEL)
}

I writing data to file so: 我将数据写入文件,以便:

fun writeData(output: DataOutputStream) {
    file.inputStream().use { input ->
          outputStream.use { it.write(input.readBytes()) }
    }
}

I solved my problem. 我解决了我的问题。 For writing data to OutputStream in Kotlin, I used: 为了将数据写入Kotlin中的OutputStream,我使用了:

file.inputStream().use { input ->
    outputStream.use { it.write(input.readBytes()) }
}

Operator .use { } closed my OutputStream after this snippet, but then I used it again.It was a reason of exceptions. 在此代码段之后,运算符.use { }关闭了我的OutputStream,但随后我再次使用它。这是出现异常的原因。

I changed writing data to OutputStream so: 我更改了将数据写入OutputStream的操作,因此:

file.inputStream().use { it.copyTo(outputStream, bufferSize = BUFFER) }

And it works correctly!!! 它可以正常工作!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.net.SocketException:recvfrom失败:EBADF(错误的文件描述符)Android - java.net.SocketException: recvfrom failed: EBADF (Bad file descriptor) Android java.io.IOException:读取失败:EBADF(错误的文件描述符) - java.io.IOException: read failed: EBADF (Bad file descriptor) RandomAccessFile.write: EBADF(坏文件描述符) - RandomAccessFile.write: EBADF (Bad file descriptor) Android Java IOException从文件描述符/输入流中读取失败的EBADF(错误文件编号) - Android Java IOException read failed EBADF (Bad File Number) From File descriptor/Input Stream IOException: write failed: EBADF (Bad file number) - IOException: write failed: EBADF (Bad file number) JRuby嵌入问题:放入失败,出现RaiseException:(Errno :: EBADF)错误的文件描述符 - JRuby Embedding prob: Puts appears to fails with RaiseException: (Errno::EBADF) Bad file descriptor 从InputStream读取时SocketException错误的文件描述符 - SocketException bad file descriptor when reading from InputStream JRuby-方法“ eval”的第二次调用给出(Errno :: EBADF)错误的文件描述符 - JRuby - second call of method “eval” gives (Errno::EBADF) Bad file descriptor 使用Release APK中的Intent.ACTION_OPEN_DOCUMENT读取大文本文件时,IOException EBADF(错误文件描述符) - IOException EBADF (Bad file descriptor) when reading large text files using Intent.ACTION_OPEN_DOCUMENT in Release APK Android java.io.IOException:写入失败:EBADF(错误的文件号) - Android java.io.IOException: write failed: EBADF (Bad file number)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM