Overview
Features
Download
Documentation
Community
Add-Ons & Services

Determining if StreamSocket is still connected

Please post support and help requests here.

Determining if StreamSocket is still connected

Postby DougE » 21 Aug 2008, 17:12

Hi,

I have a process that waits for user input, while waiting I want to check to see if the server I'm connected to is still connected. If it has gone away I want to stop waiting. I'm using a StreamSocket in blocking mode. Currently, I'm doing a getBytes with a timeout and checking for the exception. This doesn't seem very elegant. Is there a more efficient way to handle this?

Thanks,
Doug
DougE
 
Posts: 13
Joined: 15 Jul 2008, 17:44

Re: Determining if StreamSocket is still connected

Postby alex » 22 Aug 2008, 11:46

> I have a process that waits for user input, while waiting I want to check to see if the server I'm connected to is still connected. If it has gone away I want to stop waiting. I'm using a StreamSocket in blocking mode. Currently, I'm doing a getBytes with a timeout and checking for the exception. This doesn't seem very elegant. Is there a more efficient way to handle this?

Event maybe, but I'm not clear on what exactly you are doing. Can you post a snippet of code?

Alex
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Determining if StreamSocket is still connected

Postby DougE » 22 Aug 2008, 21:47

Here is the code I'm currently using:

bool valid = true;
// Set timeout to 50ms
Timespan t(0L, 50000L);
try
{
try
{
char buf[2];
_socket.setReceiveTimeout(t);
_socket.receiveBytes(buf, 2, MSG_PEEK);
}
catch (TimeoutException&)
{
}
// Reset timeout to 0
t.assign(0L, 0L);
_socket.setReceiveTimeout(t);
}
catch (Exception& ex)
{
valid = false;
}

I'm looping waiting for user input. If something happens I process it then start waiting again. If nothing to do I check my connection. If the server goes away, I want to interrupt my user input loop and handle things.

The code above works, it just seems like overkill. On my computer it takes about 600ms to complete if the connection is still valid.

Thoughts?

Doug
DougE
 
Posts: 13
Joined: 15 Jul 2008, 17:44

Re: Re: Determining if StreamSocket is still connected

Postby alex » 24 Aug 2008, 15:45

> Thoughts?

Try using Socket::poll() call. Also, depending on how much CPUs/cores you have available, you may achieve some improvement if you separate functionalities (from what I gather, there are 2 connections in play) into their own threads of execution.

Alex
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron