socket error when reading ip address from console

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • diegoblin
    New Member
    • Apr 2009
    • 4

    socket error when reading ip address from console

    hello, i have this issue.Every time i use sockets if i use a "String" as a parameter the connection resets. but if i put the ip address like this "127.0.0.1" it okay.

    and i got this message if i use a string i read from the console

    Exception in thread "main" java.net.Socket Exception: Connection r
    at java.net.Socket InputStream.rea d(Unknown Source)
    at sun.nio.cs.Stre amDecoder.readB ytes(Unknown Source)
    at sun.nio.cs.Stre amDecoder.implR ead(Unknown Source)
    at sun.nio.cs.Stre amDecoder.read( Unknown Source)
    at java.io.InputSt reamReader.read (Unknown Source)
    at java.io.Buffere dReader.fill(Un known Source)
    at java.io.Buffere dReader.readLin e(Unknown Source)
    at java.io.Buffere dReader.readLin e(Unknown Source)
    at ServidorProyect o.mensajero(Ser vidorProyecto.j ava:141)
    at Menu.selecciona Opcion(Proyecto .java:85)
    at Menu.mostrarMen u(Proyecto.java :56)
    at Proyecto.main(P royecto.java:20 2)



    Code:
    ClienteProyecto(String dirIP)throws IOException
    	{
    	
    						
    			InetAddObj = InetAddress.getByName(dirIP);// error
    			InetAddObj = InetAddress.getByName("127.0.0.1");// correct uh?
    			
    			System.out.println("Conectando a : " + InetAddObj);
    		
    			ServidorProyecto SPObj = new ServidorProyecto();
    								
    			SocketObj = new Socket(InetAddObj, SPObj.devuelvePuerto());
    
    		
    				
    	}
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Print out your String parameter dirIP and check what's wrong with it.

    kind regards,

    Jos

    Comment

    • diegoblin
      New Member
      • Apr 2009
      • 4

      #3
      I already did it, for example if i type 127.0.0.1 and print that String it says 127.0.0.1, also i checked the string length with dirIp.length() and it's correct!

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by diegoblin
        I already did it, for example if i type 127.0.0.1 and print that String it says 127.0.0.1, also i checked the string length with dirIp.length() and it's correct!
        Can you show us the code that produced that String IP address? There must be something wrong in there (maybe a trailing \n character).

        kind regards,

        Jos

        Comment

        • diegoblin
          New Member
          • Apr 2009
          • 4

          #5
          I'm using this class to read from console

          Code:
          class LeeConsola
          {
          		
          	String devuelveCadena () throws IOException
          	{
          		BufferedReader BRobj = new BufferedReader(new InputStreamReader(System.in));
          		
          		char caracterEntrada;
          		String cadena = "";
          		
          				
          		do
          		{
          			caracterEntrada = (char)  BRobj.read();
          			cadena = cadena + caracterEntrada;
          		} while (caracterEntrada != '\r');
          		
          		return cadena;
          	}
          	
          	
          	
          }
          and the code where i send the String parameter is this

          Code:
          LeeConsola leeObj = new LeeConsola();
          				String cadenaIp = "";
          				System.out.println("Dame la direccion ip");
          				cadenaIp = leeObj.devuelveCadena();
          				
          				ClienteProyecto CPObj = new ClienteProyecto(cadenaIp.substring(0,cadenaIp.length() - 1));// to get rid of /r

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by diegoblin
            I'm using this class to read from console

            Code:
            class LeeConsola
            {
            		
            	String devuelveCadena () throws IOException
            	{
            		BufferedReader BRobj = new BufferedReader(new InputStreamReader(System.in));
            		
            		char caracterEntrada;
            		String cadena = "";
            		
            				
            		do
            		{
            			caracterEntrada = (char)  BRobj.read();
            			cadena = cadena + caracterEntrada;
            		} while (caracterEntrada != '\r');
            		
            		return cadena;
            	}
            	
            	
            	
            }
            Suppose you type abc<enter>, the cadena String ends up with four characters: a, b, c and \r. You should not store the trailing \r character. btw. the BufferedReader class has a readLine() method that takes care of all the details.

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Also, these days people seem to prefer the Scanner or Console classes for getting input from the console

              Comment

              Working...