Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6096391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:55:19+00:00 2026-05-23T12:55:19+00:00

I am trying to send a file from a client to a server. The

  • 0

I am trying to send a file from a client to a server. The server receives the file and then send a confirmation message to the client.

My problem is that the client becomes unresponsive when it is waiting the confirmation.

Server :

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

public class Server {
public static void main(String args[]) throws IOException {
        int port = 1234;
    ServerSocket server_socket;

    server_socket = new ServerSocket(port);
    System.out.println("Server waiting for client on port "
            + server_socket.getLocalPort());
    // server infinite loop
    while (true) {
        Socket socket = server_socket.accept();
        System.out.println("New connection accepted "
                + socket.getInetAddress() + ":" + socket.getPort());
    try {
                byte[] b = new byte[1024];
                int len = 0;
                int bytcount = 1024;
                String FileName = "C:\\test\\java\\tmp\\sentfile.pdf"; 
                FileOutputStream inFile = new FileOutputStream(FileName);
                InputStream is = socket.getInputStream();
                BufferedInputStream in2 = new BufferedInputStream(is, 1024);
                while ((len = in2.read(b, 0, 1024)) != -1) {
                    bytcount = bytcount + 1024;
                inFile.write(b, 0, len);
                }
                System.out.println("Bytes Writen : " + bytcount);

                //Sending the response back to the client.
                ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                oos.flush();
                oos.writeObject("ok");
                System.out.println("Message sent to the client is "+"ok");

                in2.close();
                inFile.close();
            } catch (IOException e) {
        System.out.println("Unable to open file" + e);
        return;
    }
    socket.close();
    System.out.println("Connection closed by client");
        }
}
}

Client :

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

public class Client {
    public static void main(String[] args) throws UnknownHostException,
        IOException, ClassNotFoundException {

        int port = 1234;
    Socket socket = null;

    // connect to server
    socket = new Socket("127.0.0.1", port);

    try {
            byte[] buf = new byte[1024];
            OutputStream os = socket.getOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(os, 1024);
            String file = "C:\\test\\java\\client\\source.pdf"; 
            FileInputStream in = new FileInputStream(file);
            int i = 0;
            int bytecount = 1024;
            while ((i = in.read(buf, 0, 1024)) != -1) {
                bytecount = bytecount + 1024;
            out.write(buf, 0, i);
            out.flush();
            }
            System.out.println("Bytes Sent :" + bytecount);

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String confirmation = (String) ois.readObject();
            System.out.println("from server : " + confirmation);

            out.close();
            in.close();
    } catch (IOException e) {
    System.out.println(e);
    }
    socket.close();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T12:55:19+00:00Added an answer on May 23, 2026 at 12:55 pm

    Here is the complete working example. The server:

    package so6540787;
    
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server {
    
      private final Socket socket;
    
      public Server(Socket socket) {
        this.socket = socket;
      }
    
      public void receiveFile(File file) throws IOException {
        byte[] b = new byte[1024];
        int len = 0;
        int bytcount = 1024;
        FileOutputStream inFile = new FileOutputStream(file);
        InputStream is = socket.getInputStream();
        BufferedInputStream in2 = new BufferedInputStream(is, 1024);
        while ((len = in2.read(b, 0, 1024)) != -1) {
          bytcount = bytcount + 1024;
          inFile.write(b, 0, len);
        }
        System.out.println("Bytes Writen : " + bytcount);
    
        // Sending the response back to the client.
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.flush();
        oos.writeObject("ok");
        System.out.println("Message sent to the client is " + "ok");
    
        in2.close();
        inFile.close();
      }
    
      public static void main(String[] args) throws IOException {
        ServerSocket listen = new ServerSocket(10000, 1);
        Socket socket = listen.accept();
        Server server = new Server(socket);
        server.receiveFile(new File("c:/received.pdf"));
      }
    }
    

    And the client:

    package so6540787;
    
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    
    public class Client {
    
      private final Socket socket;
    
      public Client(Socket socket) {
        this.socket = socket;
      }
    
      public void sendFile(File file) throws IOException, ClassNotFoundException {
        byte[] buf = new byte[1024];
        OutputStream os = socket.getOutputStream();
        BufferedOutputStream out = new BufferedOutputStream(os, 1024);
        FileInputStream in = new FileInputStream(file);
        int i = 0;
        int bytecount = 1024;
        while ((i = in.read(buf, 0, 1024)) != -1) {
          bytecount = bytecount + 1024;
          out.write(buf, 0, i);
          out.flush();
        }
        socket.shutdownOutput(); /* important */
        System.out.println("Bytes Sent :" + bytecount);
    
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        ois.skip(Long.MAX_VALUE);
        String confirmation = (String) ois.readObject();
        System.out.println("from server : " + confirmation);
    
        out.close();
        in.close();
      }
    
      public static void main(String[] args) throws IOException, ClassNotFoundException {
        Client client = new Client(new Socket("localhost", 10000));
        client.sendFile(new File("c:/tobesent.pdf"));
      }
    }
    

    The line you were missing is the “socket.shutdownOutput()”. If you leave that line out, the server will never see the end-of-file marker -1 from the read call.

    Please fix the way you count the bytes that have been sent. You should really start with 0 instead of 1024 and only increment that counter by as many bytes as you have actually read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a servlet that will send a XML file (xml
I'm trying to code a C# UDP server. It receives a specific ID from
Im trying to read in image file from a server , with the code
I'm trying to find a way to send files of different file types from
I'm trying to make a program (client) which kan send a message to a
I am trying to send an HTTP request with the contents of a file
I am trying to send an email from a site I am building, but
i'm trying to send fake keyboard input to an application that's running in a
I am trying to send some data from a LINQ query in C# to
I am trying to develop a system that has 2 tiers: a mobile client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.