套接字仅在 1 分钟后抛出异常
Socket throw exception only after 1 minutes
我最近开发了一个应用程序,可以使用 java 套接字连接到 PC 中的 java 服务器套接字。这段代码在连接到服务器时完美运行,它在调用线程后立即发送 String 但我不知道为什么当我关闭服务器并尝试它时它没有立即抛出异常但 1 分钟后页面空闲然后只有异常调用的页面。
服务器打开和关闭时的 logcat 文件:
https://imgur.com/a/lOqdq
public class checksession implements Runnable
{
private Activity activity;
Socket soc;
public checksession(Activity activity)
{
//get activity from class called
this.activity=activity;
//this.soc=soc;
}
@Override
public void run()
{
try{
soc=new Socket("192.168.0.113",11000);
DataOutputStream dout=new DataOutputStream(soc.getOutputStream());
//request format --> requestkey-field required
//format for LG request--> LG-username-password
String sendrequest="SS";
//send requestcode to server
dout.writeUTF(sendrequest);
dout.flush();//refresh to make sure it send to the server
//wait for input
DataInputStream dis=new DataInputStream(soc.getInputStream());
final String codefromserver=(String)dis.readUTF();
System.out.println("reply:"+codefromserver);
String replycode[]= codefromserver.split("-");
//server reply code format
// if not used on database RE-CK-NO
//if used on database RE-CK-YES
String sessionavailableornot=replycode[2];
if(sessionavailableornot.equals("YES"))
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Do your UI operations like dialog opening or Toast here
//navigate user to main screen
Intent in = new Intent(activity, sessiondetected.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(in);
}
});
}
soc.close();
}catch(Exception e) {
//runOnUiThread function is to let the function to be run on main thread
//bacause UI function cannot be run on worker thread
activity.runOnUiThread(new Runnable() {
public void run() {
//Do your UI operations like dialog opening or Toast here
//navigate user back to connectionerror page so that user know
Intent inerr = new Intent(activity, serverconnectionerror.class);
inerr.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(inerr);
}
});
}
}
}
您需要为套接字指定超时值;否则将使用平台默认值。
以下代码在打开套接字之前设置 SO_TIMEOUT
(套接字读取超时)和连接超时值。
final int timeout = 2000; // in millis
soc = new Socket();
soc.setSoTimeout(timeout);
soc.connect(new InetSocketAddress("192.168.0.113", 11000), timeout);
我最近开发了一个应用程序,可以使用 java 套接字连接到 PC 中的 java 服务器套接字。这段代码在连接到服务器时完美运行,它在调用线程后立即发送 String 但我不知道为什么当我关闭服务器并尝试它时它没有立即抛出异常但 1 分钟后页面空闲然后只有异常调用的页面。
服务器打开和关闭时的 logcat 文件: https://imgur.com/a/lOqdq
public class checksession implements Runnable
{
private Activity activity;
Socket soc;
public checksession(Activity activity)
{
//get activity from class called
this.activity=activity;
//this.soc=soc;
}
@Override
public void run()
{
try{
soc=new Socket("192.168.0.113",11000);
DataOutputStream dout=new DataOutputStream(soc.getOutputStream());
//request format --> requestkey-field required
//format for LG request--> LG-username-password
String sendrequest="SS";
//send requestcode to server
dout.writeUTF(sendrequest);
dout.flush();//refresh to make sure it send to the server
//wait for input
DataInputStream dis=new DataInputStream(soc.getInputStream());
final String codefromserver=(String)dis.readUTF();
System.out.println("reply:"+codefromserver);
String replycode[]= codefromserver.split("-");
//server reply code format
// if not used on database RE-CK-NO
//if used on database RE-CK-YES
String sessionavailableornot=replycode[2];
if(sessionavailableornot.equals("YES"))
{
activity.runOnUiThread(new Runnable() {
public void run() {
//Do your UI operations like dialog opening or Toast here
//navigate user to main screen
Intent in = new Intent(activity, sessiondetected.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(in);
}
});
}
soc.close();
}catch(Exception e) {
//runOnUiThread function is to let the function to be run on main thread
//bacause UI function cannot be run on worker thread
activity.runOnUiThread(new Runnable() {
public void run() {
//Do your UI operations like dialog opening or Toast here
//navigate user back to connectionerror page so that user know
Intent inerr = new Intent(activity, serverconnectionerror.class);
inerr.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(inerr);
}
});
}
}
}
您需要为套接字指定超时值;否则将使用平台默认值。
以下代码在打开套接字之前设置 SO_TIMEOUT
(套接字读取超时)和连接超时值。
final int timeout = 2000; // in millis
soc = new Socket();
soc.setSoTimeout(timeout);
soc.connect(new InetSocketAddress("192.168.0.113", 11000), timeout);