Home All Groups Group Topic Archive Search About
Author
3 Jul 2009 8:43 PM
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
             {

             }
         }
     }

Author
4 Jul 2009 12:45 AM
Arne_Vajhøj
Andreas Ott wrote:
> 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
Are all your drivers up to date? click for free checkup

Author
4 Jul 2009 4:29 AM
Andreas Ott
Good morning,
>> My is not working.
>>
>> Every time I receive a exception.
>>             catch (Exception e)
>>             {
>>                 Console.WriteLine(e.Message);
here!
>>             }
>
> What exception ? In what line ?
>

        Message    "No connection"   
        ErrorCode    10061    int


Regards Andreas
Author
4 Jul 2009 6:11 AM
Peter Duniho
On Fri, 03 Jul 2009 21:29:16 -0700, Andreas Ott <a**@dis.microsoft.com> 
wrote:

> Good morning,
>>> My is not working.
>>>
>>> Every time I receive a exception.
>>>             catch (Exception e)
>>>             {
>>>                 Console.WriteLine(e.Message);
> here!

No.  That's where the exception is _caught_, not thrown.

>>>             }
>>  What exception ? In what line ?
>>
>
>         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

Bookmark and Share