NetWork Connection not working sometimes?
Hi, I have an application that uses several TCP and UDP connections (all as an array)
I have one Main-application and several clients that connect to the Main (via a TCP connection, all with different SocketNr). For the development I use the loopback IP-address, so I have all applications on one computer (normally they are all seperated)
All connections run normal as long as I run the application only once.
But when closing the application and then restarting it happens (sometimes????) that a specific client won't connect. I can only get it to work again when restarting the computer, so I believe it'S a problem with sockets which are not cleared. Is their a way to find and clear blocked sockets?
When closing the Main-application, all the clients are closed too (the Main application is closed by the MainForm_Unload which closes all connections before unloading himself, and the clients will run their MainForm_Unload in the winsock_close event).
Re: NetWork Connection not working sometimes?
Since you can only have one connection on one socket, the most likely problem is that you're not closing a socket sometimes. Check the clients as well as the server.
Re: NetWork Connection not working sometimes?
OK, did some more reading thru related threads.
What I'm doing is this:
The Server has a WinsockTCP(1-5) array with each WinsockTCP(i) having a different (and therefore identifying) Portnumber. The Server is listening on all WinsockTCP(i) and on a ConnectionRequest accepting the connection on that connection [I think the problem is here!]. the connection remains open as long as the application is running, if the server is closed, the connections are closed and the clients close themself.
From the reading it sounds like that I don't need a different Portnumber for each array item and that I should use only one array item to listen for connection requests. Is that correct?
I'm using the this WinsockTCP array in a way where on each GetData Event the Index is telling Who did send something. So if I only use one listening connection (for example Indexnumber 0), how would I accept the connection from a client (for example the that should use Indexnumber 2?)Would all array-items need to use the same portnumber? How could the server identify if a message would be coming from connectionIndexnumber 2 or 3?
Re: NetWork Connection not working sometimes?
This is from a piece of code I downloaded from somewhere. It has a single Winsock control on the form with Index set to 0
Code:
Private Sub wsServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim Socket As Integer
Socket = wsServer().UBound + 1
Load wsServer(Socket)
wsServer(Socket).Accept requestID
End Sub
Quote:
Originally Posted by opus
Would all array-items need to use the same portnumber?
Yes - and that makes it easier for the client, since all copies use the same code.
Quote:
How could the server identify if a message would be coming from connectionIndexnumber 2 or 3?
Code:
Private Sub wsServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Index tells you which control the data is coming in on.
Re: NetWork Connection not working sometimes?
Thanks for the reply Al42
I got it that far.
Although I'm using different portnumbers for each array-item winsock, i can use the same code for them (they are an array even with different portnumbes).
However my identification problem starts a bit earlier, each client has a specific type, so the server must be able to ident a client already in the connectiorequest event. Since all clients should use the same port, for the server the connetionrequests will all look the same. Is there a way to ID the connectiorequest, or do I have to generate an "identification-message" just after the connection?
Re: NetWork Connection not working sometimes?
Have the client send a "I'm Type A" or "I'm Type B" (or however you want to do it) connection string. Then the server will know, at the connection request, what type the client is.
Re: NetWork Connection not working sometimes?
Hi, Al42, seems you're also "working"on the weekend.
Thanks for all the help on this topic, as a matter of fact I already use such a "I'm type A" message, and this part works.
But I'm still stuck with the problem that the connection (and it is the TCP one) will not work if I run the software more than once on a machine without a reboot in between. and that happens on the "testing ground", that is one computer where all applications are running using the loopback IP .
So far I haven't been able to find the exact reproducing way, but I'll keep on trying.
The routines for closing the connections and applications are as follows
For both the Server and Clients (the Server NetzwerkFunction = Control).
Client will call mnu_quit, the Server will keep on listening!
Code:
Private Sub WinSockTCP_Close(Index As Integer)
Dim i As Integer
On Error GoTo errorhandler
WinSockTCP(Index).Close
WinSockTCP(Index).Listen
If Not NetzwerkFunction = Control Then
Timer1 = False
mnuQuit_Click
End If
Exit Sub
errorhandler:
ErrLog_schreiben "WinSockTCP_Close", Index
Resume Next
End Sub
The Form_Unload is called via Mnu_quit for Server and Clients.
Code:
Private Sub mnuQuit_Click()
On Error GoTo errorhandler
If mnuStartStop.Caption = "S&top" Then Timer1 = False
Unload MainForm
End
Exit Sub
errorhandler:
ErrLog_schreiben "mnuQuit_Click"
Resume Next
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim fs As Object
Dim i As Integer
On Error GoTo errorhandler
For i = 0 To 4
WinSockTCP(i).Close
Next i
For i = 0 To 2
WinsockUDP(i).Close
Next i
Unload frmCPARechner
Unload frmOptions
Unload frmReplay
Exit Sub
errorhandler:
ErrLog_schreiben "Form_Unload", Datei
Resume Next
End Sub
Is there a problem in those closing routines?
Re: NetWork Connection not working sometimes?
maybe this can help u understand more about multiple connections as it did also help me before...
http://www.vbforums.com/showthread.php?t=297308
Re: NetWork Connection not working sometimes?
FOUND the problem!
Instead of having just:
Code:
WinsockTCP(0).Connect ServerIp, ServerPort
the following solved the problem.
Code:
Connected=False
WinsockTCP(0).Connect ServerIp, ServerPort
Do
DoEvents
Loop Until Connected=True
With this line added in the winsockTCP_Connect Event
It looks like this DoEvents solved the starting problem (Clients not connecting sometimes). So the change in the usage of ports wasn't really neccesary, but I did learn a bit more about Winsock.