473,748 Members | 2,502 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

socket error when reading ip address from console

4 New Member
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)



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 2778
JosAH
11,448 Recognized Expert MVP
Print out your String parameter dirIP and check what's wrong with it.

kind regards,

Jos
May 7 '09 #2
diegoblin
4 New Member
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 Recognized Expert MVP
@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
diegoblin
4 New Member
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 Recognized Expert MVP
@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 MVP
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
3794
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 after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve connection, recieve data, and close socket
3
9100
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 the client machine to close per my network app the computer fills up w/ thousands of these :0 TCP foobox:8888 localhost:2188 TIME_WAIT :0 TCP foobox:8888 localhost:2189 TIME_WAIT :0 TCP foobox:8888 localhost:2190 TIME_WAIT
3
1536
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 comes to tcplistener.start it gives an excpetion: "Only one usage of each socket address (protocol/network address/port) is normally permitted" plz this is urgnet.. give reply asap
8
21912
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 that time, I get a SocketException with the message "Only one usage of each socket address (protocol/network address/port) is normally permitted". This happen if you use localhost or between two distinct computers. And, for a period of time after the...
13
2646
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 problem is executing the transactions against the remote server and returning the response to the remoting client. i can open the socket fine and, if i am executing one transaction at a time, everything works great. it's when my proxy server...
9
3602
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 that its buffer is bigger than this. I did this expecting the data to be read in one pass.
2
6877
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 there is a problem with the connection. I need to know which client has sent the incoming data as each client has its own buffer on my "server" app. I am using the standard asynch socket code from MSDN to listen for connections and they...
3
4310
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3168
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
5
12802
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? I thought about putting a boolean in there, but then what if it's still waiting for an incoming...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.