java - What is the input error here? And how do I connect via IP -
i'm trying make simple echo server can basics down code doesn't run properly. prompts port scanner (keyboard) wigs out. can perpetually hit enter without errors breaking program. i've had problem before forgot how fixed then.
additionally, i've been having trouble creating client socket inet4address. continually mismatch error saying numbers in ip large. appreciated
public static void main(string[] args) { try { system.out.println("port: "); int port = keyboard.nextint(); service = new serversocket(port); clientsocket = service.accept(); input = new bufferedreader(new inputstreamreader(clientsocket.getinputstream())); output = new printstream(clientsocket.getoutputstream()); system.out.println(service.getinetaddress()); while(true) { line = input.readline(); output.println(line); } } catch(ioexception e) { system.out.println(e); } }
in client side:
for(int = 0; < 4; i++) //taking in ip in byte parts { system.out.println("ip part " + (i + 1) + " : "); ip[i] = byte.valueof(keyboard.nextline()); } system.out.println("port: "); int port = keyboard.nextint();
i assume error you're getting in client this:
ip part 1 : 192 exception in thread "main" java.lang.numberformatexception: value out of range. value:"192" radix:10 @ java.lang.byte.parsebyte(unknown source) @ java.lang.byte.valueof(unknown source) @ java.lang.byte.valueof(unknown source) @ client.main(client.java:12)
the reason you're getting error in java, byte
s signed, i.e. in range -128 127. byte.valueof
parse values in range. if want input numbers outside of range, can't use byte.valueof
.
despite fact byte
s can't hold values greater 127, still correct type of value pass inetaddress.getbyaddress()
:
// prints "/192.168.0.1" system.out.println(inetaddress.getbyaddress(new byte[] { (byte)192, (byte)168, 0, 1}));
one solution use parsing methods of larger primitive type instead, , convert result byte
, example:
ip[i] = short.valueof(keyboard.nextline()).bytevalue();
or
ip[i] = (byte)short.parseshort(keyboard.nextline());
as range of short
-32768 32767, work values in range 0 255.
both short.valueof
, short.parseshort
same thing; differ in type of value return. former returns short
object, byte
value of calling bytevalue()
method. latter returns short
primitive, convert byte
casting it.
note approach accept values outside range 0 255, such 1000 or -12345. may not problem, given you're using program own educational purposes.
incidentally, struggled compile server code. have omitted import
statements , declarations of variables (possibly static fields). of variables had names beginning capital letters, thinking myself 'which package import keyboard
from?' turns out keyboard
variable of type scanner
. these capitalised variable names, combined missing variable/field declarations , import directives, made rather confusing code. conventions may not important code keep yourself, rather more important if want others read code , not confused it.
Comments
Post a Comment