How to reset a socket?

  • Thread starter Thread starter Diego F.
  • Start date Start date
D

Diego F.

Hello.

I'm writing an application that manages sockets and has a message protocol.
I connect to another machine and I have to send and receive certain
messages.

I need the possibility to restart the communication. I tried with a simple
socket.close method and trying to connect again as the first time, but the
connection is refused.

Is it possible to do that?
 
here it is

/// <summary>
/// Description: Check for dormant sockets and close them.
/// </summary>
/// <param name="eventState">Required parameter for a timer
call back
/// method.</param>
private void CheckSockets(object eventState)
{
lostTimer.Change(System.Threading.Timeout.Infinite,
System.Threading.Timeout.Infinite);
try
{
foreach (StateObject state in connectedSocks)
{
if (state.workSocket == null)
{ // Remove invalid state object
Monitor.Enter(connectedSocks);
if (connectedSocks.Contains(state))
{
connectedSocks.Remove(state);
Interlocked.Decrement(ref sockCount);
}
Monitor.Exit(connectedSocks);
}
else
{
if (DateTime.Now.AddTicks(-
state.TimeStamp.Ticks).Minute > timeoutMinutes)
{
RemoveSocket(state);
}
}
}
}
catch (Exception)
{
}
finally
{
lostTimer.Change(Server.timerTimeout,
Server.timerTimeout);
}
}

Auratius
http://www.auratius.co.za
 
Hello.

I'm writing an application that manages sockets and has a message protocol.
I connect to another machine and I have to send and receive certain
messages.

I need the possibility to restart the communication. I tried with a simple
socket.close method and trying to connect again as the first time, but the
connection is refused.

Is it possible to do that?

--

Regards,

Diego F.

You can try with this:

if (MySocket != null)
{
if (MySocket.Connected)
{
MySocket.Close();
MySocket.Disconnect(); // this does not work with Pocket PC
MySocket = null;
}
}
 
I tried the close method, assign to null and connecting again, but I get an
exception: No connection could be made because the target
machine actively refused it
 
I tried the close method, assign to null and connecting again, but I get an
exception: No connection could be made because the target
machine actively refused it

--

Regards,

Diego F.

In this case the machine that you connect to it must close your
connecgtion. I think the machine let the connection alive. If machine
is yours, yous must close the connection after a timeout.
I have written an application last week on pocket pc and I have used
the code that I passed to you. It work perfectly.
Can you explain me what your application do?

P.S.
(Are you italian?)
 
My application connects to a server machine. After sending and receiving
some protocol messages, it starts receiving messages that are inserted in a
database.

The original VB6 application, is capable to detect that the server is
disconnected and tries to connect again. Also, there is a button to
reconnect.

When I try to do the same in VB.NET, as I don't have the Winsock events, I
don't know if the connection is down (I should send something to get an
error and suppose that the connection is down), and worst, I get an error
when trying to reconnect again with the server. I don't know if the problem
is that the server still is VB6 and is any imcompatibility.


PS, I'm from Spain
 
My application connects to a server machine. After sending and receiving
some protocol messages, it starts receiving messages that are inserted in a
database.

The original VB6 application, is capable to detect that the server is
disconnected and tries to connect again. Also, there is a button to
reconnect.

When I try to do the same in VB.NET, as I don't have the Winsock events, I
don't know if the connection is down (I should send something to get an
error and suppose that the connection is down), and worst, I get an error
when trying to reconnect again with the server. I don't know if the problem
is that the server still is VB6 and is any imcompatibility.

PS, I'm from Spain

--

Regards,

Diego F.

You can try also to use Dispose() method to releases the unmanaged
resources used by the Socket, and optionally disposes of the managed
resources.

If It don't work then the problem is in the server machine. Because
when you make Mysocket = null, than you have destroyed the socket and
when you create it in a second time you can use it. I think the
problem is on the server side, than it is not capable to detect if the
client is connected or not.
I think you can use a ping command in the server side.

Let me know. Ok
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top