Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Socket communication error

    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


    Java Code:
    	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);

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    This will be hard to figure out without working code. Can you make a small program that compiles and executes to demonstrate the problem?

    Use the available() method to see if it shows there are bytes waiting to be read so the read() method won't block waiting. Print out what it returns.

  3. #3
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    I'll post a runnable program here - on the client side you just declare the Socket variable and create an object in main, and here is a piece of code on the server side - I'll just post the function that goes in the first case, so please test just the first case and ignore the others, since it doesn't really make any difference. So I guess this code will compile,just comment out the method calls in cases 2-5 and work with the first. Thanks a lot, I'm going to try that thing you said, but I got stuck with some deprecated class,so I have to find the non-deprecated equivalent.

    Java Code:
    package server;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Hashtable;
    
    public class Server extends Thread{
    
    	 ServerSocket s;
    	
    	 Socket socket1;
    	 
    	
    	public Server() {
    		try{
    		if(s == null){
    			
    		s = new ServerSocket(8989);
    		}
    			}catch(IOException e){
    				e.printStackTrace();
    			}
    	}
    	
    	
    	 public Socket getSocket(){
    		return socket1;
    	}
    	public void run(){
    		try {
    			 socket1 = s.accept();
    			 
    		
    /**Getting input from the client and storing it in an integer variable */			 
    			BufferedReader userInput = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
    			String readInput = userInput.readLine();
    			int var = Integer.parseInt(readInput);
    /**Selection based on the user's input */			
    			switch(var){
    			case 1:
    				chooseBand();
    				break;
    			case 2:
    				chooseGuitarist();
    				break;
    			case 3:
    				chooseAlbum();
    				break;
    			case 4:
    				chooseFilm();
    				break;
    			case 5:
    				chooseActor();
    					break;
    			
    			}
    			
    			
    			
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    	
    	public void chooseBand() throws IOException{
    		/**Asking the client another question */				
    				PrintWriter out = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    				out.println("What is, in your opinion, the best rock band ever? \n 1.Led Zeppelin \n 2.The Rolling Stones \n 3.Pink Floyd \n 4.Deep Purple \n 5.AC/DC#");
    				out.flush();
    		/**Getting the new user input and converting it to an integer*/				
    				BufferedReader userInput1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
    				String readInput1 = userInput1.readLine();
    				int var1 = Integer.parseInt(readInput1);
    				int zepp = 0, stones = 0, floyd = 0, purple = 0, acDc = 0;
    		/**Selection based on the new input by the user*/				
    				switch(var1){
    				case 1:
    		/**Sending the results to the client */		
    					zepp++;
    					PrintWriter out1 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    					out1.println("Zeppelin");
    					out1.flush();
    					
    					break;
    				case 2:
    		/**Sending the results to the client */			
    					PrintWriter out2 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    					out2.println("Stones");
    					out2.flush();
    					stones++;
    					break;
    				case 3:
    		/**Sending the results to the client */			
    					PrintWriter out3 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    					out3.println("Floyd");
    					out3.flush();
    					floyd++;
    					break;
    				case 4:
    		/**Sending the results to the client */
    					PrintWriter out4 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    					out4.println("Purple");
    					out4.flush();
    					purple++;
    					break;
    				case 5:
    		/**Sending the results to the client */
    					PrintWriter out5 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
    					out5.println("AC/DC");
    					out5.flush();
    					acDc++;
    					break;
    				}
    			}
    	public static void main(String[] args) {
    		Server s;
    		s = new Server();
    		s.start();
    		
    	}
    }

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See your cross-post.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Norm View Post
    This will be hard to figure out without working code. Can you make a small program that compiles and executes to demonstrate the problem?

    Use the available() method to see if it shows there are bytes waiting to be read so the read() method won't block waiting. Print out what it returns.
    Sorry, but available is worthless on socket streams.

  6. #6
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Socket error
    By hannes in forum Networking
    Replies: 7
    Last Post: 04-19-2010, 11:22 AM
  2. communication with socket
    By yacin87 in forum Networking
    Replies: 0
    Last Post: 04-08-2010, 11:14 PM
  3. Replies: 5
    Last Post: 11-01-2009, 05:16 PM
  4. Replies: 2
    Last Post: 09-10-2009, 02:29 PM
  5. Socket Communication on different networks
    By ersachinjain in forum Networking
    Replies: 4
    Last Post: 09-05-2009, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •