?? transactionserver.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.util.*;
import java.net.*;
public class TransactionServer implements Runnable {
protected Hashtable tags;
protected InputStream in;
protected OutputStream out;
public TransactionServer (InputStream in, OutputStream out) {
this.in = in;
this.out = out;
tags = new Hashtable ();
}
public void run () {
MessageInputStream messageIn = new MessageInputStream (in);
MessageOutputStream messageOut = new MessageOutputStream (out);
try {
while (true) {
messageIn.receive ();
Thread.sleep (1000);
String cmd = messageIn.readUTF ();
System.out.println (Thread.currentThread () + ": command " + cmd);
if (cmd.equals ("get")) {
doGet (messageIn, messageOut);
messageOut.send ();
} else if (cmd.equals ("put")) {
doPut (messageIn);
}
}
} catch (IOException ex) {
ex.printStackTrace ();
} catch (InterruptedException ignored) {
}
}
void doGet (DataInputStream dataIn, DataOutputStream dataOut) throws IOException {
String attr = dataIn.readUTF ();
dataOut.writeUTF (attr);
if (tags.containsKey (attr))
dataOut.writeUTF ((String) tags.get (attr));
else
dataOut.writeUTF ("null");
}
void doPut (DataInputStream dataIn) throws IOException {
String attr = dataIn.readUTF ();
String value = dataIn.readUTF ();
tags.put (attr, value);
}
public static void main (String[] args) throws IOException {
if (args.length != 2)
throw new IllegalArgumentException
("Syntax: TransactionServer <port> <threads>");
ServerSocket server = new ServerSocket (Integer.parseInt (args[0]));
Socket socket = server.accept ();
server.close ();
InputStream in = socket.getInputStream ();
OutputStream out = socket.getOutputStream ();
TransactionServer transactionServer = new TransactionServer (in, out);
int n = Integer.parseInt (args[1]);
for (int i = 0; i < n; ++ i)
new Thread (transactionServer).start ();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -