0
我有一个程序和一个需要相互通信的文字服务器。我能够做到这一点,发送信息到服务器并再次接收。 我有以下代码:拆分字符串时无效的端口号 - Java Sockets
try{
Socket sock=new Socket(InetAddress.getByName(null), 12837);
InputStream s=sock.getInputStream();
PrintStream out = new PrintStream(sock.getOutputStream());
BufferedReader r= new BufferedReader(new InputStreamReader(s));
String line = r.readLine();
String[] code = line.split("+");
while (line != null) {
System.out.println ("received: " + line);
// System.out is to the console.
// out.println is to the server
out.println("161*");
line = r.readLine();
}
} catch (UnknownHostException e) {
System.out.println("Unknown host. Check the hostname or ip address of the server");
} catch (ConnectException e) {
System.out.println("Problems connecting to server. Is it running?");
} catch (NumberFormatException e) {
System.out.println("Port number should be an integer");
} catch (IllegalArgumentException e) {
System.out.println("The port number needs to be less than 65536");
} catch (Throwable ex){System.out.println ("Exception: " + ex.toString());
}
的想法是,我启动了字服务器,运行我的应用程序,它就会收到消息"<bonjour+[code]>"
其中<code>
是6个随机字母和数字。我需要的是回显到服务器我的用户ID是161*<code>
。
例如服务器说"<bonjour+wj1234>"
,我需要回显"161*wj1234"
。
我想最简单的方法是在+
分裂从服务器上线(我还需要去除>
),但在运行它时,我得到以下错误:
的端口号需要小于65536
它这样做,即使我没有做任何与拆分字符串。分割字符串本身的行为是由于某种原因改变了端口号,我手动设置为12837
而不改变。
您正在捕获“IllegalArgumentException”,然后打印有关端口号的错误消息。检查“IllegalArgumentException”来自哪里。尝试在''System.out.println''之前放置''e.printStackTrace()''。 – f1sh
代码中的某处IlegalArgumentException被抛出,您的catch会打印关于端口的那部分内容,但它从未真正改变过。 –
http://stackoverflow.com/questions/2198373/java-how-to-split-a-string-on-plus-signs – Reimeus