Networks

Just a shot in the dark. I was wondering how programs such and winchat and the like were written… For a simple example: How would I write a program to locate another computer on the network and send information to it? I am guessing this to be very hard but if any one can give me a short example it would be great!

Thanks
testing38Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

3rsrichardCommented:
Here is an example that can be used on either one or two computers.  You have to know the IP addresses.

========================================

Creating a UDP application is even simpler than creating a TCP application because the UDP protocol doesn't require an explicit connection. In the TCP application above, one Winsock control must explicitly be set to "listen," while the other must initiate a connection with the Connect method.

In contrast, the UDP protocol doesn't require an explicit connection. To send data between two controls, three steps must be completed (on both sides of the connection):

Set the RemoteHost property to the name of the other computer.


Set the RemotePort property to the LocalPort property of the second control.


Invoke the Bind method specifying the LocalPort to be used. (This method is discussed in greater detail below.)
Because both computers can be considered "equal" in the relationship, it could be called a peer-to-peer application. To demonstrate this, the code below creates a "chat" application that allows two people to "talk" in real time to each other:

To create a UDP Peer

Create a new Standard EXE project.


Change the name of the default form to frmPeerA.


Change the caption of the form to "Peer A."


Draw a Winsock control on the form and name it udpPeerA.


On the Properties page, click Protocol and change the protocol to UDPProtocol.


Add two TextBox controls to the form. Name the first txtSend, and the second txtOutput.


Add the code below to the form.
Private Sub Form_Load()
    ' The control's name is udpPeerA
    With udpPeerA
        ' IMPORTANT: be sure to change the RemoteHost
        ' value to the name of your computer.
        .RemoteHost= "PeerB"
        .RemotePort = 1001   ' Port to connect to.
        .Bind 1002                ' Bind to the local port.
    End With
    frmPeerB.Show                 ' Show the second form.
End Sub

Private Sub txtSend_Change()
    ' Send text as soon as it's typed.
    udpPeerA.SendData txtSend.Text
End Sub

Private Sub udpPeerA_DataArrival _
(ByVal bytesTotal As Long)
    Dim strData As String
    udpPeerA.GetData strData
    txtOutput.Text = strData
End Sub

To create a second UDP Peer

Add a standard form to the project.


Change the name of the form to frmPeerB.


Change the caption of the form to "Peer B."


Draw a Winsock control on the form and name it udpPeerB.


On the Properties page, click Protocol and change the protocol to UDPProtocol.


Add two TextBox controls to the form. Name the TextBox txtSend, and the second txtOutput.


Add the code below to the form.
Private Sub Form_Load()
    ' The control's name is udpPeerB.
    With udpPeerB
        ' IMPORTANT: be sure to change the RemoteHost
        ' value to the name of your computer.
        .RemoteHost= "PeerA"
        .RemotePort = 1002    ' Port to connect to.
        .Bind 1001                ' Bind to the local port.
    End With
End Sub

Private Sub txtSend_Change()
    ' Send text as soon as it's typed.
    udpPeerB.SendData txtSend.Text
End Sub

Private Sub udpPeerB_DataArrival _
(ByVal bytesTotal As Long)
    Dim strData As String
    udpPeerB.GetData strData
    txtOutput.Text = strData
End Sub

To try the example, press F5 to run the project, and type into the txtSend TextBox on either form. The text you type will appear in the txtOutput TextBox on the other form.

0
VbmasterCommented:
www.planet-source-code.com has a lot of source code samples, and a large amount covers internet data tranfer issues.
0
testing38Author Commented:
When I try to run the project the line  .Bind 1001                ' Bind to the local port.  cause the computer to give the run-time error '10042' Option unknown, or unsupported. If it may be of any help... I am using a Novell Network. Thanks for your help so far!!!!! The Example (so far) was great and easy to understand.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

3rsrichardCommented:
Do you mean that one call works and the other doesn't?

Maybe 1001 is in use, try 1111 or some other number.

Which VB are you using?

..Bind is a standard method under VB6.
========================================
Syntax

object.Bind LocalPort, LocalIP

The Bind method syntax has these parts

Part Description
object An object expression that evaluates to an object in the Applies To list.

LocalPort The port used to make a connection.

LocalIP The local Internet address used to make a connection.


Remarks

You must invoke the Bind method before invoking the Listen method.

0
testing38Author Commented:
I am using VB5.0 at the moment. I don't think I will be getteing VB6.0 for a little while...

Both calls don't work but since one get called first I don't see the other. If I take out both binds then the code in the text box change meathod don't work. Thanks for all your help so far!
0
testing38Author Commented:
I sometimes play games over the network using IPX or something of the like. How does this work?
0
testing38Author Commented:
I tried the program on a machine with VB6.0 The starting of the program gave me no errors, however when I tried to type into a box I got a runtime error. If I hit debug and then continued the program (makeing no changes to the code) the program resumed running but would not reproduce the error. In fact the only way to reproduce the error was to restart the computer. As you may have guessed the text I did type did not get send to the other forms text box. Note: I am using one couputer so I use the same name in the RemoteHost property of each computer. If I ever do get this example working on one machine, how do I do it over the IPX network we have here?
0
testing38Author Commented:
One more tidbit: I am getting the name of the computer by looking in the indafacation tab under networks in the comtrol panal. This is where I'm supost to go right?
0
3rsrichardCommented:
Do not use the same names for the two programs.  Windows should have two independent programs running, and they should be able to dend messages to each other.
I have run this program several times, and it does work.  It is an example from MSDN.
I know I have run it on one machine, and I think I have run it on two.
If you are using TCP/IP your machines will have IP addresses, thats how they will tell each other apart.

