|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
a simple client exampleI 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 { } } } Andreas Ott wrote:
> I looking for a simple client example. What exception ? In what line ?> My is not working. > > Every time I receive a exception. > catch (Exception e) > { > Console.WriteLine(e.Message); > } Arne Good morning,
>> My is not working. Message "No connection" >> >> Every time I receive a exception. >> catch (Exception e) >> { >> Console.WriteLine(e.Message); here! >> } > > What exception ? In what line ? > ErrorCode 10061 int Regards Andreas On Fri, 03 Jul 2009 21:29:16 -0700, Andreas Ott <a**@dis.microsoft.com>
wrote: > Good morning, No. That's where the exception is _caught_, not thrown.>>> My is not working. >>> >>> Every time I receive a exception. >>> catch (Exception e) >>> { >>> Console.WriteLine(e.Message); > here! >>> } Just looking at the code, I'd say one possible problem is that you don't >> What exception ? In what line ? >> > > Message "No connection" > ErrorCode 10061 int 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
Other interesting topics
C# program creating folder on server/access denied
Binary Serialization overhead Filter list with LINQ Problem setting system time... COM control passed to .NET Get ContentType ASCII to hex/binary get current platform (x86, x64) How to sort a ListBox with time format strings Is MemoryStream compressed? |
|||||||||||||||||||||||