1,105,226 Community Members

Socket communication error

Member Avatar
robertmacedonia
Junior Poster in Training
85 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I'd like to ask for a little help. I have this program that handles basic voting,but it should be done client-server,i.e. the server offers questions and multiple clients connect to vote,the server stores the answers etc... I haven't even come to that point of storing etc.,cause I'm stuck here for a while. I do multiple sending and receiving of messages between the client and the server,e.g. server offers an initial menu to choose from 5 categories, the client chooses one,then I handle the choice using switch(),so I send the appropriate answer back. Since one of my answers is a multi-line menu, I should use a while((line = reader.readline()) != null) loop so I could get all the lines to present to the client, so that he could choose from the menu. And that's where the problem arises - I noticed that if I delete the while() loop and read only one line,everything is OK,I get the answer based on user's input. If I use the loop,the program gets stuck when the user inputs text,it doesn't do anything,doesn't even terminate or give me errors. So, my code is kind of big, I'm not so sure how to post it here,so I'll initially post the critical part only,an excerpt from the client-side code, assume that on the other side there is server-side handling using switch() and case. Here is the snippet. There's only one while() loop, so it won't be hard to spot it. Thanks a lot, regards

s = new Socket("127.0.0.1",8989);
/**Displaying the first question to the user*/
			System.out.println("Choose a category please: \n 1.Rock bands \n 2.Rock guitarists \n 3.Rock albums \n 4.Hitchcock films \n 5.Actors ");
/** Getting input from the user*/
			BufferedReader userEnteredText = new BufferedReader(new InputStreamReader(System.in));
			String takenUserText = userEnteredText.readLine();
/**Sending the input to the server */			
			PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			out.println(takenUserText);
			out.flush();
			//userEnteredText.close();
/**Getting the response from the server*/			
			BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String line;
			while((line = reader.readLine()) != null){
			System.out.println(line);
			}
			
/**Getting another user input*/		
			BufferedReader userEnteredText1 = new BufferedReader(new InputStreamReader(System.in));
			String takenUserText1 = userEnteredText1.readLine();
/**Sending the new user input back to the server*/	
			PrintWriter out1 = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
			out1.println(takenUserText1);
			out1.flush();
/**Reading the results from the server*/			
			BufferedReader reader1 = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String line1 = reader1.readLine();
			System.out.println(line1);
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
1
 

Because you won't get "null" until the server closes its end of the connection. Which you don't want, I would assume. I would assume you want to continue using the same connection, right? So the server is going to have to send some extra String, or something, to signify that its finished, and the client will have to check for this String and break out of the loop once it gets it (without showing that String to the user, of course. Or, you could simply use the last line of the menu as the "key" to break out of the loop, if it has a fixed, set text.

Member Avatar
robertmacedonia
Junior Poster in Training
85 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Because you won't get "null" until the server closes its end of the connection. Which you don't want, I would assume. I would assume you want to continue using the same connection, right? So the server is going to have to send some extra String, or something, to signify that its finished, and the client will have to check for this String and break out of the loop once it gets it (without showing that String to the user, of course. Or, you could simply use the last line of the menu as the "key" to break out of the loop, if it has a fixed, set text.

But I've tried that. I put a "#" at the end of the menu and modified the loop - while((line = reader.readLine()) != "#"),and still the same thing happens, it just stays as it is,I can enter another input from the keyboard, I can pres Enter, it dos nothing until I terminate it manually. I have no idea what to do.

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
1
 

Don't use = (or !=) to compare Strings. use "str.equals(str)" (or !str.equals(str)).

Edit: IOW, leave the while condition checking the "null" status (thereby catching the EOS).
And, as the first line of the loop check the String's value with the equals method and break the loop when that value matches a specific "kill" value.

Member Avatar
robertmacedonia
Junior Poster in Training
85 posts since Jul 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Don't use = (or !=) to compare Strings. use "str.equals(str)" (or !str.equals(str)).

Edit: IOW, leave the while condition checking the "null" status (thereby catching the EOS).
And, as the first line of the loop check the String's value with the equals method and break the loop when that value matches a specific "kill" value.

Thanks a lot! I edited it like this:On the server side -

out.println("Who is, in your opinion, the best rock guitar player ever? \n 1.Eric Clapton \n 2.Jimmy Page \n 3.Duane Allman \n 4.Jimi Hendrix \n 5.Jeff Beck \n#" );

And on the client side:

while(!(line = reader.readLine()).equals(null)){
				if(line.equals("#"))
					break;
				System.out.println(line);
				
				
			}

I better continue working, 'cause I got a lot to do till I finish this one. Kind regards

Question Answered as of 3 Years Ago by masijade
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article