Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present?
Winsock:  detect is server is present? Winsock:  detect is server is present?
Winsock:  detect is server is present?
Go Back  Xtreme Visual Basic Talk > > > Winsock: detect is server is present?


Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2003, 03:00 PM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default Winsock: detect if server is present?


Challenge: stump the Winsock guru's


I'm looking for a way to "automagically" detect if there is a winsock server on the local network that is listening.

Anyone know how to do this? I'm thinking because it's listening, by definition, it's not sending anything out. Which means, it would have to send something (?) out saying, 'hey everyone, I'm listening!', right?

Maybe there's a better way. Any ideas?

And to the winner, my extreme gratitude!!!!

tk

Last edited by VBerLite; 06-20-2003 at 05:58 PM.
Reply With Quote
  #2  
Old 06-21-2003, 03:30 AM
Squidge Squidge is offline
Restricted
 
Join Date: Mar 2003
Location: Manchester! England!
Posts: 872
Default

so let me get this right? you want to search for a server you dont evan know is there? over a network? or other the internet? internet would be extremly difficult becuase you would have to guess an IP i am not too sure with the network (never used one) but i suppose you could just loop through alot of ports trying to connect?

why would you want to do this?
Reply With Quote
  #3  
Old 06-21-2003, 06:46 AM
JimCamel's Avatar
JimCamelWinsock:  detect is server is present? JimCamel is offline
Mostly Absent

* Expert *
 
Join Date: Jun 2002
Location: Christchurch, New Zealand
Posts: 2,006
Default

Squidge is correct. Unless you know where to start looking, it's pretty pointless. The practise of scanning people for servers is known as Port Scanning and is against the forum rules as it is often used for hackers to determine vunerabilities in systems.

If you're doing it over a LAN, often what happens is a message is sent out by the server to a specific IP and Port, which is known as broadcasting. This IP is special in that all machines on the LAN listen to it, and any clients listening will get the message (I believe that broadcasting requires a UDP connection, so clients aren't really listening, just bound to the port). I can't remember the IP off the top of my head though.
__________________
Sometimes it happens feelings die, Whole years are lost in the blink of an eye
We once had it all but event conspired, Sometimes
Now that it's over, it is through, It gets me everytime I think of you
Sometimes It happens, feelings die, Sometimes
Reply With Quote
  #4  
Old 06-21-2003, 09:39 AM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default

Here's the deal. I wrote a stand-alone chat program that can be either a server or a client. I will be using it only on a small LAN (single subnet, and I want to stay away from the Internet with this --too many problems and security concerns at this point).

Why am asking this question is so it will automagically determine on it's own, if it needs to be a server or the client. Right now I'm using a form as a custom msgbox to prompt if the user wants to run it as a server or a client. They may not always know, and I'd like it to be as seemless to them as possible --especially since they may not always know what "server" and "client" means in this sense.

Thanks,
tk

Last edited by VBerLite; 06-21-2003 at 09:55 AM.
Reply With Quote
  #5  
Old 06-21-2003, 12:15 PM
Tsu Tsu is offline
Newcomer
 
Join Date: Mar 2003
Location: Approx. 1 AU from Sol
Posts: 4
Default

I have only experience with smaller networks, so I'm not sure, but doesn't every computer connected to a network need an IP address that starts with the first three values (eg. 10.0.0.x) ?
If so, couldn't you use a For...Next loop to try connect to 10.0.0.1 through 10.0.0.255 and where it doesn't give an error, there's a server?
Or am I thinking too simple?
Reply With Quote
  #6  
Old 06-21-2003, 07:01 PM
dethbom dethbom is offline
Newcomer
 
Join Date: Jun 2003
Posts: 3
Default

this should be a very simple answer if im not missing anything
Winsock1.LocalPort = YourUsualLocalListeningPortHere
Winsock1.Listen
If Err.Number = sckAddressInUse Then
Msgbox "There Is A Server Running!",vbCritical
End If
Reply With Quote
  #7  
Old 06-21-2003, 07:21 PM
Banjo's Avatar
BanjoWinsock:  detect is server is present? Banjo is offline
Hell's Angel

