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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 2 of 2

Thread: Help with Client Program?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with Client Program?

    I am working on a program that connects to a server and talks back and forth.

    The server sends a String initially and then I send the first letter back server sends the second and I send the third so on and so forth.

    Whoever does not send the last letter of the string sends "done!".

    My program is working up to the point of getting the string and breaking up into the chars, but will not enter the while loop it seems and I can't figure out why.

    Below is my code and the test cases I am trying to pass.

    Could someone explain why my code is not working (I think it has to do with the while loop), and what I need to do to fix it?

    My code

    import java.io.IOException;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.io.PrintWriter;
        import java.net.Socket;
        import java.util.Scanner;
     
     
        public class Program2 {
        static String rec;
        public static void goClient(){
    	try{
    	String server = "localhost";
    	int port = 4321;
    	Socket socket = new Socket(server, port);
    	InputStream inStream = socket.getInputStream();
    	OutputStream outStream = socket.getOutputStream();
    	Scanner scan = new Scanner(inStream);
    	PrintWriter out = new PrintWriter(outStream);
     
     
    		rec = scan.nextLine();
    		System.out.println(rec);
     
     
    	char[] array = new char[rec.length()];
     
    	for(int i = 0; i < rec.length(); i++){
    		array[i] = rec.charAt(i);
    		System.out.println(array[i]);
    	}
     
     
    		if(array.length % 2 == 0){
    		while(scan.hasNext()){	
    		for(int x = 0; x < array.length; x+=2){
    		String str;
    		str = Character.toString(array[x]);
    		System.out.print(str);
    		out.println(str);
    		str = scan.nextLine();
    		System.out.print(str);
    		}
    		}	
    		}
    		if(array.length % 2 != 0){
    			while(scan.hasNext()){
    			for(int x = 0; x < array.length - 1; x+=2){
    				String str;
    				str = Character.toString(array[x]);
    				System.out.print(str);
    				out.println(str);
    				str = scan.nextLine();
    				System.out.print(str);
    			}
    			}
    			out.println("done!");
    	       }
     
    	scan.close();
    	socket.close();
     
    	} catch (IOException e) {}
    	}
        }

    Junit tests
    import static org.junit.Assert.*;
     
        import java.io.IOException;
        import java.io.PrintWriter;
        import java.net.ServerSocket;
        import java.net.Socket;
        import java.util.Scanner;
        import java.util.concurrent.CountDownLatch;
     
        import org.junit.Before;
        import org.junit.Test;
     
        public class Program2Test {
    	@Before
    	public void wait2SecondsBeforeEachTest() {
    		try {
    			Thread.sleep( 2000 );
    		} 
    		catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    	}
    	@Test
    	public void test0() {
    		Runnable             client = new Runnable() {
    			@Override
    			public void run() {
    				Program2.goClient();
    			}
    		};
    		final CountDownLatch gate   = new CountDownLatch( 1 );
    		Runnable             server = new Runnable() {
    					@Override
    					public void run() {
    				ServerSocket server = null;
    				try {
    					server = new ServerSocket( 4321 );
     
    					Socket       client = server.accept();
    					PrintWriter  out    = new PrintWriter( client.getOutputStream(), true );
    					Scanner      in     = new Scanner    ( client.getInputStream() );
     
    					out.println( "Lorem" );
     
    					String actual = in.nextLine();
    					assertEquals( "Incorrect result", "L", actual );
     
    					out.println( "o" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "r", actual );
     
    					out.println( "e" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "m", actual );
     
    					out.println( "done!" );
     
    					assertFalse( "Client hasn't closed its socket", in.hasNext() );
     
    					client.close();
     
    					gate.countDown();
    				}
    				catch (IOException e) {
    					e.printStackTrace();
    				}
    				finally {
    					try {
    						if (server != null) {
    							server.close();
    						}
    					} 
    					catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		};
    		Thread serverThread  = new Thread( server );
    		serverThread.start();
    		Thread clientThread  = new Thread( client );
    		clientThread.start();
     
    		int count = 0;
    		while (serverThread.isAlive() && count++ < 300) {
    			try {
    				Thread.sleep( 10 );
    			} 
    			catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
    	}
    	@Test
    	public void test1() {
    		Runnable             client = new Runnable() {
    			@Override
    			public void run() {
    				Program2.goClient();
    			}
    		};
    		final CountDownLatch gate   = new CountDownLatch( 1 );
    		Runnable             server = new Runnable() {
    			@Override
    			public void run() {
    				ServerSocket server = null;
    				try {
    					server = new ServerSocket( 4321 );
     
    					Socket       client = server.accept();
    					PrintWriter  out    = new PrintWriter( client.getOutputStream(), true );
    					Scanner      in     = new Scanner    ( client.getInputStream() );
     
    					out.println( "Let it be." );
     
    					String actual = in.nextLine();
    					assertEquals( "Incorrect result", "L", actual );
     
    					out.println( "e" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "t", actual );
     
    					out.println( " " );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "i", actual );
     
    					out.println( "t" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", " ", actual );
     
    					out.println( "b" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "e", actual );
     
    					out.println( "." );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "done!", actual );
     
    					assertFalse( "Client hasn't closed its socket", in.hasNext() );
     
    					client.close();
     
    					gate.countDown();
    				}
    				catch (IOException e) {
    					e.printStackTrace();
    				}
    				finally {
    					try {
    						if (server != null) {
    							server.close();
    						}
    					} 
    					catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		};
    		Thread serverThread  = new Thread( server );
    		serverThread.start();
    		Thread clientThread  = new Thread( client );
    		clientThread.start();
     
    		int count = 0;
    		while (serverThread.isAlive() && count++ < 300) {
    			try {
    				Thread.sleep( 10 );
    			} 
    			catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
    	}
    	@Test
    	public void test2() {
    		Runnable             client = new Runnable() {
    			@Override
    			public void run() {
    				Program2.goClient();
    			}
    		};
    		final CountDownLatch gate   = new CountDownLatch( 1 );
    		Runnable             server = new Runnable() {
    			@Override
    			public void run() {
    				ServerSocket server = null;
    				try {
    					server = new ServerSocket( 4321 );
     
    					Socket       client = server.accept();
    					PrintWriter  out    = new PrintWriter( client.getOutputStream(), true );
    					Scanner      in     = new Scanner    ( client.getInputStream() );
     
    					out.println( "Upside Down" );
     
    					String actual = in.nextLine();
    					assertEquals( "Incorrect result", "U", actual );
     
    					out.println( "p" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "s", actual );
     
    					out.println( "i" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "d", actual );
     
    					out.println( "e" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", " ", actual );
     
    					out.println( "D" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "o", actual );
     
    					out.println( "w" );
     
    					actual = in.nextLine();
    					assertEquals( "Incorrect result", "n", actual );
     
    					out.println( "done!" );
     
    					assertFalse( "Client hasn't closed its socket", in.hasNext() );
     
    					client.close();
     
    					gate.countDown();
    				}
    				catch (IOException e) {
    					e.printStackTrace();
    				}
    				finally {
    					try {
    						if (server != null) {
    							server.close();
    						}
    					} 
    					catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		};
    		Thread serverThread  = new Thread( server );
    		serverThread.start();
    		Thread clientThread  = new Thread( client );
    		clientThread.start();
     
    		int count = 0;
    		while (serverThread.isAlive() && count++ < 300) {
    			try {
    				Thread.sleep( 10 );
    			} 
    			catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    		assertTrue( "Client did not arrive or did not finish correctly", gate.getCount() == 0 );
    	}
        }

    Console Output

    Lorem
    L
    o
    r
    e
    m
    java.net.BindException: Address already in use: JVM_Bind
    	at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    	at java.net.PlainSocketImpl.bind(Unknown Source)
    	at java.net.ServerSocket.bind(Unknown Source)
    	at java.net.ServerSocket.<init>(Unknown Source)
    	at java.net.ServerSocket.<init>(Unknown Source)
    	at Program2Test$4.run(Program2Test.java:112)
    	at java.lang.Thread.run(Unknown Source)
    java.net.BindException: Address already in use: JVM_Bind
    	at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    	at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    	at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    	at java.net.PlainSocketImpl.bind(Unknown Source)
    	at java.net.ServerSocket.bind(Unknown Source)
    	at java.net.ServerSocket.<init>(Unknown Source)
    	at java.net.ServerSocket.<init>(Unknown Source)
    	at Program2Test$6.run(Program2Test.java:199)
    	at java.lang.Thread.run(Unknown Source)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Client Program?

    java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unkno wn Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at Program2Test$4.run(Program2Test.java:112)
    It looks like the program is trying to create a second ServerSocket while there is already one open.
    Check the logic to see why it is trying to create the second one.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. client program rectangle
    By Username in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 13th, 2013, 06:25 PM
  2. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  3. [SOLVED] Help ending client program from the server
    By Chromejuice in forum Java Networking
    Replies: 0
    Last Post: October 30th, 2012, 11:28 AM
  4. Help with JAVA Server-Client Program
    By GodLeoBouki in forum Java Theory & Questions
    Replies: 3
    Last Post: April 12th, 2011, 04:35 PM
  5. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM