public class Sever { ServerSocket ss = null; public Sever() {
try {
init();
} catch (Exception e) { System.out.println(e.getMessage());
}
} void init() throws Exception {
ss = new ServerSocket(6666);
System.out.println(ss);
} void waitForClient() {
try {
while (true) {
System.out.println("Waiting for connect!");
Thread t = new Thread(new Client(ss.accept()));
t.start();
} } catch (Exception e) {
System.out.println("waitForClient ->" + e.getClass().getName()
+ ":" + e.getMessage());
} } public static void main(String[] args) {
Sever sever = new Sever();
sever.waitForClient();
}
}
try {
init();
} catch (Exception e) { System.out.println(e.getMessage());
}
} void init() throws Exception {
ss = new ServerSocket(6666);
System.out.println(ss);
} void waitForClient() {
try {
while (true) {
System.out.println("Waiting for connect!");
Thread t = new Thread(new Client(ss.accept()));
t.start();
} } catch (Exception e) {
System.out.println("waitForClient ->" + e.getClass().getName()
+ ":" + e.getMessage());
} } public static void main(String[] args) {
Sever sever = new Sever();
sever.waitForClient();
}
}
private BufferedReader inStream = null; private PrintWriter outStream = null; private Socket s = null; public Client(Socket socket) {
s = socket;
try {
System.out.println("Socket accepted!");
s.setSoTimeout(300000);
inStream = new BufferedReader(new InputStreamReader(s
.getInputStream()));
outStream = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(s.getOutputStream())), true); } catch (Exception e) {
} } /**
* 用户中止程序执行
*/
public void destroy() { } public void run() { try { while (true) { String str = inStream.readLine();
System.out.println("Got String :" + str);
//对内容进行处理..
}
} catch (Exception e) {
} finally {
try {
destroy();
inStream.close();
outStream.close();
s.close();
} catch (Exception e) {
} } }
}