如何干净的实现Android/Java Socket 长连接通信
展开全部
你好,现编这个就是个多线程服务器,只要在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
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
2016-03-22 · 知道合伙人互联网行家
护肤达人IT宅族
知道合伙人互联网行家
向TA提问 私信TA
知道合伙人互联网行家
采纳数:5637
获赞数:17441
毕业于曲阜师范大学,学士学位。互联网行业2年从业经验,读过SEO相关书籍。现任爱家网SEO优化专员。
向TA提问 私信TA
关注
展开全部
现编这个就是个多线程服务器,只要在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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
现编这个就是个多线程服务器,只要在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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询