简体   繁体   中英

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.

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.

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.

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.

You should use single ServerSocket, and once serverSocket.accept() return socket, then you can store it to array and check it's status frequently. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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