我启动服务器程序客户端套接字,并等待客户端只需30 seconds.It工作正常,在第一次迭代,并在剩余iterations.Any建议不等待。制作服务器套接字等待在每次迭代
这里
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
我已经设定服务器来监听不同的端口号在同一program.When它第一次听上说的端口号2000,然后再次,如果在我的代码我试图更改端口号,然后我得到连接重置exception.So我想到了上面这段代码。 – Intriguing
你能建议我什么时候我应该打开和关闭连接套接字以获得在这种情况下等待的时间。 – Intriguing
等待......如果您编程服务器每次听取不同的端口,您如何让客户知道哪些端口可用?根据你的问题“让服务器套接字在每次迭代中等待客户端套接字”,你应该只打开一个特定端口号为ONLY ONCE的服务器套接字,并在while循环中调用serverSocket.accept(),这样服务器就会建立连接并返回连接到客户端的套接字对象。 – wonhee