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
{
}
}
}
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
{
}
}
}