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 8695723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:06:17+00:00 2026-06-13T01:06:17+00:00

Hi i have a problem with my server, everytime i call dload the file

  • 0

Hi i have a problem with my server, everytime i call “dload” the file gets downloaded but i can’t use the other commands i have because they get returned as null. Anyone who can see the problem in the code?

Server :

public class TCPServer {

    public static void main(String[] args) {

        ServerSocket server = null;
        Socket client;
        // Default port number we are going to use
        int portnumber = 1234;
        if (args.length >= 1) {
            portnumber = Integer.parseInt(args[0]);
        }
        // Create Server side socket
        try {
            server = new ServerSocket(portnumber);
        } catch (IOException ie) {
            System.out.println("Cannot open socket." + ie);
            System.exit(1);
        }
        System.out.println("ServerSocket is created " + server);
        // Wait for the data from the client and reply

        boolean isConnected = true;

        try {
            // Listens for a connection to be made to
            // this socket and accepts it. The method blocks until
            // a connection is made
            System.out.println("Waiting for connect request...");
            client = server.accept();
            System.out.println("Connect request is accepted...");
            String clientHost = client.getInetAddress().getHostAddress();
            int clientPort = client.getPort();
            System.out.println("Client host = " + clientHost
                    + " Client port = " + clientPort);

            // Read data from the client
            while (isConnected == true) {

                InputStream clientIn = client.getInputStream();

                BufferedReader br = new BufferedReader(new InputStreamReader(
                        clientIn));
                String msgFromClient = br.readLine();
                System.out.println("Message received from client = "
                        + msgFromClient);

                // Send response to the client

                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("sum")) {
                    OutputStream clientOut = client.getOutputStream();
                    PrintWriter pw = new PrintWriter(clientOut, true);
                    Double[] list;
                    list = new Double[5];
                    String value;
                    int i;
                    try {

                        for (i = 0; i < 5; i++) {
                            pw.println("Input number in arrayslot: " + i);
                            value = br.readLine();
                            double DoubleValue = Double.parseDouble(value);
                            list[i] = DoubleValue;
                        }
                        if (i == 5) {
                            Double sum = 0.0;
                            for (int k = 0; k < 5; k++) {
                                sum = sum + list[k];
                            }
                            pw.println("Sum of array is " + sum);
                        }
                    } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("max")) {
                    OutputStream clientOut = client.getOutputStream();
                    PrintWriter pw = new PrintWriter(clientOut, true);
                    Double[] list;
                    list = new Double[5];
                    String value;
                    int i;
                    try {

                        for (i = 0; i < 5; i++) {
                            pw.println("Input number in arrayslot: " + i);
                            value = br.readLine();
                            double DoubleValue = Double.parseDouble(value);
                            list[i] = DoubleValue;
                        }
                        if (i == 5) {
                            Arrays.sort(list);
                            pw.println("Max integer in array is " + list[4]);
                        }
                    } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("time")) {
                    OutputStream clientOut = client.getOutputStream();
                    PrintWriter pw = new PrintWriter(clientOut, true);
                    Calendar calendar = GregorianCalendar.getInstance();
                    String ansMsg = "Time is:, "
                            + calendar.get(Calendar.HOUR_OF_DAY) + ":"
                            + calendar.get(Calendar.MINUTE);
                    pw.println(ansMsg);
                }
                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("date")) {
                    OutputStream clientOut = client.getOutputStream();
                    PrintWriter pw = new PrintWriter(clientOut, true);
                    Calendar calendar = GregorianCalendar.getInstance();
                    String ansMsg = "Date is: " + calendar.get(Calendar.DATE)
                            + "/" + calendar.get(Calendar.MONTH) + "/"
                            + calendar.get(Calendar.YEAR);
                    ;
                    pw.println(ansMsg);
                }
                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("c2f")) {
                    OutputStream clientOut = client.getOutputStream();
                    PrintWriter pw = new PrintWriter(clientOut, true);
                    String celciusValue;
                    boolean ifRead = false;

                    try {

                        pw.println("Input celcius value");
                        celciusValue = br.readLine();
                        ifRead = true;
                        if (ifRead == true) {
                            double celcius = Double.parseDouble(celciusValue);
                            celcius = celcius * 9 / 5 + 32;

                            pw.println(celcius);
                        }
                    } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("dload")) {

                    OutputStream outToClient = client.getOutputStream();
                    if (outToClient != null) {
                        File myFile = new File("C:\\ftp\\pic.png");
                        byte[] mybytearray = new byte[(int) myFile.length()];

                        FileInputStream fis = new FileInputStream(myFile);

                        BufferedInputStream bis = new BufferedInputStream(fis);

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

                            outToClient.flush();
                            outToClient.close();
                            bis.close();
                            fis.close();

                        } catch (IOException ex) {
                            // Do exception handling
                        }

                        System.out.println("test");

                    }
                }

                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("quit")) {
                    client.close();
                    break;
                }
                // if (msgFromClient != null
                // && !msgFromClient.equalsIgnoreCase("bye")) {
                // OutputStream clientOut = client.getOutputStream();
                // PrintWriter pw = new PrintWriter(clientOut, true);
                // String ansMsg = "Hello, " + msgFromClient;
                // pw.println(ansMsg);
                // }

                // Close sockets
                if (msgFromClient != null
                        && msgFromClient.equalsIgnoreCase("bye")) {
                    server.close();
                    client.close();
                    break;
                }

                msgFromClient = null;
            }
        } catch (IOException ie) {
        }
    }
}

Client:

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

public class TCPClient {
 public static void main(String args[]) {

  boolean isConnected = true;
  Socket client = null;
  int portnumber = 1234; // Default port number we are going to use
  if (args.length >= 1) {
   portnumber = Integer.parseInt(args[0]);
  }
  try {

   String msg = "";
   // Create a client socket
   client = new Socket("127.0.0.1", 1234);
   System.out.println("Client socket is created " + client);
   // Create an output stream of the client socket

   OutputStream clientOut = client.getOutputStream();
   PrintWriter pw = new PrintWriter(clientOut, true);
   // Create an input stream of the client socket
   InputStream clientIn = client.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(
     clientIn));
   // Create BufferedReader for a standard input
   BufferedReader stdIn = new BufferedReader(new InputStreamReader(
     System.in));

   while (isConnected == true) {
    System.out
      .println("Commands: \n1. TIME\n2. DATE\n3. C2F\n4. MAX\n5. SUM\n6. DLOAD\n7. QUIT");
    // Read data from standard input device and write it
    // to the output stream of the client socket.
    msg = stdIn.readLine().trim();
    pw.println(msg);
    // Read data from the input stream of the client socket.



    if (msg.equalsIgnoreCase("dload")) {
        byte[] aByte = new byte[1];
        int bytesRead;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        if (clientIn != null) {



            try {
                FileOutputStream fos = new FileOutputStream("C:\\ftp\\pic.png");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                bytesRead = clientIn.read(aByte, 0, aByte.length);

                do {
                    baos.write(aByte, 0, bytesRead);
                    bytesRead = clientIn.read(aByte);
                } while (bytesRead != -1);

                bos.write(baos.toByteArray());
                bos.flush();
                bos.close();

                System.out.println("File is successfully downloaded to your selected directory"+ "\n" +"*-----------------*"+ "\n" );

            } catch (IOException ex) {
                System.out.println("Couldn't dowload the selected file, ERROR CODE "+ex);
            }

        }
    }else{

        System.out.println("Message returned from the server = "
                  + br.readLine());
    }
    if (msg.equalsIgnoreCase("bye")) {

     pw.close();
     br.close();
     break;
    }
   }
  } catch (Exception e) {

  }

 }
}
  • 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-06-13T01:06:19+00:00Added an answer on June 13, 2026 at 1:06 am

    debugged your code and have two hints:

    1)

    don’t surpress your exceptions. handle them! first step would to print your stacktrace and this question on SO wouldn’t ever be opened 😉 debug your code!

    2)

    outToClient.flush();
    outToClient.close(); //is closing the socket implicitly
    bis.close();
    fis.close();
    

    so in your second call the socket on server-side will already be closed.

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

Sidebar

Related Questions

I have a C/S program. Client use socket to send a file to server,
I have odd problem: After starting server I got this error: undefined local variable
We have problem with our Qt based production server for our business application. When
I have problem with receiving an image over TCP socket [.net 4.0] Server: Socket
I'm writing chat client/server app with c# and I have problem with threading. I
i have an problem with handling data come from server , please see the
I have a problem with a Sender and a Server UDP. The SenderUDP sends
I have a simple problem with SQL SERVER charindex function. DECLARE @VAR1 varchar SET
I have a problem using a local SQL Server CE database with C# and
I have a problem with starting the sunspot server. last week it worked like

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.