首页 > 编程语言 > 详细

服务器端启动soket多线程

时间:2015-08-20 01:09:24      阅读:201      评论:0      收藏:0      [点我收藏+]

 

方法一:

Socket socket=null

try{

  ServerSocket serversocket=nwe ServerSocket(8080)

  while(true){

    socket=serversocket.apccet();

    new ServerThread(socket).start();

  }

}catch(IOException e){

  e.printStackTrace()

}

 

方法二:

public  class ServerThread extends Thread{

  private Socket socket;

  public ServerSocket(socket){

    this.socket=socket;

  }

  public void run(){

      //复写run方法

    InputStream is=null;

    OutputStream os=null;

    String ip=socket.getLocalAddress().getHostAddress();

    String port=socket.getPort();

    try{

      while(true){

        in=socket.getInputStream();

        byte[] buffer=new byte[1024];

        int index=in.read(buffer);

        String receive=new String(buffer,0,index);

        System.out.println ("接受到服务器的内容是"+receive);

        if(recive=="exit") break;

        out=sokect.getOutputStream();

        String mes="word";

        out.write(mes.getBytes());

        System.out.println ("向服务器发送的内容:"+receive);

      }

      in.close();

      out.close();

      socket.close();

    }catch(IOException){

      e.printStackTrace();

    }

  }

}

服务器端启动soket多线程

原文:http://www.cnblogs.com/java-php/p/4743849.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!