Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock)
Multiple Connection (Winsock) Multiple Connection (Winsock)
Multiple Connection (Winsock)
Go Back  Xtreme Visual Basic Talk > > > Multiple Connection (Winsock)


Reply
 
Thread Tools Display Modes
  #1  
Old 02-02-2002, 12:21 PM
Fredd
Guest
 
Posts: n/a
Question Multiple Connection (Winsock)


Hello everyone there,

I got couple of questions to ask regarding winsock connection.

1. I have come across the use of control array for multiple client connection to the server, so need you guys to enlighten me in this area.

Can I know how does actually the following codes works in allowing for multiple connection from Client?
What I understand - if there is already an existing connection made by one client, the code will create another new array of sockets, but why is it necessary to put if index is 0?
And for the last line in the procedure, I notice that each time I Close a connection from the client and connect again from the same client, the Client Port increase by 1, for example: before close 1065 but after close and connect, I got 1066, now why does the port number increase by 1 and what it means actually?

Notes: Some of this codes i picked up from internet.


Option explicit
Dim iSockets as Integer

Private Sub WinsockServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
iSockets = iSockets + 1
Load WinsockServer(iSockets)
WinsockServer(iSockets).LocalPort = 1001
WinsockServer(iSockets).Accept requestID
End If
End Sub

What about the following codes for to take care of the closing?


Private Sub WinsockServer_Close(Index As Integer)
WinsockServer(Index).Close
Unload WinsockServer(Index)
iSockets = iSockets - 1
End Sub


2.When I only have one Client and one host, how can I close the connection and reconnect again when needed. I have the following code with me but got one problem, after closing the connection it just refuse to reconnect again? Do I really need to use control array to do this? Is there any other way?

Following is server program:


Private Sub Form_load()
WinsockServer.LocalPort = 1001
WinsockServer.Listen
End Sub


Private Sub Form_Unload(Cancel As Integer)
WinsockServer.Close
End Sub

Private Sub WinsockServer_ConnectionRequest(ByVal requestID As Long)
If WinsockServer.State <> sckClosed Then
WinsockServer.Close
End If
WinsockServer.Accept requestID
End Sub


And the following are Client program:


Private Sub Form_load()
tcpClient.RemoteHost = "127.0.0.1"
tcpClient.RemotePort = 1001
End Sub

Private Sub cmdConnect_Click()
Dim strnow As Long
If tcpClient.State <> sckClosed Then
tcpClient.Close
Do Until tcpClient.State = sckClosed
DoEvents
Loop
End If
tcpClient.Connect

strnow = Timer 'read current time b4 entering the loop
Do While (tcpClient.State <> sckConnected)
DoEvents
If (Timer - strnow) > 3 Then 'prevent endless loop
Exit Sub
End If
Loop
End Sub


3. Can anyone tell me how can I use object.localport = 0? I understand that this allow for random assign port number instead of using a specific one however I failed to connect my client to the server by using the following code:

For server, I have
WinsockServer.LocalPort = 0
And for client, I have
TcpClient.LocalPort = 0


I guess that is all for now, sorry for the long question
Reply With Quote
  #2  
Old 02-02-2002, 03:46 PM
Squirm's Avatar
SquirmMultiple Connection (Winsock) Squirm is offline
Political Coder

Retired Moderator
* Guru *
 
Join Date: Mar 2001
Location: London, England
Posts: 8,037
Default

Answers to your questions:
  1. Yes, all is well and good except for one line:

    Code:
    WinsockServer(iSockets).LocalPort = 1001
    Two winsocks can not have the same LocalPort, you will get an Address In Use error, I suggest this:

    Code:
    WinsockServer(iSockets).LocalPort = 1000 + iSockets
  2. I have no idea, looks like it should work, you might want to increase the value of this, so it waits longer:

    Code:
    If (Timer - strnow) > 30 Then 'prevent endless loop
  3. I suspect the problem is that the server is given a random port number, so the client doesnt know which port to connect to. You should try a fixed or semi-fixed (with a range of values) LocalPort that the clients connect to, and then any new winsocks which actually accept the connection can be made random.

And a side note, there are tags [code] and [/code] for putting code into posts
__________________
Search the forums | Use [vb][/vb] tags | Still IRCing
Reply With Quote
  #3  
Old 02-03-2002, 09:14 AM
ChiefRedBull's Avatar
ChiefRedBullMultiple Connection (Winsock) ChiefRedBull is offline
ISearchGoogle

Retired Moderator
* Expert *
 
Join Date: May 2001
Location: england
Posts: 6,321
Default

2. - I dont see any disconnect code, but basically, when the server connection is closed, you need to reset the socket.
Add a timer, and check the state of the socket every minute or so.
Code:
Sub Timer1_Timer()
If WinsockServer.State <> sckConnected Then
  WinsockServer.Close
  WinsockServer.Listen
End If
End Sub
3. Random port assigning is normally done with a control array. You will have one control array, and one normal sock. The normal one wil listen on a certain port, and in its connection_request event, it will load a new sock in the control array and accept the request on that. Windows will automatically assign the new sock a randomly found localport to receive the connection on.
__________________
Chuck Norris ordered a Big Mac at Burger King, and got one.
Reply With Quote
  #4  
Old 05-17-2009, 07:15 PM
badiding badiding is offline
Newcomer
 
Join Date: Jul 2008
Posts: 7
Default

Ahh. I really want to create a Login Program Like the ones in YM and other MMORPG that will sign out the Account of the user if he/she Login on another Computer.. Please Help.. Thnxxx
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock) Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock) Multiple Connection (Winsock)
Multiple Connection (Winsock)
Multiple Connection (Winsock)
 
Multiple Connection (Winsock)
Multiple Connection (Winsock)
 
-->