简体   繁体   English

使服务器套接字在每次迭代中等待客户端套接字

[英]Making Server Socket to wait for Client Socket in each iteration

I start Server program and it waits for Client for just 30 seconds.It works fine in the first iteration and does not wait in the remaining iterations.Any suggestions. 我启动了Server程序,它只等待Client 30秒。它在第一次迭代中工作正常,而在其余的迭代中不等待任何建议。

Here the 在这里

minLinkWt() sets the index. 

It is however remains the same in the program. 但是,它在程序中保持不变。

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();
}
}

So At every iteration The Server has to wait for the Client but its not waiting. 因此,在每次迭代中,服务器必须等待客户端,但不等待。 I get the output as 我得到的输出为

hello
hello
hello
hello
min is6
AT index2
Cannot connect through Router1 Waiting for Router2
No Routers Available

It looks like that "index" in the begin() can be index of a array, so you want to check the status of the Server socket port form 0 to x or something. 看起来begin()中的“索引”可以是数组的索引,因此您要检查服务器套接字端口从0到x的状态。

Probably you should post more source code, but it looks like you are using single port value in the checkStatus(), which means ServerSocket will be bound same port every time. 可能您应该发布更多的源代码,但是看起来您在checkStatus()中使用的是单个端口值,这意味着ServerSocket每次都将绑定到同一端口。

It's OK at first iteration because server socket is not bound at any port, but at then end of first iteration you didn't close server socket at all. 第一次迭代就可以了,因为服务器套接字没有绑定到任何端口,但是在第一次迭代结束时,您根本没有关闭服务器套接字。

So Server socket is still bound at given port, and creating new ServerSocket with same port number will be failed since it's already bound and you can't bind it again unless you close it first. 因此,服务器套接字仍绑定在给定的端口上,并且使用相同的端口号创建新的ServerSocket将会失败,因为它已经被绑定,除非再次关闭它,否则无法再次绑定它。

You should use single ServerSocket, and once serverSocket.accept() return socket, then you can store it to array and check it's status frequently. 您应该使用单个ServerSocket,并且一旦serverSocket.accept()返回套接字,就可以将其存储到数组并经常检查其状态。 Or maybe you can differentiate port number every time and let client connect different port every time would also work, but I don't think that's what you want to do. 也许您可以每次都区分端口号,让客户端每次都连接不同的端口也可以,但是我认为这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM