簡體   English   中英

SocketException:sendto失敗:EBADF(錯誤的文件描述符)

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

我嘗試通過Socket從Android手機發送數據。 我將pdf文件發送到打印機進行打印。 在Java中,它可以正常工作,但是Kotlin拋出SocketException:sendto失敗:EBADF(錯誤文件描述符),但是使用Kotlin代碼,打印機可以進行任何打印。

完整堆棧

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)

代碼,引發異常:

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)
            }
        }

我相同的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);
                    }
                }
            }
        });

我的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)
}

我將數據寫入文件,以便:

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

我解決了我的問題。 為了將數據寫入Kotlin中的OutputStream,我使用了:

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

在此代碼段之后,運算符.use { }關閉了我的OutputStream,但隨后我再次使用它。這是出現異常的原因。

我更改了將數據寫入OutputStream的操作,因此:

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

它可以正常工作!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM