By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

Inconsistent behaviour on a network project.

P: n/a
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[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().Pro cessName ==
"EmulatorSuite" )
System.Diagnostics.Process.GetCurrentProcess().Clo se();
}
#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\Emula torSuite.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.
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Everyone relax, panic over. I found the problem....
[Test]
public void Connect()
{
System.Diagnostics.Process.Start(
@"D:\EmulatorSuite\EmulatorSuite\bin\Release\Emula torSuite.exe", "666" );


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.
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.