Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / May 2007

Tip: Looking for answers? Try searching our database.

How to reset a socket?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Diego F. - 29 May 2007 09:12 GMT
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.
auratius@gmail.com - 29 May 2007 09:55 GMT
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
Sheikko - 29 May 2007 09:59 GMT
> Hello.
>
[quoted text clipped - 13 lines]
>
> 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;
  }
}
Diego F. - 29 May 2007 10:09 GMT
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.

>> Hello.
>>
[quoted text clipped - 28 lines]
>   }
> }
Sheikko - 29 May 2007 11:41 GMT
> I tried the close method, assign to null and connecting again, but I get an
> exception: No connection could be made because the target
[quoted text clipped - 38 lines]
> >   }
> > }

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?)
Diego F. - 29 May 2007 12:25 GMT
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.

>> I tried the close method, assign to null and connecting again, but I get
>> an
[quoted text clipped - 49 lines]
> P.S.
> (Are you italian?)
Sheikko - 29 May 2007 14:25 GMT
> My application connects to a server machine. After sending and receiving
> some protocol messages, it starts receiving messages that are inserted in a
[quoted text clipped - 75 lines]
> > P.S.
> > (Are you italian?)

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.