Retired Moderator
* Guru *
 
Join Date: Jul 2001
Location: Yorkshire, UK
Posts: 10,394
Default

You're missing something

Jim hit the nail on the head. The only answer is for the server to periodically broadcast a UDP packet. There is an article on doing this at winsockvb.com.

for a dedicated system such as this would it not be simpler to have a small dedicated server program that runs on the office server maybe. That way the clients can all just point to that one fixed location.
__________________
A wise one man once said "what you talking about dog breath"
Reply With Quote
  #8  
Old 06-22-2003, 11:26 PM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default

Quote:
Originally Posted by Banjo
Jim hit the nail on the head. The only answer is for the server to periodically broadcast a UDP packet. There is an article on doing this at winsockvb.com.



Hmmm, very interesting. This brings up some more questions.
So, if my chat is TCP based, can I momentarily change the protocol to UDP, and then back to TCP? If so, what effect would that have on any open sessions? (Wouldn't it kill them?)

What about having two winsock controls on the form, one that's TCP and the UDP (for the broadcast)? Can you do that?
Or or or, would it be better/possible to have another form, non-visible with the UDP control doing it's broadcast thing?

Thanks,
tk
Reply With Quote
  #9  
Old 06-23-2003, 04:12 AM
JimCamel's Avatar
JimCamelWinsock:  detect is server is present? JimCamel is offline
Mostly Absent

* Expert *
 
Join Date: Jun 2002
Location: Christchurch, New Zealand
Posts: 2,006
Default

You can have more than one winsock per form, one doing UDP broadcasting and one establishing TCP connections. I wouldn't recommend using 1 control to do both though, because, as you mentioned you'll have to close your TCP connections for broadcasting.

You could always have another non-visible form for a UDP control, but that's most likely overkill. As long as both your winsock controls have different names you should have no problem putting them on the same form.
__________________
Sometimes it happens feelings die, Whole years are lost in the blink of an eye
We once had it all but event conspired, Sometimes
Now that it's over, it is through, It gets me everytime I think of you
Sometimes It happens, feelings die, Sometimes
Reply With Quote
  #10  
Old 06-23-2003, 11:57 AM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default

Quote:
Originally Posted by JimCamel
You can have more than one winsock per form, one doing UDP broadcasting and one establishing TCP connections.
...As long as both your winsock controls have different names you should have no problem putting them on the same form.



And different ports, too, right?

Thanks,
tk
Reply With Quote
  #11  
Old 06-23-2003, 08:36 PM
JimCamel's Avatar
JimCamelWinsock:  detect is server is present? JimCamel is offline
Mostly Absent

* Expert *
 
Join Date: Jun 2002
Location: Christchurch, New Zealand
Posts: 2,006
Default

Uh yeah, shoulda mentioned that too
__________________
Sometimes it happens feelings die, Whole years are lost in the blink of an eye
We once had it all but event conspired, Sometimes
Now that it's over, it is through, It gets me everytime I think of you
Sometimes It happens, feelings die, Sometimes
Reply With Quote
  #12  
Old 06-24-2003, 09:29 AM
blindwig's Avatar
blindwigWinsock:  detect is server is present? blindwig is offline
Ultimate Contributor

* Expert *
 
Join Date: Jun 2003
Location: New York, NY
Posts: 1,929
Default

Quote:
Originally Posted by VBerLite
Quote:
Originally Posted by JimCamel
You can have more than one winsock per form, one doing UDP broadcasting and one establishing TCP connections.
...As long as both your winsock controls have different names you should have no problem putting them on the same form.



And different ports, too, right?

Thanks,
tk


I could be wrong, but I believe that UDP and TCP have different sets of ports. So for example if you have a server listening on port TCP 23 and a client that is trying to connect on UDP 23, they'll never see each other. I think I remember that from my NET+ class, but then again I could be wrong.
Reply With Quote
  #13  
Old 06-24-2003, 03:13 PM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default

Quote:
Originally Posted by blindwig

I could be wrong, but I believe that UDP and TCP have different sets of ports. So for example if you have a server listening on port TCP 23 and a client that is trying to connect on UDP 23, they'll never see each other. I think I remember that from my NET+ class, but then again I could be wrong.



Now that's an interesting thought. I'm sure that could come in handy sometime.

But at this point, since I don't yet believe I'm short on ports, I think I'll use two different ports --just to play it safe. Thanks for the info, though!

Thanks,
tk
Reply With Quote
  #14  
Old 06-24-2003, 08:55 PM
JimCamel's Avatar
JimCamelWinsock:  detect is server is present? JimCamel is offline
Mostly Absent

* Expert *
 
Join Date: Jun 2002
Location: Christchurch, New Zealand
Posts: 2,006
Default

TCP and UDP both use the IP method of addressing. In theory they should use the same set of ports. Even if they don't, there's still what, something like 63,000 ports available on at a given IP, so I'm sure it wouldn't hurt to use different port numbers for UDP and TCP.
__________________
Sometimes it happens feelings die, Whole years are lost in the blink of an eye
We once had it all but event conspired, Sometimes
Now that it's over, it is through, It gets me everytime I think of you
Sometimes It happens, feelings die, Sometimes
Reply With Quote
  #15  
Old 06-25-2003, 12:10 AM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Unhappy

Quote:
Originally Posted by Banjo
The only answer is for the server to periodically broadcast a UDP packet. There is an article on doing this at winsockvb.com.




Well, I tried what was in that article, and I think it might be working, but I don't know how to tell. Here's what I did in two separate new projects:

First for the Broadcaster, I added a timer (timer1, interval=2000) and a Winsock control (Winsock1, protocol set to UDP) to the form, and put in the following code:

Code:
Public Sub Broadcast(ByVal Text As String) ' setup the socket Winsock1.RemoteHost = "192.168.0.255" Winsock1.RemotePort = 2010 ' send!! Winsock1.SendData Text DoEvents End Sub Private Sub Winsock_Connect() MsgBox "Connected" End Sub Private Sub Timer1_Timer() Broadcast "abc" End Sub

-----------

Then for the listener (second project), I only put a Winsock control (Winsock1, protocol set to UDP) on the form, with the following code:

Code:
Private Sub Form_Load() Winsock1.LocalPort = 2010 ' Winsock1.Listen 'commented out otherwise I get an error End Sub Private Sub Winsock1_ConnectionRequest(ByVal RequestID As Long) Winsock1.Close Winsock1.Accept RequestID End Sub

-----------

Now, they both run ok --in that I don't get any errors--, but no connection ever happens. Oh, and to anticipate your question, the broadcast address I used was because I don't want to broadcast to the world, just my local subnet --but I'm not sure how exactly to do that.

So.... what am I doing wrong?

Thanks!
tk
Reply With Quote
  #16  
Old 06-25-2003, 08:02 AM
CodeSteve CodeSteve is offline
Regular
 
Join Date: May 2003
Posts: 50
Default

UDP doesnt have any 'connections' as such,

just have something like
Code:
'server'
Public Sub Broadcast(ByVal Text As String)

  ' setup the socket
  WinsockUDP.RemoteHost = "192.168.0.255"
  WinsockUDP.RemotePort = 2010
  ' send!!
  WinsockUDP.SendData Text
  DoEvents
End Sub

'Client Side

WinsockUDP_DataArrival(bytes as long)
dim s as string
WinsockUDP.getData s
' im assuming the broadcast message was the IP!'
if WinsockTCP.State <> sckConnected then
    WinsockTCP.RemoteHost = s
    WinsockTCP.Connect
end if
end sub
And dont worry about 'broadcasting to the world'... if your modem / network card doesnt block out such requests you can rest assured your ISP will...
Reply With Quote
  #17  
Old 06-25-2003, 09:41 AM
Valeron Valeron is offline
Freshman
 
Join Date: Apr 2003
Posts: 26
Question

Hey, what a coincidence ! I'm trying to do exactly the same thing !
i'm doing this at home with a single PC so i replaced the IP with 127.0.0.1

'Server broadcasting his IP
Code:
Private Sub Broadcast() wsUDP.RemoteHost = "127.0.0.1" wsUDP.RemotePort = 21018 wsUDP.SendData wsPresListen.LocalIP DoEvents End Sub Private Sub tmrBroadCast_Timer() Call Broadcast End Sub

'Client

Code:
Private Sub wsUDP_DataArrival(ByVal bytesTotal As Long) wsUDP.GetData strIP If cmdConnect.Enabled = False Then cmdConnect.Enabled = True End If Label5.Caption = strIP End Sub

However, label5 didn't change, and the command button aint enabled, whats the matter ? Did we do something wrong ?

I'm having the same problem as VBerLite
Quote:
Now, they both run ok --in that I don't get any errors--, but no connection ever happens.
Reply With Quote
  #18  
Old 06-25-2003, 10:48 AM
VBerLite VBerLite is offline
Freshman
 
Join Date: May 2003
Posts: 34
Default

Quote:
Originally Posted by CodeSteve
Code:
'Client Side WinsockUDP_DataArrival([B]bytes as long[/B]) dim s as string WinsockUDP.getData s ' im assuming the broadcast message was the IP!' if Winsock[B]TCP[/B].State <> sckConnected then Winsock[B]TCP[/B].RemoteHost = s Winsock[B]TCP[/B].Connect end if end sub

And dont worry about 'broadcasting to the world'... if your modem / network card doesnt block out such requests you can rest assured your ISP will...




Well, on the server side I didn't change anything since it looked the same as what you had. On the client side, however, there were some differences. I took what you had with some changes (see bolded):

Code:
Private Sub WinsockUDP_Connect() MsgBox "connected" End Sub [B]Private Sub [/B] WinsockUDP_DataArrival([B]ByVal bytesTotal As Long[/B]) Dim s As String WinsockUDP.GetData s ' im assuming the broadcast message was the IP!' If Winsock[B]UDP[/B].State <> sckConnected Then Winsock[B]UDP[/B].RemoteHost = s Winsock[B]UDP[/B].Connect End If End Sub


But to no avail. It still didn't connect. Should I have not made the changes? (btw, on the ByVal one, I had to because with what you had suggested I got an error, and I assumed the others needed to be changed since we're talking UDP, not TCP...)

