Results 1 to 4 of 4

Thread: Stuck on TCP/IP "Server"....

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Stuck on TCP/IP "Server"....

    I'm looked all across the internet at tutorial after tutorial for a SIMPLE TCP/IP communication program, and found nothing. Just a slew of programs built to perform complicated, but routine functions.

    I dont need a Server/Client connections. I just need Master/Slave connections.
    I have a Master computer, that I need to call other slave computers and communicate with them. So I guess it's the reverse of a Server/Client.

    I have a handful of "stupid" or "Slave" computers, their soul job is to listen for a command, then execute said command.

    With this will be a single Master computer. I need it to call the slaves via their IP address, and issue commands to them. I have this working (it was done in Python, rather than VB.NET, so no code to show here).


    But what I'm stuck on is, is there a simple way to have the slave computers waiting, listening to a port, accept a connection, then once connected with the Master computer, be able to read/write simple ascii with the Master?

    I dont need synchronous connecting, or dictionaries of clients (for there will be only one master to it), etc.

    All the tutorials I find are for too complicated for me, and it's difficult weeding out useless code from the code I actually need. (I'd post the tutorial, however it's many pages long).

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Stuck on TCP/IP "Server"....

    Here is a pretty simple sample on using Sockets in .Net:

    http://www.eggheadcafe.com/articles/20020323.asp


    If I was writing the app you are talking about, I would probably have the slaves register with the master when they started and unregister when they are closing. You would then have the master keep track of what slaves are available.

    Basically your master will have a TCPListener listening for slaves starting up and shutting down and it will create TCPClients that connect to each slave when a command needs to be sent.

    Your slave will create a TCPClient on startup and shutdown which lets the master know if it is available. It will also create a listener that will wait for a command from the master.

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Stuck on TCP/IP "Server"....

    Click the link in my signature, but chances are that you've already come across that thread.
    The "slave" computers would need to have a TcpListener each, listening on a port. The "master" computer would connect to the "slave" computers using a TcpClient, one for each connection.
    Once you're connected, you can call the GetStream method of the TcpClient(s), which will return a Stream...use this stream to both read and write from the network.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Stuck on TCP/IP "Server"....

    Ok, so I went ahead (after thinking hard about it) and made the "Slaves" clients and the "Master" a server.

    I'm using Atheist's code and it works perfectly. I can get this part done from here....

    Now my next problem, how can I make the system know there was a connection error, and start the connection process over again? Each time I try something, it just holds onto the port, and wont let it go. So after the server is reset, just shows "port in use" in the server.

    Something like (say if server program restarts):
    If client's connection to server = False then
    Let go of the port
    tryconnect()

    Can anyone point me in the right direction?


    Please note, there is ALOT of debug lines still left in code. Please ignore these....
    vb Code:
    1. Public Class Form1
    2.     Private client As System.Net.Sockets.TcpClient
    3.  
    4.     Private Const BYTES_TO_READ As Integer = 255
    5.     Private readBuffer(BYTES_TO_READ) As Byte
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.  
    9.     End Sub
    10.  
    11.     Private Sub tryconnect()
    12.         Dim jj As Boolean = True
    13.         Do While jj = True
    14.             Application.DoEvents()
    15.             Try
    16.                 Me.Text = "Connecting..."
    17.                 Timer1.Enabled = False
    18.                 client = New System.Net.Sockets.TcpClient("192.168.0.102", 8882)
    19.                 client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing)
    20.                 Me.Text = "Connected to 192.168.0.102 (MASTER)"
    21.                 jj = False
    22.             Catch ex As Exception
    23.             End Try
    24.         Loop
    25.     End Sub
    26.  
    27.     Private Sub doRead(ByVal ar As System.IAsyncResult)
    28.         Dim totalRead As Integer
    29.         Try
    30.             totalRead = client.GetStream.EndRead(ar) 'Ends the reading and returns the number of bytes read.
    31.         Catch ex As Exception
    32.             'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
    33.             'to this client and remove it from the list of connected clients.
    34.         End Try
    35.  
    36.         If totalRead > 0 Then
    37.             'the readBuffer array will contain everything read from the client.
    38.             Dim receivedString As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, totalRead)
    39.             messageReceived(receivedString)
    40.         End If
    41.  
    42.  
    43.  
    44.         Try
    45.             client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) 'Begin the reading again.
    46.         Catch ex As Exception
    47.             'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections
    48.             'to this client and remove it from the list of connected clients.
    49.         End Try
    50.     End Sub
    51.  
    52.     Private Sub messageReceived(ByVal message As String)
    53.         Select Case message
    54.             Case "exit"
    55.                 client.Close()
    56.                 Timer1.Enabled = True
    57.             Case "who"
    58.                 SendMessage("slave1")
    59.             Case Else
    60.                 MessageBox.Show(message)
    61.         End Select
    62.     End Sub
    63.  
    64.  
    65.  
    66.     Private Sub SendMessage(ByVal msg As String)
    67.         Dim sw As IO.StreamWriter
    68.         Try
    69.             sw = New IO.StreamWriter(client.GetStream)
    70.             sw.Write(msg)
    71.             sw.Flush()
    72.         Catch ex As Exception
    73.  
    74.         End Try
    75.     End Sub
    76.  
    77.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    78.         Dim sw As IO.StreamWriter
    79.         Try
    80.             sw = New IO.StreamWriter(client.GetStream)
    81.             sw.Write("Hello my Master")
    82.             sw.Flush()
    83.         Catch ex As Exception
    84.             MessageBox.Show(ex.ToString)
    85.         End Try
    86.     End Sub
    87.  
    88.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    89.         tryconnect()
    90.     End Sub
    91.  
    92. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width