473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.SocketException: Connection r
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at ServidorProyecto.mensajero(ServidorProyecto.java:1 41)
at Menu.seleccionaOpcion(Proyecto.java:85)
at Menu.mostrarMenu(Proyecto.java:56)
at Proyecto.main(Proyecto.java:202)



Expand|Select|Wrap|Line Numbers
  1.  
  2. ClienteProyecto(String dirIP)throws IOException
  3.     {
  4.  
  5.  
  6.             InetAddObj = InetAddress.getByName(dirIP);// error
  7.             InetAddObj = InetAddress.getByName("127.0.0.1");// correct uh?
  8.  
  9.             System.out.println("Conectando a : " + InetAddObj);
  10.  
  11.             ServidorProyecto SPObj = new ServidorProyecto();
  12.  
  13.             SocketObj = new Socket(InetAddObj, SPObj.devuelvePuerto());
  14.  
  15.  
  16.  
  17.     }
  18.  
  19.  
  20.  
May 7 '09 #1
6 2738
JosAH
11,448 Expert 8TB
Print out your String parameter dirIP and check what's wrong with it.

kind regards,

Jos
May 7 '09 #2
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!
May 7 '09 #3
JosAH
11,448 Expert 8TB
@diegoblin
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
May 7 '09 #4
I'm using this class to read from console

Expand|Select|Wrap|Line Numbers
  1. class LeeConsola
  2. {
  3.  
  4.     String devuelveCadena () throws IOException
  5.     {
  6.         BufferedReader BRobj = new BufferedReader(new InputStreamReader(System.in));
  7.  
  8.         char caracterEntrada;
  9.         String cadena = "";
  10.  
  11.  
  12.         do
  13.         {
  14.             caracterEntrada = (char)  BRobj.read();
  15.             cadena = cadena + caracterEntrada;
  16.         } while (caracterEntrada != '\r');
  17.  
  18.         return cadena;
  19.     }
  20.  
  21.  
  22.  
  23. }
  24.  
and the code where i send the String parameter is this

Expand|Select|Wrap|Line Numbers
  1.  
  2. LeeConsola leeObj = new LeeConsola();
  3.                 String cadenaIp = "";
  4.                 System.out.println("Dame la direccion ip");
  5.                 cadenaIp = leeObj.devuelveCadena();
  6.  
  7.                 ClienteProyecto CPObj = new ClienteProyecto(cadenaIp.substring(0,cadenaIp.length() - 1));// to get rid of /r
  8.  
May 7 '09 #5
JosAH
11,448 Expert 8TB
@diegoblin
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
May 8 '09 #6
r035198x
13,262 8TB
Also, these days people seem to prefer the Scanner or Console classes for getting input from the console
May 8 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
3
by: Phadnis | last post by:
hi.. iam trying to have a proxy server which listens to the rquests comign from port 80. in tcp listener declaration i have give my system name n the port 80 to listen to. but when i run it...
8
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
13
by: coloradowebdev | last post by:
i am working on basically a proxy server that handles requests via remoting from clients and executes transactions against a third-party server via TCP. the remoting site works like a champ. my...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.