简体   繁体   English

软件导致连接中止:套接字写入错误

[英]Software caused connection abort: socket write error

I have a case where the client after establishing a connection with the server, receives one file and when the same connection was used ( persistent ) I end up getting this error mentioned above. 我有一个案例,客户端与服务器建立连接后,收到一个文件,当使用相同的连接(持久)时,我最终得到上述错误。 Below is the implemented code: 以下是实现的代码:

Scanner in = new Scanner(file);

this.clientSocket = new Socket(this.host,this.port);

this.os = new DataOutputStream(this.clientSocket.getOutputStream());

this.is = this.clientSocket.getInputStream();

while(in.hasNextLine()){
  newFile = in.nextLine();
  System.out.println(newFile);

  this.os.writeBytes(newFile + '\n');
  this.os.flush();
  scanFileList();
  writeFile();

 }

and the server side implementation is: 和服务器端实现是:

 final class HttpRequest implements Runnable {
   final static String CRLF = "\r\n";
   Socket socket;
   static String dir;
   BufferedOutputStream outToClient = null;
   // Constructor
   public HttpRequest(Socket socket) throws Exception {
      this.socket = socket;
      dir = "C:\\Users\\";
 }


      // Implement the run() method of the Runnable interface.
 public void run() {
     try {

       // Get a reference to the socket's input and output streams.
       InputStream is = socket.getInputStream(); 
       outToClient = new BufferedOutputStream(socket.getOutputStream());
       processRequest(is,outToClient);
         } catch (Exception e) {
               System.out.println(e);
         }
 }

private void processRequest(InputStream is,BufferedOutputStream os) throws Exception {

    // Set up input stream filters.
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    // Get the request line of the HTTP request message.
    String fileName = br.readLine();



    // Prepend a "." so that file request is within the current directory.
    System.out.println(fileName);
    // Open the requested file.
    File myFile = null ;

    boolean fileExists = true ;

    myFile = new File(dir + fileName);

    FileInputStream fis = null ;
    try {
      fis = new FileInputStream(dir + fileName);
      System.out.println(fis);
    } catch (FileNotFoundException e) {
        fileExists = false ;
    }

    // Debug info for private use
    System.out.println("Incoming!!!");


   // Send the entity body.
   if (fileExists) {
    sendBytes(myFile, os);
    //fis.close();
   } 
   // Close streams and socket.
   is.close();
   os.close();
   br.close();
   socket.close();
   }

   private static void sendBytes(File myFile, 
    BufferedOutputStream os) throws Exception {
         // Construct a 1K buffer to hold bytes on their way to the socket.
         byte[] mybytearray = new byte[(int) myFile.length()];


         FileInputStream fis = null;
         // Copy requested file into the socket's output stream.
         try {
           fis = new FileInputStream(myFile);
         } catch (FileNotFoundException ex) {
            // Do exception handling
         }
         BufferedInputStream bis = new BufferedInputStream(fis);

         try {
           bis.read(mybytearray, 0, mybytearray.length);
           os.write(mybytearray, 0, mybytearray.length);
           os.flush();

           // File sent, exit the main method
           return;
         } catch (IOException ex) {
         // Do exception handling
           }
        }

   }

The error happens when the program on the client side tries to write to the server through: this.os.writebytes(newFile + /n); 当客户端程序尝试通过以下方式写入服务器时发生错误:this.os.writebytes(newFile + / n);

  Testfile01.bmp

  writing

  saved

  Testfile02.bmp

Exception in thread "main" java.net.SocketException: Socket closed
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.writeBytes(Unknown Source)
at TCPClientPersistentNp.openSocket(TCPClientPersistentNp.java:53)
at TCPClient.main(TCPClient.java:66)

What is this doing in your code? 你在代码中做了什么?

is.close();
os.close();
br.close();
socket.close();

You are explictly closing everything after processing every request? 处理完每个请求后,你是否明确地关闭了所有内容? And this is, you say, a persistent connection implementation? 你说,这是一个持久的连接实现?

I'm not going to try to read your code in detail (see comment above ...), but there is clearly something very strange / wrong about it. 我不打算详细阅读你的代码(见上面的评论......),但显然有一些非常奇怪/错误的事情。

Put simply, if you are going to talk to an HTTP-based server, you can't just open a socket and write stuff. 简而言之,如果您要与基于HTTP的服务器通信,则不能只打开套接字并编写内容。 Your client has to create well-formed HTTP requests, and process the HTTP responses that come back. 您的客户端必须创建格式良好的HTTP请求,并处理返回的HTTP响应。

The exception on the client-side is happening because the server side ... in fact YOUR CODE ... has closed the connection at the other end. 客户端的例外正在发生,因为服务器端......实际上你的代码......已经关闭了另一端的连接。

暂无
暂无

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

相关问题 jvisualvm:软件导致连接中止:套接字写入错误 - jvisualvm: Software caused connection abort: socket write error 软件导致连接异常终止:写入DataOutputStream时套接字写入错误 - Software caused connection abort: socket write error while writing to DataOutputStream “软件导致连接中止:套接字写入错误”的官方原因 - Official reasons for "Software caused connection abort: socket write error" 在FTP中存储ObjectOutputStream导致java.net.SocketException:软件导致连接中止:套接字写入错误 - Store ObjectOutputStream in FTP causes java.net.SocketException: Software caused connection abort: socket write error java.net.SocketException:软件导致连接中止:重新提交请求时套接字写入错误 - java.net.SocketException: Software caused connection abort: socket write error when resubmitting the request javax.servlet.jsp.JspException:无法插入页面…软件导致连接中止:套接字写入错误 - javax.servlet.jsp.JspException: Can't insert page … Software caused connection abort: socket write error 通过串行端口java.net.SocketException的通信:软件导致连接中止:套接字写入错误 - Communication through serial port, java.net.SocketException: Software caused connection abort: socket write error Maven Web项目在eclipse(tomcat 7)中运行错误:错误:无法调用Tomcat管理器:软件导致连接中止:套接字写入错误 - The maven web project runs error in eclipse(tomcat 7):error:Cannot invoke Tomcat manager: Software caused connection abort: socket write error 软件导致连接中止:即使从最近的列表中清除了该应用程序,套接字写入错误(它在与服务不同的线程中运行) - Software caused connection abort: socket write error on clearing the app from recent list even (it running in a different thread from a service) Java错误 - 软件导致连接中止:recv失败 - Java error - Software caused connection abort: recv failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM