您好我已经编写了一个服务器程序来接收ISO8583-93版本请求处理它们并发送响应。我将接收连续请求。它工作正常,但如果套接字在下一个请求到来时处于空闲状态,则服务器无法读取。 请在下面找到代码段
服务器:
public class MBServ {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listening = true;
String request_date = null;
String request_time = null;
try {
serverSocket = new ServerSocket(7777);
} catch (IOException e) {
System.err.println("Could not listen on port: 7777.");
System.exit(-1);
}
while (listening) {
new MBServT(serverSocket.accept()).start();
}
serverSocket.close();
}
}
主题:
public class MBServT extends Thread {
private Socket socket = null;
Logger log = Logger.getLogger(MBServT .class.getName());
public MBServT(Socket socket) throws FileNotFoundException, IOException {
super("MBServT");
this.socket = socket;
}
public void run() {
String inputLine = "";
String msgType = null;
int response = 12;
String outwardMsg = null;
BufferedReader buffReaderObj = null;
// String ip = "172.30.12.69";
String stanNo = "0";
boolean ISSUBFIELDPARSING = false;
GenericPackager packager;
try {
packager = new GenericPackager("basicparse.xml");
BufferedReader is = new BufferedReader(new InputStreamReader(socket
.getInputStream(), "ISO8859_1"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream(), "ISO8859_1"));
String strBuf = null;
int length = 4;
char[] chrBuf = new char[length];
int cnt = 0;
while (true) {
socket.setKeepAlive(true);
int value = 0;
int ret = 0;
ret = is.read(chrBuf, 0, chrBuf.length);
if (-1 == ret)
{
log.error("nothing to read closing socket");
try{
socket.close();
}
catch(Exception e){
System.out.println("Error in socket.close: " +e.toString());
}
throw new Exception("Read Error: Socket closed");
}
strBuf = new String(chrBuf);
chrBuf = new char[Integer.parseInt(strBuf)];
is.read(chrBuf, 0, chrBuf.length);
strBuf = new String(chrBuf);
chrBuf = null;
chrBuf = new char[4];
value = 0;
/*writing response*/
Runnable respThread = new MBServRespT(writer, fieldList,
"threadname");
((Thread) respThread).start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error::" + e.toString());
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
答案 0 :(得分:-3)
在我们实现set sockettimeout之后,它工作了。