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 test cases
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)