Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: stuck sending file from client to server

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Thanked 0 Times in 0 Posts

    Default stuck sending file from client to server

    I have a client which is supposed to download an image file encrypted with AES (I'm not sure if I'm even implementing this correctly) from a server. However, with the following codes, the server gets stuck at the "Sending image" print message without moving on to the "Done" print message. In other words, I can't download the image file. Why is this so?

        public class Server { 
        	public static void main (String [] args ) throws InvalidKeyException, IOException, Exception { 
        		ServerSocket serverSocket = new ServerSocket(15123);
        		Socket socket = null;
        		//start of AES
        		//Generate AES Key
        		int sizeofkey = 128;
        		KeyGenerator kg = KeyGenerator.getInstance("AES");
        		//initialize with sizeofkey
        		Key mykey = kg.generateKey();
        		System.out.println("AES Key Generated");
        		//create cipher object & initialize with generated key
        		Cipher cipher = Cipher.getInstance("AES");
        		cipher.init(Cipher.ENCRYPT_MODE, mykey);
        		while(true)	{
        			socket = serverSocket.accept();
        			System.out.println("Accepted connection : " + socket.getRemoteSocketAddress().toString() + " <-> /" );
        			OutputStream sos = socket.getOutputStream();
        			// get the image from a webcam
        			URL myimage = new URL("");
        			//Picture in string format
        			//String plainpic = null;
        			//Picture in byte array format
        			byte[] plainpic = null;
        			ByteArrayOutputStream baos = new ByteArrayOutputStream();
        			InputStream is = null;
        			try	{
        				//in = new DataInputStream(myimage.openStream()); 
        				is = myimage.openStream ();
        				byte[] byteChunk = new byte[4096]; 
        				int n;
        				while ( (n = is.read(byteChunk)) > 0 ) {
        					baos.write(byteChunk, 0, n);
        				//Picture in string format
        				//Picture in byte array format
        			catch (Exception ee)	{
        				System.out.println("Check internet connection please");
        			byte[] cipherpic = cipher.doFinal(plainpic);
        			DateFormat dateFormat = new SimpleDateFormat("yy/MM/dd HH:mm:ss");
        			Date date = new Date();
        			System.out.println("Sending image " + dateFormat.format(date));
        			try	{
        				while (true) {
        			catch (EOFException ee)	{ 
        				System.out.println("-------------- Done ----------"); 

        public class Client { 
          public static void main(String [] args) throws IOException {
            String fname = "image.jpg";
            SSLSocket sslsocket = new Socket("",15123);
            DataInputStream in = null;
            try{ in = new DataInputStream(sslsocket.getInputStream()); }
            catch (Exception ee)
            { System.out.println("Check connection please");
              sslsocket.close(); return;
            FileOutputStream fos = new FileOutputStream(fname);
        		while (true)
            catch (EOFException ee)
            {  System.out.println("File transfer complete");
            public static String asHex (byte buf[]) {
                //Obtain a StringBuffer object
                StringBuffer strbuf = new StringBuffer(buf.length * 2);
                int i;
                for (i = 0; i < buf.length; i++) {
                    if (((int) buf[i] & 0xff) < 0x10)
                    strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
                // Return result string in Hexadecimal format
                return strbuf.toString();
    Last edited by wei123; February 14th, 2018 at 01:48 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,708 Times in 2,658 Posts

    Default Re: stuck sending file from client to server

    server gets stuck at the "Sending image" print message
    The write statement is in a forever running while loop.
    To see what the code is doing add a print statement inside the loop.

    What does the Client program receive?

    How is the statement printing "Done" supposed to be executed?

    Note: The posted code does not compile without errors. Please fix the errors and post code that compiles without errors.
    If you need help fixing the errors, copy the full text of the error messages and paste it here with your questions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Method :sending image java server to php client
    By ashi123 in forum Java Theory & Questions
    Replies: 0
    Last Post: July 22nd, 2011, 10:15 AM
  2. sending images client/server sockets
    By devett in forum Java Networking
    Replies: 7
    Last Post: June 7th, 2011, 08:38 AM
  3. HELP with sending data within text file to client with printwriter
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 24th, 2011, 02:42 PM
  4. sending objects from client to server
    By 11moshiko11 in forum What's Wrong With My Code?
    Replies: 16
    Last Post: October 8th, 2010, 04:47 AM
  5. Monitor the progress of sending a file to client
    By adumi in forum Java Theory & Questions
    Replies: 0
    Last Post: April 17th, 2010, 07:01 AM

Tags for this Thread