Keep winsock connection alive! URGENT

Hi, I am using TCP protocal for a simple client/server applications the problem I am running into is that if I disconnect I must re-open my server application in order re-connect again.  I want to know how I can always make sure my server application is listening and that my client can always connect

I added this code to the ERROR procedure in the winsock control on my server.
On error resume next

It doesn't seem to work because I can't re-establish a connection from the client unless i re-load the server and client.
So basically I can only connect once.  I want to make sure I can disconnect and re-connect without re-opening the server application.
LVL 29
Who is Participating?
nffvrxqgrcfqvvcAuthor Commented:
okay..I think I fixed the problem. Thanks

'This seemed to fix the problem.

If Index = 0 Then
      gSockInstance = gSockInstance + 1
      Load TCP1(gSockInstance)
      TCP1(gSockInstance).LocalPort = 0
      TCP1(gSockInstance).Accept requestID
      Load TCP1(gSockInstance)
   End If
Hi egl1044,

    Your best bet is to do something along the lines of this,

     Listener = new TcpListener(IPAddress.Parse(m_IPAddress),m_port);
                    Socket clientSocket = Pop3Listener.AcceptSocket();
                    //Proccess stuff

            I actually run another thread off the above, and let the thread handle the incomming packets for the port. What you should be doing here is a command accept procedure, like how ftp works.

            The above method means the code placed into the serve code will sit the listening until something comes in, then it can go off and chek to see what it needs to do then falls back into a loop again,

            I wrote my onw mail server using this method so if you have nay more questions I'll try to help.
nffvrxqgrcfqvvcAuthor Commented:
Yes, I haven't got the slightest idea on how to do what you mentioned, I know about a year ago I did it somehow using just the winsock ERROR,CONNECT,ConnectionRequest, but i forgot how I had done it. I am willing to try your method but don't know where to begin as that code looks to be C++
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

No its actually c#

So you can write

Dim Listener as TcpListener = new TcpListener( IPAddress, Port)

while (true)
  Dim clientSocket as Socket = Listener.AcceptSocket()
end while

The Listner.AcceptSocket just sits there until something comes through that port...
nffvrxqgrcfqvvcAuthor Commented:
I am using the winsock control, I'm sorry but the above code doesn't work, or am I missing something?
I would recommend using the method I described.

Dim Listener As System.Net.Sockets.TcpListener = New System.Net.Sockets.TcpListener(IPAddress, Port)

While (True)
  Dim clientSocket As System.Net.Sockets.Socket = Listener.AcceptSocket()
End While
End Sub

The reason I say this is because you can extend this method to be able to have a handshaking between the client and the serve. You could have the client send a command such as

list - And it will retrieve the list, or even send back a command saying unknown command. When learning I stayed clear of the old VB6 methods and used the new socket programming, when you get into this you might see the benefits a little more clearer.

But hey I did help with...

Mike TomlinsonMiddle School Assistant TeacherCommented:

This "Visual Basic" Topic Area focuses on VB6 and below...which use the winsock control.  Many of the authors asking questions here do not use Visual Basic.Net.

Whoops my bad... No wonder that code I posted didn't work then:-)
Mike TomlinsonMiddle School Assistant TeacherCommented:
Once the connection between a client and server is broken youu have to do two things..

1. reopen the server listening port .
2. cilent should reconnect to the server.

the code mentioned by you will only start the server socket..
On error resume next

you should also code the client to reconnect once the disconnect event in the client is fired.
THis would be normal connect routine you use during initial connection.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.