How do you cancel a ServerSocket.accept while it's waiting?

Hello all,

While my program is listening for an accept....

ServerSocket serverListen;

Socket clientSocket;

clientSocket = serverListen.accept( );

I would like to have some kind of dialog box be visible that would give the user to hit cancel and then the serverListen.accept( ) would stop. I'm guessing I would use something like serverListen.close(), but I'm not sure how to get the message box up, then be able to listen for the button click and stop the accpet line.

Also, I would like for it to automatically have the Dialog box close if a connection was made.

Do I need to use setSoTimeout( ) somehow?

Thanks

[678 byte] By [philcollins] at [2007-9-26 2:16:21]
# 1

You could set the timeout to be something like 1 second and look many times through an accept method

// show a dialog (Frame) with Cancel button

servsock.setSoTimeOut(1000);

while(!dialogClickedCancel)

{

try

{

clientsock = servsock.accept();

}

catch(Exception e)

{

}

}

// connection was made, bring down the Frame

schillj at 2007-6-29 9:14:41 > top of Java-index,Archived Forums,Java Programming...
# 2
thanks, i'll try that.but I'm not sure how I can manually close a dialog window. I'm guessing it will be something like if (dialog.isShowing) dialog.close()?
philcollins at 2007-6-29 9:14:41 > top of Java-index,Archived Forums,Java Programming...
# 3

I'm not getting this to work.

I have tried this and it doesn't work...

boolean connect = false;

int result = Mbox.showConfirmDialog(this,"Now waiting for opponent.\nPress cancel to stop waiting.","Now Waiting",JOptionPane.CANCEL_OPTION);

while(result!= JOptionPane.CANCEL_OPTION || connect == false){

try{

clientSocket = serverListen.accept();

Mbox.setVisible(false);

connect = true;

}

catch(Exception e){}

}

if(result == JOptionPane.CANCEL_OPTION){

Mbox.showMessageDialog(this,"Waiting aborted","",1);

cancelWasHit();

}

else{

//do my other stuff.

I think it just sits there and purposly waits for the user to hit something and it wont continue to process anything in the while loop.

hmmm....

any sugjestions?

philcollins at 2007-6-29 9:14:41 > top of Java-index,Archived Forums,Java Programming...
# 4
> while(result!= JOptionPane.CANCEL_OPTION || connect == false){shouldn't that bewhile( (result != JOptionPane.CANCEL_OPTION) && (connect == false) ){
parthasarkar at 2007-6-29 9:14:41 > top of Java-index,Archived Forums,Java Programming...
# 5

I'm pretty new to sockets (just started playing around with them this past weekend) but I did find a way to do this. It seems to work Ok but if anyone knows a better way let me know!

The serverListen.accept() blocks the thread it's running on which makes any type of GUI a little more difficult. I put the code that gets the socket connection in its own thread, and then let the code that handles the dialog box periodically ask the server thread if a connection had been established. If the user clicked the "cancel" button then a method in the thread that had serverListen.close() was called.

I was using a JFrame for the dialog box, so I imagine you could close it quite easily once the connection was made or if the user hit cancel.

Hope this helps!

markgallina at 2007-6-29 9:14:41 > top of Java-index,Archived Forums,Java Programming...