Acts as a client program to demonstrate the use of the NetworkStream class : NetworkStream « Network « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Network » NetworkStreamScreenshots 
Acts as a client program to demonstrate the use of the NetworkStream class
Acts as a client program to demonstrate the use of the NetworkStream class

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

// NStrCli.cs -- Acts as a client program to demonstrate the use of
//               the NetworkStream class.
//
//               Compile this program with the following command line.
//                   C:>csc NStrCli.cs
//
// To use this program with the companion server program, first start
// the server, then connect to it using the client program (this program)
// in a separate console window.
// As you enter lines from the client, they will appear in the server
// console window. To end the session, press <Enter> on a blank line.
//
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace nsStreams
{
    public class NStrCli
    {
        static public void Main ()
        {
            IPAddress hostadd = Dns.Resolve("localhost").AddressList[0];
            Console.WriteLine ("Host is " + hostadd.ToString());
            IPEndPoint EPhost = new IPEndPoint(hostadd, 2048);

            Socket s = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Stream,
                                  ProtocolType.Tcp );
            string str = "Hello, World!";
            byte [] b;
            StringToByte (out b, str);
            s.Connect (EPhost);
            NetworkStream strm = new NetworkStream (s, FileAccess.ReadWrite);
            if (!s.Connected)
            {
                Console.WriteLine ("Unable to connect to host");
                return;
            }
            strm.Write (b, 0, b.Length);
            while (b[0!= 4)
            {
                string text = Console.ReadLine();
                if (text.Length == 0)
                {
                    b[04;
                    strm.Write (b, 01);
                    break;
                }
                StringToByte (out b, text);
                strm.Write (b, 0, text.Length);
            }
            strm.Close ();
            s.Close ();
        }
//
// Convert a buffer of type byte to a string
        static string ByteToString (byte [] b, int start)
        {
            string str = "";
            for (int x = start; x < b.Length; ++x)
            {
                str += (char[x];
            }
            return (str);
        }
//
// Convert a buffer of type string to byte
        static void StringToByte (out byte [] b, string str)
        {
            b = new byte [str.Length];
            for (int x = 0; x < str.Length; ++x)
            {
                b[x(bytestr [x];
            }
        }
   }
}

           
       
Related examples in the same category
1.Write to a NetworkStream
2.implements a NetworkStream server
3.implements a NetworkStream client 2
4.Acts as a server program to demonstrate the use of the NetworkStream classActs as a server program to demonstrate the use of the NetworkStream class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.