java中在创建客户端socket通信应用时候,经常会遇到一些莫名的异常信息。现根据异常情况,处理如下:
try {
socket = new Socket(ip, port);
this.isConnect = false;
} catch (UnknownHostException e) {
strErrorDesc = "没有发现主机";
this.isConnect = true;
} catch (ConnectException e) {
strErrorDesc = "连接被拒绝";
this.isConnect = true;
} catch (SocketTimeoutException e) {
strErrorDesc = "连接超时";
this.isConnect = true;
} catch (IOException e) {
strErrorDesc = "连接失败";
this.isConnect = true;
}
/*
* 下面两行代码主要是判断远端服务器是否断开的处理
*/
try {
socket.setOOBInline(false);
socket.sendUrgentData(0xFF);
} catch (Exception ex) {
strErrorDesc = "服务器端口连接断开";
}
try {
socket = new Socket(ip, port);
this.isConnect = false;
} catch (UnknownHostException e) {
strErrorDesc = "没有发现主机";
this.isConnect = true;
} catch (ConnectException e) {
strErrorDesc = "连接被拒绝";
this.isConnect = true;
} catch (SocketTimeoutException e) {
strErrorDesc = "连接超时";
this.isConnect = true;
} catch (IOException e) {
strErrorDesc = "连接失败";
this.isConnect = true;
}
/*
* 下面两行代码主要是判断远端服务器是否断开的处理
*/
try {
socket.setOOBInline(false);
socket.sendUrgentData(0xFF);
} catch (Exception ex) {
strErrorDesc = "服务器端口连接断开";
}