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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.