0

I have a problem when sending files using sockets. The problem is that on the target host, the files I send are copied to the contents of the first file (including the file name and content of the other files) and not as separate files. For my test case I send only two files.

Local host:

public void sendFilesToServer(List<String> compFiles) throws IOException {

    Socket client = new Socket("127.0.0.1", 5000);

    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    int in;
    byte[] byteArray;

    DataOutputStream dos = new DataOutputStream(
            client.getOutputStream());

    //numero de archivos a enviar
    dos.writeInt(2);
    dos.flush();
    bos = new BufferedOutputStream(client.getOutputStream());

    for(String file : compFiles){
        // Fichero a transferir
        final String filename = file;

        try {
            final File localFile = new File(filename);
            bis = new BufferedInputStream(new FileInputStream(localFile));              
            dos.writeUTF(localFile.getName());
            dos.flush();
            byteArray = new byte[8192];
            while ((in = bis.read(byteArray)) != -1) {
                bos.write(byteArray, 0, in);
            }

            bos.flush();

        } catch (Exception e) {
            System.err.println(e);
        }
    }
    dos.close();
    bos.close();
    client.close();

}

target host:

public void escucha(int puerto) throws IOException {
    ServerSocket server = null;
    Socket connection;

    BufferedInputStream bis;
    BufferedOutputStream bos;

    byte[] receivedData;
    int in;
    String file;

    try {
        server = new ServerSocket(puerto);
        while (true) {
            connection = server.accept();

            receivedData = new byte[1024];
            DataInputStream dis = new DataInputStream(
                    connection.getInputStream());

            // recibimos la cantidad de ficheros que recibiremos
            int q = dis.readInt();

            for (int i = 0; i < q; i++) {
                bis = new BufferedInputStream(connection.getInputStream());

                file = dis.readUTF();
                file = "C:\\targetFolder" + File.separator
                        + file.substring(file.indexOf('\\') + 1, file.length());


                bos = new BufferedOutputStream(new FileOutputStream(file));
                while ((in = bis.read(receivedData)) != -1) {
                    bos.write(receivedData, 0, in);
                }
                bos.flush();
            }

            dis.close();
            server.close();

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        server.close();
    }
}

in the second iteration of the for occurs: java.io.EOFException

12:54:19,723 ERROR [stderr] (http-localhost-127.0.0.1-8082-6) java.io.EOFException
12:54:19,724 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)

12:54:19,725 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUTF(DataInputStream.java:589)

12:54:19,726 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.io.DataInputStream.readUTF(DataInputStream.java:564)

12:54:19,727 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.tfc.websvn.transfer.socket.recibefichero.ServidorRecibeFichero.escucha(ServidorRecibeFichero.java:44)

12:54:19,728 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.tfc.websvn.transfer.servlet.InitSocketServlet.doGet(InitSocketServlet.java:41)

12:54:19,728 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)

12:54:19,729 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

12:54:19,730 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

12:54:19,731 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

12:54:19,732 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)

12:54:19,733 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)

12:54:19,734 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)

12:54:19,735 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,736 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)

12:54:19,737 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,739 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)

12:54:19,739 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,740 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)

12:54:19,742 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,743 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)

12:54:19,744 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,745 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)

12:54:19,746 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,747 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)

12:54:19,748 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,749 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)

12:54:19,750 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,751 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)

12:54:19,752 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,753 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)

12:54:19,754 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

12:54:19,755 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,755 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)

12:54:19,756 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,756 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)

12:54:19,757 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)

12:54:19,757 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)

12:54:19,758 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)

12:54:19,758 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)

12:54:19,759 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)

12:54:19,759 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

12:54:19,760 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

12:54:19,760 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:397)

12:54:19,761 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50)

12:54:19,762 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

12:54:19,763 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

12:54:19,764 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

12:54:19,764 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

12:54:19,765 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

12:54:19,765 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

12:54:19,766 ERROR [stderr] (http-localhost-127.0.0.1-8082-6)   at java.lang.Thread.run(Thread.java:744)
jurus
  • 13
  • 4

1 Answers1

1

Sorry but I guess you have missed quite some aspects from the design and code. 1st of all you need the protocol, 2nd you need a single source of input stream on target side.

Protocol

In the output stream of local side, I suggest...

  1. Number of how many files are to be sent.
  2. Length of file name.
  3. The file name.
  4. Length of the file.
  5. File content
  6. Repeat 2~5 for the next file if there are any.

So the output stream would be something like:

[num_of_files][file1_name_length][file1_name][file1_size][file1_data][file2_.....]

Target Side

I suggest opening up ONE input stream ONLY and read according to the protocol. Remember flushing and closing each FileOutputStream in each loop.

Kaayan
  • 36
  • 3