A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 彭璞 中级黑马   /  2012-11-28 17:26  /  1311 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 彭璞 于 2012-11-28 22:12 编辑

短连接只能一次发送 ,怎么样可以长期连接

1 个回复

倒序浏览
本帖最后由 Tokgo 于 2012-11-28 17:51 编辑

现编这个就是个多线程服务器,只要在client不释放连接,服务器端的run里边写while(TRUE)循环,那么就可以长期连接。
class ConnectionThread extends Thread{
Socket client;
int counter;
public ConnectionThread(Socket cl,int c){
client = cl;
counter= c;
}
    @Override
public void run(){
  try{
       String destIP=client.getInetAddress().toString();
       int destport =client.getPort();
       PrintStream outstream=new PrintStream(client.getOutputStream());
       DataInputStream instream=new DataInputStream(client.getInputStream());
        String inline=instream.readLine();
}//try
catch(IOException e){
System.out.println(e);
}
}//run

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马