|
-
Jul 8th, 2008, 03:58 PM
#1
Thread Starter
Addicted Member
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).
-
Jul 8th, 2008, 04:39 PM
#2
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.
-
Jul 8th, 2008, 04:41 PM
#3
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.
-
Jul 9th, 2008, 01:35 PM
#4
Thread Starter
Addicted Member
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:
Public Class Form1 Private client As System.Net.Sockets.TcpClient Private Const BYTES_TO_READ As Integer = 255 Private readBuffer(BYTES_TO_READ) As Byte Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub tryconnect() Dim jj As Boolean = True Do While jj = True Application.DoEvents() Try Me.Text = "Connecting..." Timer1.Enabled = False client = New System.Net.Sockets.TcpClient("192.168.0.102", 8882) client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) Me.Text = "Connected to 192.168.0.102 (MASTER)" jj = False Catch ex As Exception End Try Loop End Sub Private Sub doRead(ByVal ar As System.IAsyncResult) Dim totalRead As Integer Try totalRead = client.GetStream.EndRead(ar) 'Ends the reading and returns the number of bytes read. Catch ex As Exception '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 'to this client and remove it from the list of connected clients. End Try If totalRead > 0 Then 'the readBuffer array will contain everything read from the client. Dim receivedString As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, totalRead) messageReceived(receivedString) End If Try client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, AddressOf doRead, Nothing) 'Begin the reading again. Catch ex As Exception '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 'to this client and remove it from the list of connected clients. End Try End Sub Private Sub messageReceived(ByVal message As String) Select Case message Case "exit" client.Close() Timer1.Enabled = True Case "who" SendMessage("slave1") Case Else MessageBox.Show(message) End Select End Sub Private Sub SendMessage(ByVal msg As String) Dim sw As IO.StreamWriter Try sw = New IO.StreamWriter(client.GetStream) sw.Write(msg) sw.Flush() Catch ex As Exception End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sw As IO.StreamWriter Try sw = New IO.StreamWriter(client.GetStream) sw.Write("Hello my Master") sw.Flush() Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick tryconnect() End Sub 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|