本帖最后由 freehello 于 2015-8-29 21:23 编辑
- public class ServerTcp {
- public static void main(String[] args) throws Exception {
- ServerSocket ss = new ServerSocket(10000);
- while(true) {
- Socket s = ss.accept(); //这里每接受一次会创建一个新的Socket吗?为什么可以同时连接多个客户
- new Thread(new run(s)).start();
- }
- }
- }
- class run implements Runnable {
- private Socket s;
- public run (Socket s) {
- this.s = s;
- System.out.println(s);
- }
- public void run() {
- while(true) {
- try {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(s.getInputStream()));
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- String str = null;
- while((str=br.readLine()) != null) {
- System.out.println(str);
- pw.println("world");
- }
- }catch (Exception e){}
- }
- }
- }
复制代码
|
|