Inconsistent behaviour on a network project.

Inconsistent behaviour on a network project.

Post by Frosty Mad » Thu, 04 Aug 2005 22:44:54


have the beginnings of a device emulator that sits on the network...

**************

using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace EmulatorSuite.SmartLase110i
{
public class EmulatorTcpServer
{
#region Fields

int port;
TcpListener server;
Thread listeningThread;
Socket myClient;
byte newLine;

#endregion

#region Construction

public EmulatorTcpServer( int port )
{
this.port = port;
this.server = new TcpListener( IPAddress.Any, port );
this.newLine = Encoding.ASCII.GetBytes(
Environment.NewLine )[0];
}

#endregion

#region Methods

public void Stop()
{
if ( listeningThread != null )
listeningThread.Abort();
}

public void BeginStart()
{
server.Start();
ThreadStart ts = new ThreadStart( Receive );
listeningThread = new Thread( ts );
listeningThread.Start();
}

public void Start()
{
server.Start();
Receive();
}

private void Receive()
{
Console.WriteLine( "Waiting for connection." );
myClient = server.AcceptSocket();
Console.WriteLine( "Connected." );

while ( true )
{
bool receivedCR = false;
int bytesReceived = 0;
byte[] buffer;
StringBuilder commandBuilder = new StringBuilder();

while ( ! receivedCR )
{
buffer = new byte[1024];
if ( myClient.Available > 0 )
{
bytesReceived += myClient.Receive( buffer );
}
else
{
Console.WriteLine( "No data." );
Thread.Sleep( 1000 );
}

Console.WriteLine( bytesReceived );

for ( int i = 0; i < bytesReceived && ! receivedCR ;
i++ )
{
if ( buffer[i] == this.newLine )
receivedCR = true;
else
commandBuilder.Append(
Encoding.ASCII.GetString( buffer, i, 1 ) );
}
}

HandleCommand( myClient, commandBuilder.ToString() );
}
}

public void HandleCommand( Socket socket, string command )
{
Console.WriteLine( "Received command : " + command );
socket.Send( Encoding.ASCII.GetBytes( Environment.NewLine +
"SL1>" ) );
if ( command == "KILL" &&
System.Diagnostics.Process.GetCurrentProcess().ProcessName ==
"EmulatorSuite" )
System.Diagnostics.Process.GetCurrentProcess().Close();
}
#endregion Methods
}
}


*************************

I have a console app too...

*************************

public class Executable
{
private Executable()
{
}

static void Main( string[] args )
{
try
{
int port;
if ( args.Length == 0 )
{
port = 666;
}
else
{
port = int.Parse( args[0] );
}

EmulatorTcpServer server = new Em
 
 
 

Inconsistent behaviour on a network project.

Post by Frosty Mad » Thu, 04 Aug 2005 23:02:26

Everyone relax, panic over. I found the problem....


Hard coded path to the Release version, I was working in debug so the
release version wasn't being updated. D'oh!

Stupid absolute paths.

<sulks/>

Frosty.