Oh, and regarding the broadcasting, I'm just trying to make sure I stay away from breaking the TOS and UA --to be safe-- but I appreciate what you're saying.

Thanks,
tk
Reply With Quote
  #19  
Old 06-25-2003, 11:03 AM
Valeron Valeron is offline
Freshman
 
Join Date: Apr 2003
Posts: 26
Default

Hi, VBerLite,

Code:
WinsockUDP_DataArrival(bytes As long) Dim s As String WinsockUDP.getData s ' im assuming the broadcast message was the IP!' If WinsockTCP.State <> sckConnected Then WinsockTCP.RemoteHost = s WinsockTCP.Connect End If End Sub

The reason he's using TCP is because, the UDP socket only accepts and stores the IP broadcasted by the Server, after receiving the IP, he uses the TCP socket to establish a TCP connection to the server, you'll need a TCP winsock on the server listening to the TCP request from the client

But, as i've said... the UDP DataArrival event aren't triggered at all, VBerLite, try putting a MsgBox "Received UDP" in your
Code:
Private Sub WinsockUDP_DataArrival(ByVal bytesTotal As Long)
This will tell you whether your data arrival even happened or not
Reply With Quote
  #20  
Old 06-25-2003, 11:04 AM
Valeron Valeron is offline
Freshman
 
Join Date: Apr 2003
Posts: 26
Default

Hey, i see codesteve is here ! HI !!
Hope he can help us out a bit...
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
MMORPG Server: Winsock or DP LordHelmut Communications 15 11-12-2003 02:44 AM
Socket Server.... is a winsock control good enough?! xmcnuggetx Communications 4 06-21-2003 07:08 PM
programming for IRC with VB tutorial crazycheetah Tutors' Corner 0 05-04-2003 05:15 PM
WinSock vbsupernewbie Communications 1 09-12-2001 10:20 PM

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
Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present? Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present? Winsock:  detect is server is present?
Winsock:  detect is server is present?
Winsock:  detect is server is present?
 
Winsock:  detect is server is present?
Winsock:  detect is server is present?
 
-->