Here is what part of the code looks like on my machine
========================================

Private Sub Form_Load()
' The control's name is udpPeerA
With udpPeerA
' IMPORTANT: be sure to change the RemoteHost
' value to the name of your computer.
..RemoteHost = "131.198.273.211"
..RemotePort = 161   ' Port to connect to.
..Bind 1002                ' Bind to the local port.
End With
frmPeerB.Show                 ' Show the second form.
End Sub
0
3rsrichardCommented:
The program can probably use your computers name to look up it's IP address.  It's alright if the RemoteHost names are the same, but the form names should be different.
0
testing38Author Commented:
Sorry the computers in the network I use turn out not to have induvidual IPs! I guess this example wont work. So It's back to IPX. Have you ever heard of a program called 'Winpopup'? This program seems to work on the network I'm using.
0
3rsrichardCommented:
I have seen the word, but I don't know about the program.
If one of your machines acts as a server then you can probably broadcast messages from it.
0
testing38Author Commented:
Any ideas on how to set this up?
0
mcriderCommented:
Would the code to a winsock chat server help you???

Here you go:


Cheers!

------------------------------------------------------------

1) create a project

2) add the winsock control to your project

3) add a winsock control to your form an set it's INDEX property to 0

4) add a listbox to your form and set the SORTED Property to TRUE

5) add a command button to your form

6) Paste the following code into the DECLARATION section of the form and run it.  Anyone can then telnet into this server on port 1001 and chat. You could also write a client that would connect to this server on port 1001...


Private Sub Command1_Click()
    If List1.ListIndex <> -1 Then Winsock1_Close CInt(Mid$(List1.List(List1.ListIndex), 12, 4))
End Sub

Private Sub Form_Load()
    Command1.Caption = "Disconnect"
    Winsock1(0).LocalPort = 1001
    Winsock1(0).Listen
End Sub
Private Sub Winsock1_Close(Index As Integer)
    Dim iVal As Long
     
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected Then
            Winsock1(iVal).SendData Winsock1(Index).RemoteHostIP + " DISCONNECTED" + vbCrLf
            DoEvents
        End If
    Next iVal
    Winsock1(Index).Close
    For iVal = 0 To List1.ListCount - 1
        If InStr(1, List1.List(iVal), "Connection " + Format$(Index, "0000")) <> 0 Then
            List1.RemoveItem iVal
            Exit For
        End If
    Next iVal
    If Index = Winsock1.UBound Then
        Unload Winsock1(Index)
    End If
End Sub
Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    Dim iVal As Long
    Dim TotalConnect As Long
    Dim Acceptor As Long
     
    Acceptor = 0
    TotalConnect = 1
    For iVal = 1 To Winsock1.UBound
        Select Case Winsock1(iVal).State
            Case sckConnected
                TotalConnect = TotalConnect + 1
            Case sckClosed
                Acceptor = iVal
        End Select
    Next iVal
    If Acceptor = 0 Then Acceptor = Winsock1.Count
    If Acceptor > Winsock1.UBound Then Load Winsock1(Acceptor)
    With Winsock1(Acceptor)
        .Accept requestID
        List1.AddItem "Connection " + Format$(Acceptor, "0000") + " - " + .RemoteHostIP
        .SendData "Welcome to Telnet Chat Server" + vbCrLf + "Server hosted on " + _
            Winsock1(0).LocalIP + vbCrLf + "Total sessions connected: " + CStr(TotalConnect) + vbCrLf + vbCrLf
        DoEvents
    End With
    'ANNOUNCE THE NEW CONNECTION TO EVERYONE
    For iVal = 1 To Winsock1.UBound
        If Winsock1(iVal).State = sckConnected And iVal <> Acceptor Then
            Winsock1(iVal).SendData "From SERVER: " + Winsock1(Acceptor).RemoteHostIP + " has joined session." + vbCrLf
            DoEvents
        End If
    Next iVal
     
End Sub
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    Dim iVal As Long
     
    Winsock1(Index).GetData strData, vbString
    Winsock1(Index).Tag = Winsock1(Index).Tag + strData
    If InStr(1, Winsock1(Index).Tag, vbCr) Then
        Winsock1(Index).SendData vbCrLf
        DoEvents
        'SEND THE BUFFER TO EVERYONE
        For iVal = 1 To Winsock1.UBound
            If Winsock1(iVal).State = sckConnected And iVal <> Index Then
                Winsock1(iVal).SendData "From " + Winsock1(Index).RemoteHostIP + ": " + Winsock1(Index).Tag
                DoEvents
            End If
        Next iVal
        Winsock1(Index).Tag = "" 
    Else
        'ECHO THE TEXT BACK TO THE LOCAL TERMINAL
        Winsock1(Index).SendData strData
    End If
End Sub
0
testing38Author Commented:
This is a big one. I tried to copy and paste but I had problems. After I tried to resolve them I got an error at line, Winsock1(0).Listen
If you could please send big programs like this to testing38@hotmail.com   Thanks.

I think my errors may have been because of copying and pasteing.
0
mcriderCommented:
I sent the project to your mailbox...
0
testing38Author Commented:
Sorry the project did not make it. Come to think of it neither did the message form EE saying the Question was answered. Is the project larger then a meg? Try resending the server there may have been down for some reason...

0
mcriderCommented:
I sent it again to testing38@hotmail.com

The subject is...

   RE: Networks Question On EXPERTS EXCHANGE

It's a 3K attachment.  If you don't get it let me know.


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.