Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Keep winsock connection alive! URGENT

Posted on 2005-04-12
11
Medium Priority
?
704 Views
Last Modified: 2013-11-13
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.
0
Comment
Question by:nffvrxqgrcfqvvc
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13768456
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!
0
 
LVL 29

Author Comment

by:nffvrxqgrcfqvvc
ID: 13768504
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++
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13768572
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...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Author Comment

by:nffvrxqgrcfqvvc
ID: 13768637
I am using the winsock control, I'm sorry but the above code doesn't work, or am I missing something?
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 0 total points
ID: 13768663
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
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13768725
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...

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13769101
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
0
 
LVL 9

Expert Comment

by:cyberdevil67
ID: 13769121
Whoops my bad... No wonder that code I posted didn't work then:-)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13769331
0
 
LVL 1

Expert Comment

by:virendra_malushte
ID: 13769731
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question