a simple client example

A

Andreas Ott

Hello,

I looking for a simple client example.
My is not working.

Every time I receive a exception.
catch (Exception e)
{
Console.WriteLine(e.Message);
}

My goal is.
I need a client for receive data, without limit for logging.
The client is / must be every time on state --- receive

Can somebody help me.


Thanks.

Regards
Andreas




class Program
{
static void Main(string[] args)
{
if ((args.Length < 1) || (args.Length > 3))
{
throw new ArgumentException("Parameters: <Word>
[<Server>] [<Port>]");
}

byte[] byteBuffer = Encoding.ASCII.GetBytes(args[0]);


string server = (args.Length == 1) ? args[0].ToString() :
Dns.GetHostName();

int servPort = (args.Length == 2) ? Int32.Parse(args[1]) : 7;

TcpClient tcpClient = null;
NetworkStream netStream = null;

try
{
tcpClient = new TcpClient(server, servPort);

// Uses the GetStream public method to return the
NetworkStream.
netStream = tcpClient.GetStream();

//if (netStream.CanWrite)
//{
// Byte[] sendBytes = Encoding.UTF8.GetBytes("Is
anybody there?");
// // netStream.Write(sendBytes, 0, sendBytes.Length);
//}
//else
//{
// Console.WriteLine("You cannot write data to this
stream.");
// tcpClient.Close();

// // Closing the tcpClient instance does not close
the network stream.
// netStream.Close();
// return;
//}

while (true)
{
if (netStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new
byte[tcpClient.ReceiveBufferSize];

// Read can return anything from 0 to
numBytesToRead.
// This method blocks until at least one byte
is read.
netStream.Read(bytes, 0,
(int)tcpClient.ReceiveBufferSize);

// Returns the data received from the host to
the console.
string returndata =
Encoding.UTF8.GetString(bytes);

Console.WriteLine("This is what the host
returned to you: " + returndata);

}
}
//else
//{
Console.WriteLine("You cannot read data from this
stream.");
tcpClient.Close();

// Closing the tcpClient instance does not close
the network stream.
netStream.Close();
return;
//}
//netStream.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

}
}
}
 
A

Arne Vajhøj

Andreas said:
I looking for a simple client example.
My is not working.

Every time I receive a exception.
catch (Exception e)
{
Console.WriteLine(e.Message);
}

What exception ? In what line ?

Arne
 
P

Peter Duniho

Good morning,
here!

No. That's where the exception is _caught_, not thrown.
Message "No connection"
ErrorCode 10061 int

Just looking at the code, I'd say one possible problem is that you don't
understand the NetworkStream.Read() method. In particular, while your
first comment states correctly that the return value will be "anything
from 0 to numBytesToRead", the second comment incorrectly states that the
"method blocks until at least one byte is read", and you fail to check for
a 0 return value, indicating the end-of-stream.

And if you try to read from a socket once it's reached the end-of-stream,
it's entirely possible you'd get the "no connection" exception you're
seeing.

If that doesn't help you fix your code, then you need to provide a more
complete example. In particular, seeing the client side code isn't
sufficient; there's no way to be sure we can reproduce the problem unless
you provide enough information to use that client code with some specific
server. I.e. either provide the server code itself, or provide the
necessary command line arguments that one would require in order to
connect to an appropriate server.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top