?? nbserver.java
字號:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.net.*;
public class NBServer {
static InputStream in0, in1;
static OutputStream out0, out1;
public static void main (String[] args) throws IOException {
if (args.length != 1)
throw new IllegalArgumentException ("Syntax: NBServer <port>");
try {
accept (Integer.parseInt (args[0]));
int x0, x1;
while (((x0 = readNB (in0)) != -1) &&
((x1 = readNB (in1)) != -1)) {
if (x0 >= 0)
out1.write (x0);
if (x1 >= 0)
out0.write (x1);
}
} finally {
System.out.println ("Closing");
close (out0);
close (out1);
}
}
static void close (OutputStream out) {
if (out != null) {
try {
out.close ();
} catch (IOException ignored) {
}
}
}
static void accept (int port) throws IOException {
System.out.println ("Starting on port " + port);
ServerSocket server = new ServerSocket (port);
try {
System.out.println ("Waiting..");
Socket client0 = server.accept ();
System.out.println ("Accepted from " + client0.getInetAddress ());
in0 = client0.getInputStream ();
out0 = client0.getOutputStream ();
out0.write ("Welcome. Please wait.\r\n".getBytes ("latin1"));
System.out.println ("Waiting..");
Socket client1 = server.accept ();
System.out.println ("Accepted from " + client1.getInetAddress ());
in1 = client1.getInputStream ();
out1 = client1.getOutputStream ();
out1.write ("Welcome.\r\n".getBytes ("latin1"));
out0.write ("Proceed.\r\n".getBytes ("latin1"));
} finally {
server.close ();
}
}
static int readNB (InputStream in) throws IOException {
if (in.available () > 0)
return in.read ();
else
return -2;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -