Solved

networking

Posted on 2000-03-03
12
204 Views
Last Modified: 2013-11-15
i want to write a prgram that will use the tcp/ip protocal to talk between two computers on my home network. i have done this but the program works by creating special files that the other computers on the network (running the same program)can read. This is not good as all the computers have to be able to read and write to a comman area. also not that fast. Is there a way that i can do the same using gust the ip address of a computer. any ideas will be great.
0
Comment
Question by:kplonk
  • 5
  • 5
  • 2
12 Comments
 
LVL 24

Expert Comment

by:SunBow
ID: 2582124
What is it you want to do. A human at both machines doing a chat? A program running on each machine to move files back and forth, like FTP does? A script or instruction that one passes to the other to tell it what to do?

It looks like your current state is that you have code to move files via IP address. True? I am not clear one why you do not want your program to acces the file, since it looks like it has to also be able to create it.
0
 

Author Comment

by:kplonk
ID: 2582287
i want to make a chat progam that ueses tcp/ip. But know nothing about it
0
 
LVL 14

Accepted Solution

by:
mcrider earned 50 total points
ID: 2582584
Here is a very small chat server built with the winsock control.  Use this an an example.


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 can also create your own client that will connect to this server on port 1001 if you want...



THE CODE:
---------------------------------------


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
 

Author Comment

by:kplonk
ID: 2583601
looks good will try but this not exactly what i wanted. What i need is a expliniation of tcp/ip and will this eventuly work over the inter net?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2583644
Yes, the code I gave you will work over the internet...

TCP/IP in 50 words or less:

TCP/IP is the communication protocol that the Internet uses to allow 2 machines to talk to eachother.  Every machine on the internet has a unique TCP/IP address.  This address is used to "route" the information from point A to B...

A system's connection is handled through a socket. (in the case of VB, the winsock control.)  A individual connection is handled using a port number... There are several ports that are designated as "well known ports".  These ports support different protocols.

For example FTP operates on port 21.  So, if your IP address is 123.45.67.89 and you are running an FTP SERVER, people could connect to that address and port to transfer files...

The example of a chat server I gave you uses the winsock control and operates on port 1001.  Anyone that connects to your system on port 1001 will be connected to the chat server...


Make sense???


Cheers!®©
0
 
LVL 24

Expert Comment

by:SunBow
ID: 2589881
So it is two humans in your house (non-internet) soing a chat! I did not suspect, but in part queried on it since there are an abundance of sample code for this. Also for UDP. I thought VB included a sample(s).

I'll skip review of the proposed, you may now be happy.  But caution here, be very wary about what you want to do over the internet, it is much less simple and you become that much more vulnerable. Even to access friend/family in town. ALso look up on proxy.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:kplonk
ID: 2641715
works well but i need to know some ideas on how to write the client as i do not want to use telnet
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2644436
Using the server as a guideline, you would do the following to connect:

   Winsock1.RemoteHost = "servername"
   Winsock1.RemotePort = 1001
   Winsock1.Connect


Then in the DataArrival event of the winsock control, you would use the winsock GetData function to read information sent to you from the server.

When you want to send data, you would use the winsock SendData function...


Cheers!®©

0
 

Author Comment

by:kplonk
ID: 2694913
i have don this but i have a problem. the text that i send top the serve is only echoed to the person who typed it and not to all the others any idase here si the code

Private Sub Form_Load()

    Winsock1.RemoteHost = "192.168.0.2"
    Winsock1.RemotePort = 1001
    Winsock1.Connect
   
    NL = Chr(13) + Chr(10)
   
End Sub



Private Sub txtText_KeyPress(KeyAscii As Integer)
Dim strData

    If KeyAscii = vbKeyReturn Then
        strData = txtText
        Winsock1.SendData strData
        txtText.Text = ""
        txtMain.Text = txtMain.Text + vbCrLf
    End If
   
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

    Winsock1.GetData strData, vbString
    txtMain.Text = txtMain.Text + strData
   
End Sub

thanks for all the help
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2695207
Do you have Service Pack 3 installed?  It not, you need to install it.  It fixes a bunch of problems with Winsock...

Also, try DoEvents in your code... Like This:

Private Sub txtText_KeyPress(KeyAscii As Integer)
Dim strData

    If KeyAscii = vbKeyReturn Then
        strData = txtText
        Winsock1.SendData strData
        DoEvents
        txtText.Text = ""
        txtMain.Text = txtMain.Text + vbCrLf
    End If
     
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)

Dim strData As String

    Winsock1.GetData strData, vbString
    txtMain.Text = txtMain.Text + strData
    DoEvents
     
End Sub




Cheers!®©
0
 

Author Comment

by:kplonk
ID: 2760838
thanks for all the help
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2761292
Thanks for the points! Glad I could help!


Cheers!®©
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
This article is an update and follow-up of my previous article:   Storage 101: common concepts in the IT enterprise storage This time, I expand on more frequently used storage concepts.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now