我写的一个多线程的Socket服务器,在线程内部写个定时程序,如果定时时间到则关闭Socket,在windows下好使,到时间socket可以关掉,但是在linux下不好使,不知道为什么,请高手指点!!
这是一个线程类,其它程序中建立服务器端ServerSocket,如果侦听到链接则调用该类!
程序如下:
//多线程类:EchoMultiServerThread.java
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class EchoMultiServerThread extends Thread {
private final Timer timer = new Timer();
private Socket Ssocket = null;
private OutputStream out = null;
private InputStream in = null;
protected Log logger = LogFactory.getLog(getClass());
public void start1() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound()
{
try{
logger.warn("cennect time out");
out.close();
in.close();
Ssocket.close();
Thread.currentThread().interrupt();
}
catch(SocketException e){
logger.warn("Socket close error");}
catch (IOException e) {
logger.warn("IO error");
}
}
}, 30000);
}
public EchoMultiServerThread(Socket socket) {
super("EchoMultiServerThread");
this.Ssocket = socket;
}
public void run() {
try{
out = Ssocket.getOutputStream();
in = Ssocket.getInputStream();
}
catch (Exception e) {
logger.warn("IO error");
}
this.start1();
try{
String datastr="",
strreq="";
int tmpcount=0;
byte[] tmp1=new byte[3000];
try{
tmpcount=in.read(tmp1);
}
catch(Throwable e){
in.close();
out.close();
Ssocket.close();
}
if(tmpcount>0){
byte[] datastrbytes=new byte[tmpcount];
System.arraycopy(tmp1,0,datastrbytes,0,tmpcount);
datastr=new String(datastrbytes);
}
else{
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
if(datastr == null){
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
else{
if (datastr.length() == 0 || datastr.length() >100)
{in.close();
out.close();
Ssocket.close();
timer.cancel();}
int ldatastr=datastr.length();
strreq=datastr.substring(1,ldatastr-1);
logger.info("Request data: "+strreq); String strres ="";
strres = strreq + "end";
if(strres != null && strres.length() >0 && strres.length() <=100){
out.write(strres.getBytes());
out.flush();
logger.info("Response data: "+strres);
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
else{
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
}
}catch(Exception e){}
}
}
这是一个线程类,其它程序中建立服务器端ServerSocket,如果侦听到链接则调用该类!
程序如下:
//多线程类:EchoMultiServerThread.java
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class EchoMultiServerThread extends Thread {
private final Timer timer = new Timer();
private Socket Ssocket = null;
private OutputStream out = null;
private InputStream in = null;
protected Log logger = LogFactory.getLog(getClass());
public void start1() {
timer.schedule(new TimerTask() {
public void run() {
playSound();
timer.cancel();
}
private void playSound()
{
try{
logger.warn("cennect time out");
out.close();
in.close();
Ssocket.close();
Thread.currentThread().interrupt();
}
catch(SocketException e){
logger.warn("Socket close error");}
catch (IOException e) {
logger.warn("IO error");
}
}
}, 30000);
}
public EchoMultiServerThread(Socket socket) {
super("EchoMultiServerThread");
this.Ssocket = socket;
}
public void run() {
try{
out = Ssocket.getOutputStream();
in = Ssocket.getInputStream();
}
catch (Exception e) {
logger.warn("IO error");
}
this.start1();
try{
String datastr="",
strreq="";
int tmpcount=0;
byte[] tmp1=new byte[3000];
try{
tmpcount=in.read(tmp1);
}
catch(Throwable e){
in.close();
out.close();
Ssocket.close();
}
if(tmpcount>0){
byte[] datastrbytes=new byte[tmpcount];
System.arraycopy(tmp1,0,datastrbytes,0,tmpcount);
datastr=new String(datastrbytes);
}
else{
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
if(datastr == null){
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
else{
if (datastr.length() == 0 || datastr.length() >100)
{in.close();
out.close();
Ssocket.close();
timer.cancel();}
int ldatastr=datastr.length();
strreq=datastr.substring(1,ldatastr-1);
logger.info("Request data: "+strreq); String strres ="";
strres = strreq + "end";
if(strres != null && strres.length() >0 && strres.length() <=100){
out.write(strres.getBytes());
out.flush();
logger.info("Response data: "+strres);
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
else{
in.close();
out.close();
Ssocket.close();
timer.cancel();
}
}
}catch(Exception e){}
}
}
import java.io.*; public class MultiUser extends Thread{
private Socket client; public MultiUser(Socket c){
this.client=c;
} public void run(){
try{
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter out=new PrintWriter(client.getOutputStream());
//String str=in.readLine();
//System.out.println(str);
out.println("----welcom--------");
out.flush();
//Mutil User but can parallel
while(true){
//
String str=in.readLine();
System.out.println(str);
out.println("has receive....");
out.flush();
in.close();
out.close();
client.close(); //在这里断开socket,但是在windows下好使,linux下不好使!!
if(str.equals("end"))
break;
}
client.close();
}catch(IOException ex){
}finally{
}
} public static void main(String[] args)throws IOException{
ServerSocket server=new ServerSocket(5678);
while(true){
//transfer location change Single User or Multi User
MultiUser mu=new MultiUser(server.accept());
mu.start();
}
}
}
其实我就是想在服务器端写个超时,到时间了自动断开socket连接,有没有其它更好的办法啊!
client=null;
应该可以的
--------------------------------
你看一下ServerSocket的构造方法,其中有一个可以设定连接超时的。