2014-12-03 73 views
0

我已经创建了一个简单的客户端/服务器程序,其中客户端从命令行参数中获取文件。客户端然后将文件发送到服务器,在那里使用GZIP进行压缩并发送回客户端。线程“主”异常java.net.SocketException:连接重置 -

服务器程序首次运行时很好,并且不会产生错误,但运行客户端后出现错误。

我得到一个错误,说连接重置,我试过很多不同的端口,所以我想知道如果我的代码有问题或者我关闭了流的时间?

任何帮助将不胜感激!

编辑 - 对两个程序进行了更改。

客户:

import java.io.*; 
import java.net.*; 

//JZip Client 

public class NetZip { 

    //Declaring private variables. 
    private Socket socket = null; 
    private static String fileName = null; 
    private File file = null; 
    private File newFile = null; 
    private DataInputStream fileIn = null; 
    private DataInputStream dataIn = null; 
    private DataOutputStream dataOut = null; 
    private DataOutputStream fileOut = null; 


    public static void main(String[] args) throws IOException { 
    try { 
     fileName = args[0]; 
     } 
    catch (ArrayIndexOutOfBoundsException error) { 
     System.out.println("Please Enter a Filename!"); 
    } 
    NetZip x = new NetZip(); 
    x.toServer(); 
    x.fromServer(); 

    } 

    public void toServer() throws IOException{ 
    while (true){ 
    //Creating socket 
     socket = new Socket("localhost", 4567); 
     file = new File(fileName); 

     //Creating stream to read from file. 
     fileIn = new DataInputStream(
      new BufferedInputStream(
       new FileInputStream(
        file))); 

     //Creating stream to write to socket. 
     dataOut = new DataOutputStream(
      new BufferedOutputStream(
       socket.getOutputStream())); 

     byte[] buffer = new byte[1024]; 

      int len; 
      //While there is data to be read, write to socket. 
     while((len = fileIn.read(buffer)) != -1){ 
      try{ 
      System.out.println("Attempting to Write " + file 
       + "to server."); 
      dataOut.write(buffer, 0, len); 
      } 
      catch(IOException e){ 
      System.out.println("Cannot Write File!"); 
      } 
      } 
     fileIn.close(); 
     dataOut.flush(); 
     dataOut.close(); 

     } 

    } 
    //Read data from the serversocket, and write to new .gz file. 
    public void fromServer() throws IOException{ 

    dataIn = new DataInputStream(
      new BufferedInputStream(
        socket.getInputStream())); 

    fileOut = new DataOutputStream(
      new BufferedOutputStream(
       new FileOutputStream(
        newFile))); 

    byte[] buffer = new byte[1024]; 

     int len; 
     while((len = dataIn.read(buffer)) != -1){ 
      try { 
      System.out.println("Attempting to retrieve file.."); 
      fileOut.write(buffer, 0, len); 
      newFile = new File(file +".gz"); 

      } 
      catch (IOException e){ 
      System.out.println("Cannot Recieve File"); 
      } 
     } 
     dataIn.close(); 
     fileOut.flush(); 
     fileOut.close(); 
     socket.close(); 

    } 


} 

服务器:

import java.io.*; 
import java.net.*; 
import java.util.zip.GZIPOutputStream; 

//JZip Server 

public class ZipServer { 

    private ServerSocket serverSock = null; 
    private Socket socket = null; 
    private DataOutputStream zipOut = null; 
    private DataInputStream dataIn = null; 

    public void zipOut() throws IOException { 

    //Creating server socket, and accepting from other sockets. 
    try{ 
    serverSock = new ServerSocket(4567); 
    socket = serverSock.accept(); 
    } 
    catch(IOException error){ 
     System.out.println("Error! Cannot create socket on port"); 
    } 

    //Reading Data from socket 
    dataIn = new DataInputStream(
      new BufferedInputStream(
        socket.getInputStream())); 

    //Creating output stream. 
    zipOut= new DataOutputStream(
     new BufferedOutputStream(
      new GZIPOutputStream(
       socket.getOutputStream()))); 

    byte[] buffer = new byte[1024]; 

     int len; 
     //While there is data to be read, write to socket. 
     while((len = dataIn.read(buffer)) != -1){ 
      System.out.println("Attempting to Compress " + dataIn 
       + "and send to client"); 
      zipOut.write(buffer, 0, len); 
     } 
     dataIn.close(); 
     zipOut.flush(); 
     zipOut.close(); 
     serverSock.close(); 
     socket.close(); 
    } 

    public static void main(String[] args) throws IOException{ 
    ZipServer run = new ZipServer(); 
    run.zipOut(); 

    } 

} 

错误消息:

Exception in thread "main" java.net.SocketException: Connection reset 
    at java.net.SocketInputStream.read(SocketInputStream.java:196) 
    at java.net.SocketInputStream.read(SocketInputStream.java:122) 
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235) 
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275) 
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334) 
    at java.io.DataInputStream.read(DataInputStream.java:100) 
    at ZipServer.<init>(ZipServer.java:38) 
    at ZipServer.main(ZipServer.java:49) 
+0

您不需要'toServer()'方法中的'while(true)'循环。在close()是多余的之前,你不需要'DataOutputStream'或'DataInputStream.' flush()'。 – EJP 2014-12-04 00:23:33

+0

@EJP谢谢,我摆脱了所有的冲洗和数据流。但它似乎仍然没有工作?值得一提的是,我正在运行Ubuntu并使用Eclipse。我没有编译器错误或警告标志,错误是一样的。 它在错误的底部说: at ZipServer.zipOut(ZipServer.java:38) at ZipServer.main(ZipServer.java:53) which which to; ((len = dataIn.read(buffer))!= -1){ , \t run.zipOut(); (在主要方法中) 难道这与这些有关吗? – Rastamouse 2014-12-04 00:36:42

+0

这些评论是评论,而不是答案。也不要因为你第一次没有得到答案而转发问题。 – EJP 2014-12-04 00:38:18

回答

1

首先,因为客户端失败和发送任何数据之前结束,所以所发生的错误 连接在服务器想要读取时关闭。因为您指定的文件对象未使用的局部变量(没有你的编译器没有发出警告?)

public File file = null; 
public File newFile = null; 

public static void main(String[] args) throws IOException { 
try { 
    String fileName = args[0]; 
    File file = new File(fileName); 
    File newFile = new File(file +".gz"); 
    } 
catch (ArrayIndexOutOfBoundsException error) { 
    System.out.println("Please Enter a Filename!"); 
} 

但在你toServer方法使用类变量文件作为参数的FileInputStream和这个变量为空时
错误这会导致程序结束的错误。

第二,如果你完成了写入OutputStream的,你应该叫

socket.shtdownOutput(); 

否则,服务器尝试读取,直到发生超时。

+0

好的,在您编辑之后,我的回答的第一部分已经过时了:) – Joachim 2014-12-04 00:12:34

+0

如果他关闭套接字,则不需要调用shutdownOutput(),并且不会因为未设置而发生超时一。 – EJP 2014-12-04 00:24:05

+0

@EJP关闭套接字是否解决了问题?我不确定是否应该关闭连接,因为服务器必须继续监听客户端。我遇到同样的问题,我的服务器很简单,它接受客户端应用程序,并返回调用一个方法,向客户端返回强大的消息,因此通信正常工作。一个奇怪的事情,但如果我关闭客户端,服务器也死了,并拉出连接重置错误。我的服务器有一个while循环,一直监听客户端连接,如果发生任何连接,则返回字符串 – crakama 2018-01-18 11:09:22

相关问题