【发布时间】:2014-02-23 11:17:44
【问题描述】:
我启动服务器程序,它只等待客户端 30 秒。它在第一次迭代中运行良好,在剩余的迭代中不等待。任何建议。
这里
minLinkWt() sets the index.
但在程序中保持不变。
import java.sql.*;
import java.net.*;
import java.lang.*;
class Democ{
int index,port,min=100;
ServerSocket ss=null;
Socket s=null;
void begin(){
int av=0;boolean b=false;
minLinkWt();
while(!b){
av=checkStatus(index);
if(av==1){b=true;}
}
if(av==1)
Connect();
else
System.out.println("No Routers Available");
}
void Connect()
{
System.out.println("Enter the Message to send to clients::");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String msg=br.readLine();
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println(msg);
}catch(Exception e){e.printStackTrace();}
}
void callSwitch(int index_formal)
{
switch(index_formal)
{
case 1:
port=2000;
break;
case 2:
port=2001;
break;
case 3:
port=2002;
break;
case 4:
port=2003;
break;
default:
System.out.println("No Routers in Available");
}
}
int checkStatus(int index_formal){
try
{
ss=new ServerSocket(port);
ss.setSoTimeout(30000);
s=ss.accept();
}catch(InterruptedIOException e){
System.out.println("Cannot connect through Router1 Waiting for Router2");}
catch(Exception g){g.printStackTrace();}
if(s==null)
return 0;
else
return 1;
}
class DemoCopy{
public static void main(String s[])
{
Democ obj=new Democ();
obj.begin();
}
}
因此,在每次迭代中,服务器都必须等待客户端,但它不会等待。 我得到的输出为
hello
hello
hello
hello
min is6
AT index2
Cannot connect through Router1 Waiting for Router2
No Routers Available
【问题讨论】:
标签: java sockets while-loop timeout