Socket&ServerSocket方式—-Blocking I/O
1对1模式(仅用于测试)
public class LinyServer {
public static void main(String[] args) {
System.out.println("Server: started...");
LinyServer svr = new LinyServer();
try {
svr.start();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server: bye!");
}
public void start() throws IOException {
ServerSocket ss = new ServerSocket(9998);
Socket s = null;
s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream());
String line = null;
while((line=in.readLine()) != null) {
System.out.println("Receive: " + line);
String res = line + "Svr";
System.out.println("Send: " + res);
out.println(res);
out.flush();
}
}
}
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 9998);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
Scanner scanner = new Scanner(System.in);
String keyIn = "";
while(scanner.hasNext()) {
keyIn = scanner.next();
out.println(keyIn);
out.flush();
System.out.println("Send: " + keyIn);
String read = in.readLine();
System.out.println("Receive: " + read);
}
}
1对多模式
public class LinyServer {
public static void main(String[] args) {
System.out.println("Server: started...");
LinyServer svr = new LinyServer();
try {
svr.start();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Server: bye!");
}
public void start() throws IOException {
ServerSocket ss = new ServerSocket(9998);
while(true) {
Socket s = ss.accept();
new LinyThread(s).start();
}
}
}
public class LinyThread extends Thread {
private Socket mSocket;
public LinyThread(Socket s) {
super();
mSocket = s;
}
@Override
public void run() {
System.out.println("Started: " + Thread.currentThread());
try {
BufferedReader in = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
PrintWriter out = new PrintWriter(mSocket.getOutputStream());
String line = null;
while((line=in.readLine()) != null) {
System.out.println(Thread.currentThread() + "Rceive: " + line);
String res = line + "--> from server" + Thread.currentThread();
System.out.println(Thread.currentThread() + "Send: " + res);
out.println(res);
out.flush();
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
public class LinyClient {
public static void main(String[] args) throws Exception {
test();
}
public static void test() throws Exception {
int i = 0;
Socket s1 = new Socket("127.0.0.1", 9998);
BufferedReader in1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));
PrintWriter out1 = new PrintWriter(s1.getOutputStream(), true);
Socket s2 = new Socket("127.0.0.1", 9998);
BufferedReader in2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));
PrintWriter out2 = new PrintWriter(s2.getOutputStream(), true);
String keyIn = "";
BufferedReader in;
PrintWriter out;
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
keyIn = scanner.next();
if(i%2 == 0) {
in = in1;
out = out1;
} else {
in = in2;
out = out2;
}
out.println(keyIn);
out.flush();
System.out.println("Send: " + keyIn);
String read = in.readLine();
System.out.println("Receive: " + read);
i++;
}
}
}