F
Frosty Madness
I 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 == 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 EmulatorTcpServer( port );
server.Start();
}
catch( Exception e )
{
Console.WriteLine( e.Message );
Console.Read();
}
}
}
*************************
I'm having a problem with consistency depending on how I start the server...
*************************
[Test]
public void Connect()
{
System.Diagnostics.Process.Start(
@"D:\EmulatorSuite\EmulatorSuite\bin\Release\EmulatorSuite.exe", "666" );
//EmulatorTcpServer server = new EmulatorTcpServer( port );
//server.BeginStart();
TcpClient client = new TcpClient( "localhost", 666 );
Byte[] data = System.Text.Encoding.ASCII.GetBytes( "Input1" +
Environment.NewLine );
NetworkStream stream = client.GetStream();
stream.Write( data, 0, data.Length );
data = new byte[3];
string responseData = String.Empty;
int bytes = stream.Read( data, 0, data.Length );
responseData = System.Text.Encoding.ASCII.GetString( data, 0, bytes );
data = System.Text.Encoding.ASCII.GetBytes( "KILL" + Environment.NewLine );
stream.Write( data, 0, data.Length );
client.Close();
}
**********************
This hangs when trying to write data, the server receives the Input1 OK, but
the Network stream just says there is no data available afterwards.
If I uncomment lines 2 and 3 of the test method, and comment out the line
launching the process, it doesn't hang, although the server doesn't seem to
receive the KILL command.
If I comment out all 3 of the first lines, and start the console server
manually, it seems to work fine too.
Help!
..Net 1.1.
2000 Professional SP4
Regards,
Frosty.
**************
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 == 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 EmulatorTcpServer( port );
server.Start();
}
catch( Exception e )
{
Console.WriteLine( e.Message );
Console.Read();
}
}
}
*************************
I'm having a problem with consistency depending on how I start the server...
*************************
[Test]
public void Connect()
{
System.Diagnostics.Process.Start(
@"D:\EmulatorSuite\EmulatorSuite\bin\Release\EmulatorSuite.exe", "666" );
//EmulatorTcpServer server = new EmulatorTcpServer( port );
//server.BeginStart();
TcpClient client = new TcpClient( "localhost", 666 );
Byte[] data = System.Text.Encoding.ASCII.GetBytes( "Input1" +
Environment.NewLine );
NetworkStream stream = client.GetStream();
stream.Write( data, 0, data.Length );
data = new byte[3];
string responseData = String.Empty;
int bytes = stream.Read( data, 0, data.Length );
responseData = System.Text.Encoding.ASCII.GetString( data, 0, bytes );
data = System.Text.Encoding.ASCII.GetBytes( "KILL" + Environment.NewLine );
stream.Write( data, 0, data.Length );
client.Close();
}
**********************
This hangs when trying to write data, the server receives the Input1 OK, but
the Network stream just says there is no data available afterwards.
If I uncomment lines 2 and 3 of the test method, and comment out the line
launching the process, it doesn't hang, although the server doesn't seem to
receive the KILL command.
If I comment out all 3 of the first lines, and start the console server
manually, it seems to work fine too.
Help!
..Net 1.1.
2000 Professional SP4
Regards,
Frosty.