Link to home
Start Free TrialLog in
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

asked on

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
TCP1.Close
TCP1.Listen

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.
Avatar of cyberdevil67
cyberdevil67

Hi egl1044,

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

     Listener = new TcpListener(IPAddress.Parse(m_IPAddress),m_port);
     Listener.Start();
            while(true)
            {
                    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.
           
Cheers!
Avatar of nffvrxqgrcfqvvc

ASKER

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 procedures...like 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++
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...
I am using the winsock control, I'm sorry but the above code doesn't work, or am I missing something?
ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...

Avatar of Mike Tomlinson
cyberdevil67,

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.

~IM
Whoops my bad... No wonder that code I posted didn't work then:-)
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
TCP1.Close
TCP1.Listen


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.

regds